Spring Cloud Feign高级应用实例详解


Posted in Python onDecember 10, 2019

这篇文章主要介绍了Spring Cloud Feign高级应用实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.使用feign进行服务间的调用

2.开启gzip压缩

Feign支持对请求与响应的压缩,以提高通信效率,需要在服务消费者配置文件开启压缩支持和压缩文件的类型

添加配置

feign.compression.request.enabled=true
feign.compression.response.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048

3.开启日志

配置

logging.level.com.xyz.comsumer.feign.RemoteHelloService=debug

添加FeignLogConfig类

package com.xyz.comsumer.configure;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignLogConfig {
  @Bean
  Logger.Level feignLogger(){
    return Logger.Level.FULL;
  }
}

输出

2019-12-07 23:23:03.630 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] <--- HTTP/1.1 200 (671ms)
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] content-length: 14
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] content-type: text/plain;charset=UTF-8
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] date: Sat, 07 Dec 2019 15:23:03 GMT
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Origin
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Access-Control-Request-Method
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Access-Control-Request-Headers
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] 
2019-12-07 23:23:03.632 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] hello,provider
2019-12-07 23:23:03.632 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] <--- END HTTP (14-byte body)

说明:

Feign日志记录只能响应DEBUG日志级别

对每一个Feign客户端,可以配置一个Logger.Level对象,通过该对象控制日志输出内容。

Logger.Level有如下几种选择:

NONE, 不记录日志 (默认)。

BASIC, 只记录请求方法和URL以及响应状态代码和执行时间。

HEADERS, 记录请求和应答的头的基本信息。

FULL, 记录请求和响应的头信息,正文和元数据。

4.替换JDK默认的URLConnection为okhttp

在默认情况下 spring cloud feign在进行各个子服务之间的调用时,http组件使用的是jdk的HttpURLConnection

服务之间调用使用的HttpURLConnection,效率非常低

为了提高效率,可以通过连接池提高效率

使用okhttp,能提高qps,因为okhttp有连接池和超时时间进行调优

添加依赖

<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-okhttp</artifactId>
</dependency>

修改配置

禁用默认的http,启用okhttp

feign.okhttp.enabled=true
feign.httpclient.enabled=false

添加FeignOkHttpConfig类

package com.xyz.comsumer.configure;

import feign.Feign;
import okhttp3.ConnectionPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig {

  @Bean
  public okhttp3.OkHttpClient okHttpClient(){
    return new okhttp3.OkHttpClient.Builder()
        .readTimeout(10, TimeUnit.SECONDS)
        .connectTimeout(10, TimeUnit.SECONDS)
        .writeTimeout(20, TimeUnit.SECONDS)
        .retryOnConnectionFailure(true)
        .connectionPool(new ConnectionPool())
        .build();
  }
}

5.超时设置

Feign调用服务的默认时长是1秒钟

hystrix的超时时间

hystrix.command.default.execution.timeout.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000

说明:

hystrix.command.default.execution.timeout.enabled 执行是否启用超时,默认启用true

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds 命令执行超时时间,默认1000ms

常用的配置还有

hystrix.command.default.execution.isolation.strategy 隔离策略,默认是Thread, 可选THREAD|SEMAPHORE,建议选择SEMAPHORE

Feign 的负载均衡底层用的就是 Ribbon

ribbon的超时时间

ribbon.ReadTimeout=10000
ribbon.ConnectTimeout=10000

6.使用hystrix进行熔断、降级处理

feign启用hystrix,才能熔断、降级

feign.hystrix.enabled=true

hystrix服务降级处理

RemoteHelloServiceFallbackImpl

package com.xyz.comsumer.feign.fallback;

