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
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/11/spring-cloud-manually-define-feign-client/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
Spring Cloud手动定义Feign客户端
手动定义Feign客户端,可以灵活设置需要调用的服务
基本实现
@RestController
@Import(FeignClientsConfiguration.class)
public class AppblogService imple……
文章目录
关闭
共有 0 条评论