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 相关文章推荐
flask中使用SQLAlchemy进行辅助开发的代码
Feb 10 Python
python实现目录树生成示例
Mar 28 Python
wxPython学习之主框架实例
Sep 28 Python
在Python3中使用asyncio库进行快速数据抓取的教程
Apr 02 Python
深入理解Python中range和xrange的区别
Nov 26 Python
Python生成器generator用法示例
Aug 10 Python
在Python文件中指定Python解释器的方法
Feb 18 Python
ZABBIX3.2使用python脚本实现监控报表的方法
Jul 02 Python
numpy.meshgrid()理解(小结)
Aug 01 Python
Python一行代码解决矩阵旋转的问题
Nov 30 Python
在spyder IPython console中,运行代码加入参数的实例
Apr 20 Python
Appium中scroll和drag_and_drop根据元素位置滑动
Feb 15 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
用Zend Encode编写开发PHP程序
2006/10/09 PHP
php中计算时间差的几种方法
2009/12/31 PHP
php cookie使用方法学习笔记分享
2013/11/07 PHP
PHP中source #N问题的解决方法
2014/01/27 PHP
PHP实现删除非站内外部链接实例代码
2014/06/17 PHP
Symfony控制层深入详解
2016/03/17 PHP
php微信公众号开发之音乐信息
2018/10/20 PHP
设定php简写功能的方法
2019/11/28 PHP
使用闭包对setTimeout进行简单封装避免出错
2013/07/10 Javascript
node.js操作mongoDB数据库示例分享
2014/11/26 Javascript
jQuery判断对象是否存在的方法
2015/02/05 Javascript
js实现的Easy Tabs选项卡用法实例
2015/09/06 Javascript
JQuery fileupload插件实现文件上传功能
2016/03/18 Javascript
微信小程序 WXML、WXSS 和JS介绍及详解
2016/10/08 Javascript
原生js获取浏览器窗口及元素宽高常用方法集合
2017/01/18 Javascript
了解VUE的render函数的使用
2017/06/08 Javascript
浅谈Vue SSR 的 Cookies 问题
2017/11/20 Javascript
javascript动态创建对象的属性详解
2018/11/07 Javascript
vue遍历对象中的数组取值示例
2019/11/07 Javascript
使用Vue实现简单计算器
2020/02/25 Javascript
[52:52]DOTA2上海特级锦标赛C组资格赛#1 OG VS LGD第三局
2016/02/27 DOTA
CentOS7安装Python3的教程详解
2019/04/10 Python
使用Python实现跳一跳自动跳跃功能
2019/07/10 Python
Python学习笔记之Zip和Enumerate用法实例分析
2019/08/14 Python
python基于三阶贝塞尔曲线的数据平滑算法
2019/12/27 Python
关于python 跨域处理方式详解
2020/03/28 Python
sublime3之内网安装python插件Anaconda的流程
2020/11/10 Python
python中字符串的编码与解码详析
2020/12/03 Python
pycharm配置安装autopep8自动规范代码的实现
2021/03/02 Python
美国室内和室外装饰花盆购物网站:ePlanters
2019/03/22 全球购物
美国环保妈妈、儿童和婴儿用品购物网站:The Tot
2019/11/24 全球购物
数据库设计的包括哪两种,请分别进行说明
2016/07/15 面试题
.net C#面试题
2012/08/28 面试题
学习型党组织心得体会
2014/09/12 职场文书
乡镇群众路线整改落实情况汇报
2014/10/28 职场文书
利用Python+OpenCV三步去除水印
2021/05/28 Python