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 相关文章推荐
基于Python开发chrome插件的方法分析
Jul 07 Python
通过python改变图片特定区域的颜色详解
Jul 15 Python
使用django的objects.filter()方法匹配多个关键字的方法
Jul 18 Python
python搜索包的路径的实现方法
Jul 19 Python
django连接oracle时setting 配置方法
Aug 29 Python
python 消费 kafka 数据教程
Dec 21 Python
Python imutils 填充图片周边为黑色的实现
Jan 19 Python
Django model重写save方法及update踩坑详解
Jul 27 Python
Django rest framework分页接口实现原理解析
Aug 21 Python
提高python代码运行效率的一些建议
Sep 29 Python
Django Auth用户认证组件实现代码
Oct 13 Python
详解如何修改jupyter notebook的默认目录和默认浏览器
Jan 24 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
php jquery 实现新闻标签分类与无刷新分页
2009/12/18 PHP
基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法详解
2013/05/07 PHP
php 批量生成html,txt文件的实现代码
2013/06/26 PHP
Destoon模板制作简明教程
2014/06/20 PHP
在CentOS系统上从零开始搭建WordPress博客的全流程记录
2016/04/21 PHP
laravel 判断查询数据库返回值的例子
2019/10/11 PHP
JavaScript 设计模式 安全沙箱模式
2010/09/24 Javascript
JS弹出新窗口被拦截的解决方法
2016/08/09 Javascript
利用Angularjs中模块ui-route管理状态的方法
2016/12/27 Javascript
javascript实现简易计算器
2017/02/01 Javascript
JavaScript中清空数组的三种方式
2017/03/22 Javascript
一个简易的js图片轮播效果
2017/07/22 Javascript
php中and 和 &amp;&amp;出坑指南
2018/07/13 Javascript
微信小程序Flex布局用法深入浅出分析
2019/04/25 Javascript
详解vue-cli项目在IE浏览器打开报错解决方法
2020/12/10 Vue.js
[02:42]完美大师赛主赛事淘汰赛第三日观众采访
2017/11/25 DOTA
[00:14]护身甲盾
2019/03/06 DOTA
python统计文本字符串里单词出现频率的方法
2015/05/26 Python
python3.6+opencv3.4实现鼠标交互查看图片像素
2018/02/26 Python
使用 tf.nn.dynamic_rnn 展开时间维度方式
2020/01/21 Python
Python破解BiliBili滑块验证码的思路详解(完美避开人机识别)
2020/02/17 Python
TensorFlow的环境配置与安装教程详解(win10+GeForce GTX1060+CUDA 9.0+cuDNN7.3+tensorflow-gpu 1.12.0+python3.5.5)
2020/06/22 Python
使用TensorBoard进行超参数优化的实现
2020/07/06 Python
纯CSS3制作的简洁蓝白风格的登录模板(非IE效果更好)
2013/08/11 HTML / CSS
HTML5图片预览实例分享
2014/06/04 HTML / CSS
英国买鞋网站:Charles Clinkard
2019/11/14 全球购物
Yahoo-PHP面试题3
2012/01/14 面试题
室内设计专业学生的自我评价分享
2013/11/27 职场文书
应届毕业生个人求职自荐信
2014/01/06 职场文书
《云雀的心愿》教学反思
2014/02/25 职场文书
租房合同协议书
2014/04/09 职场文书
预备党员转正考核材料
2014/06/03 职场文书
关于Nginx中虚拟主机的一些冷门知识小结
2022/03/03 Servers
MongoDB修改oplog大小的四种方法
2022/04/11 MongoDB
JavaWeb Servlet开发注册页面实例
2022/04/11 Java/Android
Vue ECharts实现机舱座位选择展示功能
2022/05/15 Vue.js