import com.xyz.comsumer.feign.RemoteHelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class RemoteHelloServiceFallbackImpl implements RemoteHelloService {
  private final Logger logger = LoggerFactory.getLogger(RemoteHelloServiceFallbackImpl.class);

  @Override
  public String hello() {
    logger.error("feign 查询信息失败:{}");
    return null;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python2中文处理纪要的实现方法
Mar 10 Python
python  创建一个保留重复值的列表的补码
Oct 15 Python
python3实现逐字输出的方法
Jan 23 Python
python SQLAlchemy的Mapping与Declarative详解
Jul 04 Python
python3.8 微信发送服务器监控报警消息代码实现
Nov 05 Python
Pytorch to(device)用法
Jan 08 Python
Python selenium页面加载慢超时的解决方案
Mar 18 Python
如何解决安装python3.6.1失败
Jul 01 Python
六种酷炫Python运行进度条效果的实现代码
Jul 17 Python
Python中Selenium库使用教程详解
Jul 23 Python
Python try except else使用详解
Jan 12 Python
Python 中Operator模块的使用
Jan 30 Python
flask 使用 flask_apscheduler 做定时循环任务的实现
Dec 10 #Python
使用opencv将视频帧转成图片输出
Dec 10 #Python
django框架cookie和session用法实例详解
Dec 10 #Python
python selenium实现发送带附件的邮件代码实例
Dec 10 #Python
opencv设置采集视频分辨率方式
Dec 10 #Python
django框架forms组件用法实例详解
Dec 10 #Python
django框架auth模块用法实例详解
Dec 10 #Python
You might like
对text数据类型不支持代码页转换 从: 1252 到: 936
2011/04/23 PHP
使用php统计字符串中中英文字符的个数
2013/06/23 PHP
PHPAnalysis中文分词类详解
2014/06/13 PHP
PHP7标量类型declare用法实例分析
2016/09/26 PHP
Laravel创建数据库表结构的例子
2019/10/09 PHP
PHP哈希表实现算法原理解析
2020/12/11 PHP
关于递归运算的顺序测试代码
2011/11/30 Javascript
jQuery contains过滤器实现精确匹配使用方法
2013/04/12 Javascript
JavaScript中instanceof运算符的用法总结
2013/11/19 Javascript
Javascript限制网页只能在微信内置浏览器中访问
2014/11/09 Javascript
jquery+css实现动感的图片切换效果
2015/11/25 Javascript
jQuery给指定的table动态添加删除行的操作方法
2016/10/12 Javascript
JQuery学习总结【二】
2016/12/01 Javascript
JQuery Dialog对话框 不能通过Esc关闭的原因分析及解决办法
2017/01/18 Javascript
bootstrap datetimepicker日期插件超详细使用方法介绍
2017/02/23 Javascript
微信小程序Redux绑定实例详解
2017/06/07 Javascript
Vue实例中生命周期created和mounted的区别详解
2017/08/25 Javascript
node puppeteer(headless chrome)实现网站登录
2018/05/09 Javascript
Nodejs核心模块之net和http的使用详解
2019/04/02 NodeJs
ES6 Proxy实现Vue的变化检测问题
2019/06/11 Javascript
JS实现扫码枪扫描二维码功能
2020/01/03 Javascript
[25:45]2018DOTA2亚洲邀请赛4.5SOLO赛 Sylar vs Paparazi
2018/04/06 DOTA
[06:45]2018DOTA2亚洲邀请赛 4.5 SOLO赛 Sccc vs Maybe
2018/04/06 DOTA
[01:47]2018年度DOTA2最佳教练-完美盛典
2018/12/16 DOTA
[08:08]DOTA2-DPC中国联赛2月28日Recap集锦
2021/03/11 DOTA
Python字符串处理函数简明总结
2015/04/13 Python
以windows service方式运行Python程序的方法
2015/06/03 Python
Python网络编程详解
2017/10/31 Python
Python实现上下班抢个顺风单脚本
2018/02/07 Python
手把手教你如何安装Pycharm(详细图文教程)
2018/11/28 Python
解决python给列表里添加字典时被最后一个覆盖的问题
2019/01/21 Python
python numpy 按行归一化的实例
2019/01/21 Python
Python3.5面向对象编程图文与实例详解
2019/04/24 Python
浅谈html5标签css3的常用样式
2016/10/20 HTML / CSS
养殖人员的创业计划书范文
2013/12/26 职场文书
2016年小学端午节活动总结
2016/04/01 职场文书