网站导航/权重查询工具
文章目录
- @[TOC](文章目录)
- 前言
- 一、openfeign 集成
- 二、配置使用
- 1.全局feign 配置
- 2.yml feign配置
- 总结
文章目录
- @[TOC](文章目录)
- 前言
- 一、openfeign 集成
- 二、配置使用
- 1.全局feign 配置
- 2.yml feign配置
- 总结
前言
feign 相对于 resttemplate 来说不仅兼具他的功能,而且比他更强大,所以当项目为springcloud架构之后,无论是服务之间的调用,还是其他的http调用,都应该统一用feign处理;
如果是单体项目,则resttemplate优先考虑;
一、openfeign 集成
implementation 'org.springframework.boot:spring-boot-starter:2.6.3'implementation 'org.springframework.boot:spring-boot-starter-data-mongodb:2.6.3'implementation 'org.springframework.boot:spring-boot-starter-validation:2.6.3'implementation 'org.springframework.boot:spring-boot-starter-web:2.6.3'implementation 'org.projectlombok:lombok:1.18.22'implementation 'cn.hutool:hutool-all:5.8.8'implementation 'io.springfox:springfox-boot-starter:3.0.0'implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:2021.0.1.0'implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config:2021.0.1.0'implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.0'implementation 'com.alibaba.cloud:spring-cloud-alibaba-dependencies:2021.0.1.0'implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:2.2.8.RELEASE'
二、配置使用
1.全局feign 配置
FeignConfig
Configuration
@Slf4j
public class FeignConfig {@Beanpublic feign.Logger logger() {return new Slf4jLogger();}/*** 跳过https** @return Client* @throws NoSuchAlgorithmException 未找到异常* @throws KeyManagementException 秘钥异常*/@Bean@ConditionalOnMissingBeanpublic Client feiClient() throws NoSuchAlgorithmException, KeyManagementException {SSLContext ctx = SSLContext.getInstance("SSL");X509TrustManager tm = new X509TrustManager() {@Overridepublic void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return null;}};ctx.init(null, new TrustManager[]{tm}, null);return new Client.Default(ctx.getSocketFactory(), new HostnameVerifier() {@Overridepublic boolean verify(String hostname, SSLSession sslSession) {return true;}});}/*** 自定义错误解析** @return 错误*/@Beanpublic ErrorDecoder errorDecoder() {return new ErrorDecoder() {@Overridepublic Exception decode(String methodKey, Response response) {try {InputStream inputStream = response.body().asInputStream();JSONObject jsonObject = JSONUtil.parseObj(String.valueOf(IoUtil.read(inputStream)));inputStream.close();return ExceptionUtil.wrapRuntime("返回异常" + JSONUtil.toJsonStr(jsonObject));} catch (IOException e) {e.printStackTrace();}return null;}};}
}
2.yml feign配置
具体如下:
# feign 日志 超时配置
feign:client:config:default:loggerLevel: fullhttpclient:connection-timeout: 5000connection-timer-repeat: 5000
# mongodb 日志打印
logging:level:feign.Logger: debug
这样配置两处,之后,所有的feign就都会打印日志了;
总结
网上也有单独配置某个feign 的方法; 传送门
这里推荐全局配置,这样省去了逐个配置的麻烦,然后这样有助于在生产环境定位问题,待服务运行平稳之后,再将feign日志去掉,或者整体调整服务日志级别 打印等;