Spring Cloud手动定义Feign客户端

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

基本实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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

1
2
3
4
5
6
7
8
9
10
11
12
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
//@EnableEurekaClient
@Import(FeignClientsConfiguration.class)
public class ChannelFrontApplication {

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

}

FeignConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@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

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :