客户端负载均衡Feign之二:Feign 功能介绍
一、Ribboon配置
在Spring cloud Feign中客户端负载均衡是通过Spring cloud Ribbon实现的,所以我们可以直接通过配置Ribbon客户端的方式来自定义各个服务客户端调用的参数。那么我们怎么在Spring cloud Feign中配置Ribbon呢?
全局配置
全局配置方法简单,直接用ribbon.<key>=<value>的方式设置ribbon的默认参数。如下:
#ribbon请求连接的超时时间
ribbon.ConnectTimeout=250
#请求处理的超时时间
ribbon.ReadTimeout=1000
#对所有操作请求都进行重试
ribbon.OkToRetryOnAllOperations=true
#对当前实例的重试次数
ribbon.MaxAutoRetries=1
#对下个实例的重试次数
ribbon.MaxAutoRetriesNextServer=1
指定服务配置
为了有针对性的配置,针对各个服务客户端进行个性化配置方式与使用Spring cloud Ribbon时的配置方式一样的,都采用<client>.ribbon.<key>=<value>的格式进行设置。如下:
hello-service.ribbon.ConnectTimeout=500
hello-service.ribbon.ReadTimeout=1000
二、重试机制
#对所有操作请求都进行重试
ribbon.OkToRetryOnAllOperations=false
#对当前实例的重试次数
ribbon.MaxAutoRetries=1
#对下个实例的重试次数
ribbon.MaxAutoRetriesNextServer=1
结果:
未超时(正常)时,compute服务被调用一次。
超时时,compute服务被调用3次。
三、Hystrix配置
在Spring cloud Feign中,除了引入Spring cloud Ribbon之外,还引入了服务保护与容错的工具Hystrix。默认情况下, Spring cloud Feign会为将所有Feign客户端的方法都封装到Hystrix命令中进行服务保护。
那么在Spring cloud Feign如何配置Hystrix的属性以及如何实现服务降级?
全局配置
全局配置通ribbon一样,直接使用它的默认配置前缀hystrix.command.default就可以进行设置,
在设置之前,需要确认feign.hystrix.enabled参数是否设置为false,如果为false则关闭Feign客户端的Hystrix支持。
或者使用hystrix.command.default.execution.timeout.enabled=false来关闭熔断功能。
比如设置全局的超时时间:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
详细参数列表见《服务容错保护断路器Hystrix之二:Hystrix工作流程解析》中的《2.8、关于配置》
指定服务配置
如果想局部关闭Hystrix,需要通过使用@Scope("prototype")注解为指定的客户端配置Feign.Builder实例,详细步骤如下:
package com.dxz.feign; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; import feign.Feign; @Configuration
public class DisableHystrixConfiguration { @Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}
package com.dxz.feign.remote; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import com.dxz.feign.DisableHystrixConfiguration; //@FeignClient("compute-service")
@FeignClient(name="compute-service",configuration=DisableHystrixConfiguration.class)
public interface HelloService { @RequestMapping(value="/add", method = RequestMethod.GET)
String hello(@RequestParam("a") Integer a, @RequestParam("b") Integer b, @RequestParam("sn") Integer sn); }
结果:
下面的超时,不会熔断调用fallback方法,而是等待。
四、服务降级配置
新增一个服务降级处理类:
package com.dxz.feign.remote; import org.springframework.stereotype.Service; @Service
public class HelloServiceFallback implements HelloService { @Override
public String hello(Integer a, Integer b, Integer sn) {
System.out.println("HelloServiceFallback");
return "fallback";
} }
在服务绑定接口HelloService中,通过@FeignClient注解的fallback属性来指定服务降级处理类:
package com.dxz.feign.remote; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import com.dxz.feign.DisableHystrixConfiguration; @FeignClient(name="compute-service", fallback=HelloServiceFallback.class)
//@FeignClient(name="compute-service",configuration=DisableHystrixConfiguration.class)
public interface HelloService { @RequestMapping(value="/add", method = RequestMethod.GET)
String hello(@RequestParam("a") Integer a, @RequestParam("b") Integer b, @RequestParam("sn") Integer sn); }
测试:
其他配置
请求压缩
Spring cloud Feign支持请求与响应的GZIP压缩,以减少通讯过程中的性能损耗。只需要通过下面两个参数设置,就能开启请求与响应的压缩功能:
feign.compression.request.enabled=true
feign.compression.response.enabled=true
日志配置
Spring cloud Feign在构建被@FeignClient修饰的服务客户端时,会为每一个客户端创建一个feign.Logger实例,我们可以利用该日志对象的DEBUG模式来帮助分析Feign的请求细节。
开启方式:
logging.level.<FeignClient>=<LEVEL value>
logging.level.com.dxz.feign.remote.HelloService=DEBUG
但是,只添加了如上配置,还无法实现对DEBUG日志的输出,这是由于Feign客户端默认的Logger.LEVEL对象定义为NONE级别。该级别不吉利任何Feign调用过程中的信息,所以需要调整级别,针对全局日志调整,直接在启动类里调整如下,
package com.dxz.feign; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean; import feign.Logger; @SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class ConsumerApplication { @Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
如果是局部调整,可以为日志级别增加配置类,如下:
package com.dxz.feign; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import feign.Logger; @Configuration
public class FullLogConfiguation { @Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
} }
package com.dxz.feign.remote; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import com.dxz.feign.DisableHystrixConfiguration;
import com.dxz.feign.FullLogConfiguation; //@FeignClient(name="compute-service", fallback=HelloServiceFallback.class)
//@FeignClient(name="compute-service",configuration=DisableHystrixConfiguration.class)
@FeignClient(name="compute-service", fallback=HelloServiceFallback.class, configuration=FullLogConfiguation.class)
public interface HelloService { @RequestMapping(value="/add", method = RequestMethod.GET)
String hello(@RequestParam("a") Integer a, @RequestParam("b") Integer b, @RequestParam("sn") Integer sn); }
测试结果:
客户端负载均衡Feign之二:Feign 功能介绍的更多相关文章
- 客户端负载均衡Ribbon之二:Loadbalance的源码
Load Balance负载均衡是用于解决一台机器(一个进程)无法解决所有请求而产生的一种算法. 像nginx可以使用负载均衡分配流量,ribbon为客户端提供负载均衡,dubbo服务调用里的负载均衡 ...
- gRPC负载均衡(客户端负载均衡)
前言 上篇介绍了如何使用etcd实现服务发现,本篇将基于etcd的服务发现前提下,介绍如何实现gRPC客户端负载均衡. gRPC负载均衡 gRPC官方文档提供了关于gRPC负载均衡方案Load Bal ...
- 客户端负载均衡Feign之一:申明式服务调用Feign入门示例
Spring Cloud提供了Ribbon和Feign作为客户端的负载均衡. 前面使用了Ribbon做客户端负载均衡,使用Hystrix做容错保护,这两者被作为基础工具类框架被广泛地应用在各个微服务的 ...
- Spring Cloud负载均衡:使用Feign作客户端负载均衡
有了一篇服务端负载均衡后,再来一篇客户端负载均衡,客户端负载均衡很简单,无需在zuul中做多余配置(本示例不引入zuul),只需要在客户端进行Feign引入和配置即可. 准备工作很简单,实现客户端负载 ...
- springcloud(十二):Ribbon客户端负载均衡介绍
springcloud(十二):Ribbon客户端负载均衡介绍 Ribbon简介 使用分布式微服务脚骨的应用系统,在部署的时候通常会为部分或者全部微服务搭建集群环境,通过提供多个实例来提高系统的稳定型 ...
- Spring Cloud入门教程(二):客户端负载均衡(Ribbon)
对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...
- Spring Cloud 2-Ribbon 客户端负载均衡(二)
Spring Cloud Eureka 1.Hello-Service服务端配置 pom.xml application.yml 启动两个service 2.Ribbon客户端配置 pom.xml ...
- 【SpringCloud微服务实战学习系列】客户端负载均衡Spring Cloud Ribbon
Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现.通过Spring Cloud的封装,可以让我们轻松地将面向服务的RES模板 ...
- 五、springcloud之客户端负载均衡Ribbon
一.简介 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式: 一种是ribbon+restTemplate, ...
随机推荐
- this语句的用法第一、二点
1.this是js的一个关键字,指定一个对象然后去代替他. 函数内的this和函数外的this,函数内的this指向行为发生的主体.函数外的this都指向window没有意思. 例题: functio ...
- 为移动端而设计的bootstrap的使用
一.下载 --bs ---css ----bootstrap.css ----bootstrap.min.css ---fonts ---js ----bootstrap.js ----jquery. ...
- 计算x
如果x的x次幂结果为10(参见[图1.png]),你能计算出x的近似值吗? 显然,这个值是介于2和3之间的一个数字. 请把x的值计算到小数后6位(四舍五入),并填写这个小数值. 注意:只填写一个小数, ...
- ng-repeat的用法:
-------------------------------------转载: 遍历数组: <li ng-repeat="item in array">{{it ...
- day3 zookeeper
PS:在生产的场景中,一般有两个需求:1.提供设备的注册 2.对所注册的接口进行监听.zookeeper就是提供这样的功能,它本身就是一个集群,如果存在半数以上的节点活着就能提供服务,本身就具备很高的 ...
- dive 方便的观察容器各层信息的工具
dive 是一个方便的观察容器各层信息的工具,同时也集成了容器构建命令,方便我们在构建容器 镜像的同时查询镜像各层的变动信息 安装 mac 系统,可以按照自己的系统选择安装方式 wget http ...
- python 调试之assert and logging
断言 assert assert后面跟的表达式应该是True,否则,根据程序运行的逻辑,后面的代码肯定会出错. 如果断言失败,会抛出AssertionError def foo(s): n = int ...
- 使用 FreeCAD 打开 KiCad 用于制作外壳
使用 FreeCAD 打开 KiCad 用于制作外壳 先看导入后的结果. 步骤: 安装 FreeCAD 安装 KiCad StepUp Mod Addon 重启 FreeCAD(非常重要,不重启不生效 ...
- webpack 中的 chunk 种类
webpack 将 chunk 划分为三类: 入口 chunk.入口 chunk 包含 webpack runtime 和将要加载的模块. 普通 chunk.普通 chunk 不包含 webpack ...
- 获取docker容器的ip地址
1.进入容器后执行cat /etc/hosts 会显示自己以及(– link)软连接的容器IP 2.使用命令 docker inspect --format '{{ .NetworkSettings. ...