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实现问号表达式(?)的方法
Nov 27 Python
python实现的简单窗口倒计时界面实例
May 05 Python
Python提取Linux内核源代码的目录结构实现方法
Jun 24 Python
Python实现识别手写数字大纲
Jan 29 Python
Python使用cx_Oracle模块操作Oracle数据库详解
May 07 Python
使用python实现mqtt的发布和订阅
May 05 Python
python3中类的继承以及self和super的区别详解
Jun 26 Python
python正则过滤字母、中文、数字及特殊字符方法详解
Feb 11 Python
基于Numba提高python运行效率过程解析
Mar 02 Python
使用tensorflow框架在Colab上跑通猫狗识别代码
Apr 26 Python
Python通过文本和图片生成词云图
May 21 Python
Python函数参数分类原理详解
May 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
PHP异常Parse error: syntax error, unexpected T_VAR错误解决方法
2014/05/06 PHP
PHP创建XML接口示例
2019/07/04 PHP
PHP中通过getopt解析GNU C风格命令行选项
2019/11/18 PHP
doctype后如何获得body.clientHeight的方法
2007/07/11 Javascript
一段利用WSH获取登录时间的jscript代码
2008/05/11 Javascript
一个背景云变换js特效 鼠标移动背景云变化
2012/12/28 Javascript
JS获得QQ号码的昵称,头像,生日的简单实例
2013/12/04 Javascript
JS获取客户端IP地址、MAC和主机名的7个方法汇总
2014/07/21 Javascript
JQuery中DOM事件合成用法实例分析
2015/06/13 Javascript
深入理解JavaScript编程中的原型概念
2015/06/25 Javascript
Angularjs material 实现搜索框功能
2016/03/08 Javascript
Avalon中文长字符截取、关键字符隐藏、自定义过滤器
2016/05/18 Javascript
jquery遍历table的tr获取td的值实现方法
2016/05/19 Javascript
JavaScript BASE64算法实现(完美解决中文乱码)
2017/01/10 Javascript
基于jQuery制作小图标上下滑动特效
2017/01/18 Javascript
Vue.2.0.5实现Class 与 Style 绑定的实例
2017/06/20 Javascript
bootstrap-paginator服务器端分页使用方法详解
2020/02/13 Javascript
原生JS实现pc端轮播图效果
2020/12/21 Javascript
[40:17]2018DOTA2亚洲邀请赛 4.5 淘汰赛 LGD vs Liquid 第一场
2018/04/06 DOTA
跟老齐学Python之玩转字符串(2)
2014/09/14 Python
Python实现多线程抓取妹子图
2015/08/08 Python
Python基于pandas实现json格式转换成dataframe的方法
2018/06/22 Python
彻底理解Python中的yield关键字
2019/04/01 Python
Python企业编码生成系统之系统主要函数设计详解
2019/07/26 Python
HTML5单选框、复选框、下拉菜单、文本域的实现代码
2020/12/01 HTML / CSS
北美领先的智能产品购物网站:Wellbots
2018/06/11 全球购物
美国饼干礼物和美食甜点购买网站:Cheryl’s
2020/05/28 全球购物
资深财务管理人员自我评价
2013/09/22 职场文书
货代行业个人求职简历的自我评价
2013/10/22 职场文书
大学自主招生自荐信范文
2014/02/26 职场文书
教学质量评估实施方案
2014/03/17 职场文书
售后服务承诺书范文
2014/03/26 职场文书
乔布斯斯坦福大学演讲稿
2014/05/23 职场文书
生物学专业求职信
2014/07/23 职场文书
2014年化验员工作总结
2014/11/18 职场文书
运动会观后感
2015/06/09 职场文书