- SpringCloud02-Feign、Hystix 和 Gateway
- 3.Feign 声明式服务调用
- 3.1 Feign 声明式服务调用
- 3.2 feign 超时设置
- 3.3 Feign 日志记录
- 4.Hystrix 熔断器
- 4.1 Hystix降级
- 1.服务提供方降级
- 2.服务消费方降级
- 4.2 Hystrix熔断
- 4.3 Hystrix熔断监控
- 5.Gateway 网关
- 5.1 Gateway 网关快速入门
- 5.2 Gateway 路由配置
- 5.3 Gateway 过滤器
Feign是一个声明式的REST客户端,它用了基于接口的注解方式,很方便实现客户端配置。
作用在消费端
3.1 Feign 声明式服务调用使用步骤:
- 在消费端引入open-feign依赖
- 编写Feign调用接口
- 在启动类添加 @EnableFeignClients注解,开启Feign功能。
依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-openfeign
调用接口
@FeignClient(value = "EUREKA-PROVIDER")
public interface GoodsFeignClient {
@GetMapping("/goods/goods/{id}")
public Goods findGoodsById(@PathVariable("id") Integer id);
}
@Resource
private GoodsFeignClient goodsFeignClient;
@GetMapping("/goods3/{id}")
public Goods findGoodsById3(@PathVariable("id") Integer id){
Goods goods = goodsFeignClient.findGoodsById(id);
return goods;
}
3.2 feign 超时设置
Feign底层依赖于Ribbon实现负载均衡和远程调用,所以一般配置Ribbon的超时,但Feign实际也有自己的超时配置。
Ribbon默认超时一秒。超时报错。
调用方配置
# 设置Ribbon的超时时间 ribbon: ConnentTimeOut: 1000 #连接超时时间 默认1s ReadTimeOut: 3000 #逻辑处理的超时时间 默认1s3.3 Feign 日志记录
调用的数据包监控。
1.Feign只能记录debug级别的日志信息
# 设置当前的日志级别为debug
logging:
level:
com.li: debug
2.定义Feign日志级别Bean
@Configuration
public class FeignLogConfig {
@Bean
public Logger.Level level(){
return Logger.Level.FULL;
}
}
3.启用该Bean
@FeignClient(value = "EUREKA-PROVIDER",configuration = FeignLogConfig.class)4.Hystrix 熔断器
Hystix是Netflix开源的一个延迟和容错库,用于隔离访问远程服务、第三方库、防止出现级联失败(雪崩)。
雪崩:一个服务失败,导致整条链路的服务都失败的情形。
Hystrix主要功能 :
-
隔离:
线程池隔离:如果服务a调用服务时,服务挂掉,则可能a会有更多线程去调用服务,但都无法释放导致线程池可用线程资源耗尽,其他服务则不能正常调用,所以需要线程池隔离。默认为线程池隔离。
信号量隔离:添加访问次数,超过次数可以拒绝提供服务。
-
降级 :提供方和消费方都需要降级,异常,超时。给提示。
-
熔断 :
-
限流
当服务发生异常或调用超时,返回默认数据。
1.服务提供方降级- 引入hystrix依赖
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
- 定义降级方法
public Goods findGoods_fallback(@PathVariable("id") Integer id){
Goods goods = new Goods();
goods.setName("降级了~~~");
return goods;
}
- 使用@HystrixCommand注解配置降级方法
@GetMapping("/goods/{id}")
@HystrixCommand(fallbackMethod = "findGoods_fallback",commandProperties = {
//超时三秒降级
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
public Goods findGoodsById(@PathVariable("id") Integer id){
int i = 1/0;
Goods goods = goodsService.findOne(id);
goods.setName(goods.getName()+":"+port);
return goods;
}
- 在启动类上开启Hystrix
@EnableCircuitBreaker2.服务消费方降级
消费方也可以使用服务端的写法来编写降级
由于消费方使用feign组件进行远程调用,而feign组件已经集成了hystrix组件
定义feign 调用接口实现类,复写方法,即降级方法。
@Component
public class GoodsFeignClientFallback implements GoodsFeignClient {
@Override
public Goods findGoodsById(Integer id) {
Goods goods = new Goods();
goods.setName("消费端降级~~");
return goods;
}
}
在@FeignClient注解中使用fallback属性设置降级处理类。
@FeignClient(value = "EUREKA-PROVIDER",fallback = GoodsFeignClientFallback.class)
配置开启 feign的hystrix支持,默认关闭
# 启用feign的hystrix降级
feign:
hystrix:
enabled: true
注意:当服务端和消费端都提供了降级时,由于提供方已经降级所以提供的数据就是正常数据而不是异常数据,一般为调用超时或调用异常降级,所以显示的是服务端降级。
4.2 Hystrix熔断用于监控微服务的调用情况,当失败的情况达到预定的阈值(5秒失败20次),会打开断路器,拒绝所有请求,直到服务恢复正常为止。
如果熔断时,5秒后,断路器会半开尝试,调用失败继续打开断路器,如果调用成功次数达到阈值,则关闭熔断器。
熔断器机制默认是开启的。
配置
@GetMapping("/goods/{id}")
@HystrixCommand(fallbackMethod = "findGoods_fallback",commandProperties = {
//超时三秒降级
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
//失败次数,默认20次
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "20"),
//监控时间,默认5000毫秒
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000"),
//失败率,默认50%
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50")
})
4.3 Hystrix熔断监控
Hystrix 提供了Hystrix-dashboard 功能,用于实时监控微服务运行状态。但只能监控一个微服务。
Netflix提供了Turbine,进行聚合监控。
在浏览器访问http://localhost:8769/hystrix/ 进入Hystrix Dashboard界面
界面中输入监控的Url地址 http://localhost:8769/turbine.stream,监控时间间隔2000毫秒和title,
5.Gateway 网关网关旨在为微服务架构提供一直简单而有效的统一的API路由管理方式。
在微服务架构中,不同的微服务可以有不同的网络地址,各个微服务之间通过互相调用完成用户请求,客户端可能通过调用N个微服务接口完成一个用户请求。
存在的问题:
客户端多次请求不同的微服务,增加客户端的复杂性
认证复杂,每个服务都要进行认证
http请求不同服务次数增加,性能不高
网关就是系统的入口,封装了应用程序的内部结构,为客户端提供统一服务,一些与业务本身功能无关的公共逻辑可以在这里实现,诸如认证、鉴权、监控、缓存、负载均衡、流量管控、路由转发等
在目前的网关解决方案里,有Nginx+Lua、Netflix Zuul、Spring Cloud Gateway等
5.1 Gateway 网关快速入门1.搭建网关模块
2.引入依赖
org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-gateway
3.编写启动类
4.编写配置文件
server:
port: 80
spring:
application:
name: api-gateway-server
cloud:
# 网关配置
gateway:
#路由配置:转发规则
routes: #集合
# id:唯一标识。默认是一个UUID
# uri:转发路径
# predicates:条件,用于请求网关路径的匹配规则
- id: eureka-provider
uri: http://localhost:8001/
predicates:
- Path=/goods
@Override
public int getOrder() {
return 0;
}
}



