Spring Cloud手动定义Feign客户端

手动定义Feign客户端,可以灵活设置需要调用的服务

基本实现

@RestController
@Import(FeignClientsConfiguration.class)
public class AppblogService implements IAppblogService {

    private ClientApi clientApi;

    @Autowired
    public AppblogService(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        clientApi = Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract)
                //.target(ClientApi.class, "http://appblog-pay-provider-channel-gateway-alipay")
                //.target(Target.EmptyTarget.create(ClientApi.class, "appblog-pay-provider-channel-gateway-alipay"));
                .target(new Target.HardCodedTarget<ClientApi>(ClientApi.class, "appblog-pay-provider-channel-gateway-alipay", "http://appblog-pay-provider-channel-gateway-alipay"));
    }

    ...

封装使用

Application.java

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
//@EnableEurekaClient
@Import(FeignClientsConfiguration.class)
public class ChannelFrontApplication {

    public static void main(String[] args) {
        SpringApplication.run(ChannelFrontApplication.class, args);
    }

}

FeignConfig.java

import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Data
@Component
public class FeignConfig {
    private Feign.Builder feignBuilder;

    @Autowired
    public FeignConfig(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        feignBuilder = Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract);
    }
}

ChannelPayService.java

@Resource
private FeignConfig feignConfig;
private Map<String, ChannelGatewayPayApi> channelGatewayApiMap = new HashMap<>();
private Map<String, String> channelGatewayMap = new HashMap<>();
private Map<String, ChannelInfo> channelInfoMap = new HashMap<>();

@ResponseBody
@Override
public Result<ChannelPayResponse> pay(ChannelPayRequest createRequest) {

    ...

    ChannelGatewayPayApi channelGatewayPayApi;
    if ((channelGatewayPayApi = channelGatewayApiMap.get(channelInfo.getChnlCode())) == null) {
        channelGatewayPayApi = feignConfig.getFeignBuilder().target(ChannelGatewayPayApi.class, "http://" + channelProduct.getServiceAppName());
        channelGatewayApiMap.put(channelInfo.getChnlCode(), channelGatewayPayApi);
        channelGatewayMap.put(channelInfo.getChnlCode(), channelProduct.getServiceAppName().substring(channelProduct.getServiceAppName().indexOf("gateway") + 8));
    }

    ...

    Result<ChannelGatewayPayCreateResponse> channelGatewayResult = channelGatewayPayApi.pay(request);

    ...
}

参考:https://www.cnblogs.com/jinjiyese153/p/8664370.html
参考:https://my.oschina.net/joryqiao/blog/1925633
参考:https://stackoverflow.com/questions/43733569/how-can-i-change-the-feign-url-during-the-runtime

上一篇 Spring Boot 2.X返回json数据中null字段不显示解决方法
下一篇 Spring Cloud Feign传递map对象和多媒体文件
目录
文章列表
1 Swift UI - 微调器或步进器(UIStepper)
Swift UI - 微调器或步进器(UIStepper)
2
Resource IDs cannot be used in a switch statement in Android library modules
Resource IDs cannot be used in a switch statement in Android library modules
3
Swift UI - 使用UIWebView和UIToolbar制作一个浏览器
Swift UI - 使用UIWebView和UIToolbar制作一个浏览器
4
CentOS下安装Confluence 7.1.1 bin包
CentOS下安装Confluence 7.1.1 bin包
5
Android编译OpenSSL库报错ui_openssl.c:function read_string_inner
Android编译OpenSSL库报错ui_openssl.c:function read_string_inner
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。