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爬虫技巧汇总
Sep 28 Python
python如何在列表、字典中筛选数据
Mar 19 Python
使用pandas对两个dataframe进行join的实例
Jun 08 Python
Python3数据库操作包pymysql的操作方法
Jul 16 Python
python数据结构之线性表的顺序存储结构
Sep 28 Python
Python os.rename() 重命名目录和文件的示例
Oct 25 Python
对pandas读取中文unicode的csv和添加行标题的方法详解
Dec 12 Python
通过PHP与Python代码对比的语法差异详解
Jul 10 Python
python获取Pandas列名的几种方法
Aug 07 Python
Tensorflow tf.nn.depthwise_conv2d如何实现深度卷积的
Apr 20 Python
Python中如何引入第三方模块
May 27 Python
Python常用类型转换实现代码实例
Jul 28 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
Ajax PHP 边学边练 之三 数据库
2009/11/26 PHP
destoon整合UCenter图文教程
2014/06/21 PHP
PHP实现Soap通讯的方法
2014/11/03 PHP
PHP中的随机性 你觉得自己幸运吗?
2016/01/22 PHP
Yii2中YiiBase自动加载类、引用文件方法分析(autoload)
2016/07/25 PHP
11个用于提高排版水平的基于jquery的文字效果插件
2012/09/14 Javascript
jQuery实现点击文本框弹出热门标签的提示效果
2013/11/17 Javascript
Javascript Objects详解
2014/09/04 Javascript
JS实现仿京东淘宝竖排二级导航
2014/12/08 Javascript
Bootstrap实现默认导航栏效果
2020/09/21 Javascript
探究Javascript模板引擎mustache.js使用方法
2016/01/26 Javascript
浅谈JavaScript前端开发的MVC结构与MVVM结构
2016/06/03 Javascript
jQuery悬停文字提示框插件jquery.tooltipster.js用法示例【附demo源码下载】
2016/07/19 Javascript
js控住DOM实现发布微博效果
2016/08/30 Javascript
javascript十六进制数字和ASCII字符之间的转换方法
2016/12/27 Javascript
js 判断当前时间是否处于某个一个时间段内
2019/09/19 Javascript
vue中beforeRouteLeave实现页面回退不刷新的示例代码
2019/11/01 Javascript
js+canvas实现简单扫雷小游戏
2021/01/22 Javascript
[54:19]完美世界DOTA2联赛PWL S2 Magma vs PXG 第二场 11.28
2020/12/01 DOTA
[04:40]DOTA2-DPC中国联赛1月26日Recap集锦
2021/03/11 DOTA
Python三级目录展示的实现方法
2016/09/28 Python
Python迭代和迭代器详解
2016/11/10 Python
linux环境下python中MySQLdb模块的安装方法
2017/06/16 Python
python 实现在Excel末尾增加新行
2018/05/02 Python
如何利用Python开发一个简单的猜数字游戏
2019/09/22 Python
python Event事件、进程池与线程池、协程解析
2019/10/25 Python
python关于变量名的基础知识点
2020/03/03 Python
解决Jupyter NoteBook输出的图表太小看不清问题
2020/04/16 Python
一级方程式赛车官方网上商店:F1 Store(支持中文)
2018/01/12 全球购物
【魔兽争霸3重制版】原版画面与淬火MOD画面对比
2021/03/26 魔兽争霸
办理暂住证介绍信
2014/01/11 职场文书
调研座谈会发言材料
2014/08/23 职场文书
漂亮妈妈观后感
2015/06/08 职场文书
2016年党校科级干部培训班学习心得体会
2016/01/06 职场文书
MySQL系列之十二 备份与恢复
2021/07/02 MySQL
详解Spring Security中的HttpBasic登录验证模式
2022/03/17 Java/Android