spring-cloud-zuul 是什么?
spring-cloud-zuul 是微服务的服务网关,用来充当客户端调用服务的统一入口,至于为什么需要服务网关,已在《spring-cloud 学习遇到的困难》一文中回答
spring-cloud-zuul 可以做什么?
路由转发+过滤器(路由转发就是通过过滤器实现的)
常用网关方案:
1.Nginx+Lua
2.Kong
3.Tyk(go 语言开发)
4.spring-cloud-zuul
spring-cloud-zuul 定义了四种过滤器API
1.前置(Pre)
2.路由(Route)
3.后置(Post)
4.错误(Error)
请求生命周期
request ——> pre filters ——> route filters ——> server ——> post filters ——> response
若发生错误,则是从 发生错误的位置——> error filters——> response
spring-cloud-zuul 架构图
spring-cloud-zuul 如何使用?
1.pom.xml 文件中引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
2.启动类添加注解 @EnableZuulProxy
3.修改配置文件
spring:
cloud:
config:
discovery:
enabled: true
service-id: config
profile: dev
eureka:
client:
service-url:
defaultZone: http://localhost:8761/
4.添加详细配置
zuul:
routes:
myProduct: // 名字任意(下面部分代表的意思是将 /myProduct/** 映射至 /product/**)
path: /myProduct/** // 访问路径
serviceId: product // 映射的服务名称
sensitiveHeaders: // 敏感头设置(这里设置为空,可在 product 服务中接收到类似 cookie 等敏感的信息)
ignored-patterns: // 不处理的路径set集合
- /myProduct/msg
- /product/msg
5.实现 zuul 的动态刷新(动态路由)
@ConfigurationProperties("zuul")
@RefreshScope
public ZuulProperties auulProperties() {
return new ZuulProperties();
}
6.过滤器的使用
6.1.继承 ZuulFilter 类
6.2.重写 filterType, filterOrder, shouldFilter, run 四个方法
/**
* 过滤类型(pre, route, post, error)
* @return
*/
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
/**
* 过滤器执行顺序
* @return
*/
@Override
public int filterOrder() {
return FilterConstants.PRE_DECORATION_FILTER_ORDER - 1;
}
/**
* 是否开启过滤
* @return
*/
@Override
public boolean shouldFilter() {
return true;
}
/**
* 过滤逻辑
* @return
*/
@Override
public Object run() {
// 获取当前请求上下文
RequestContext requestContext = RequestContext.getCurrentContext();
// 过滤逻辑
return null;
}
7.跨域问题
可通过 CorsFilter 来配置跨域问题
8.服务容错
通过 Hystrix 来实现服务容错,防止雪崩效应(一个服务出错后导致所有服务瘫痪)
Hystrix 中实现服务容错的方式:
1.服务降级:优先核心服务,非核心服务不可用或弱可用
2.服务熔断
3.依赖隔离:通过 HystrixCommand 自动实现线程池隔离
4.监控
熔断器的状态:打开、关闭、半开
熔断器的运作方式:
当访问错误率到达指定值时,熔断器开启(打开状态),触发服务降级,同时打开一个休眠计时窗口,当休眠计时达到指定值时,熔断器状态转换为半开状态(将服务切回主逻辑),当下一次访问发生时,先使用主逻辑进行处理,若主逻辑还是无法正常使用,则熔断器又回到打开状态,并且休眠计时窗口开始重新计时;若这一次主逻辑恢复正常,则熔断器回到关闭状态
应用:
8.1.在 pom.xml 文件中引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
8.2.在启动类中添加注解 @EnableCircuitBreaker
8.3.在服务上配置服务降级
@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class ClientControl {
@HystrixCommand(commandProperties = {
// 超时时间(超过时间将触发服务降级)
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
})
@GetMapping("/msg")
public String msg() {
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://localhost:8080/msg", String.class);
return result;
}
@HystrixCommand(
fallbackMethod = "fallback",
commandProperties = {
// 开启服务熔断机制
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
// 设置在滚动窗口中,断路器的最小请求数
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
// 休眠时间窗口的时间(当断路器打开后,时间窗口开始计时,当达到指定时间后,断路器切换为半开状态)
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
// 断路器打开的错误最小百分比
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
}
)
@GetMapping("/msg2")
public String msg2(int number) {
if (number % 2 == 0) {
return "success";
}
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://localhost:8080/msg", String.class);
return result;
}
private String fallback(int number) {
return "服务繁忙,请稍后再试";
}
private String defaultFallback() {
return "默认:服务繁忙,请稍后再试";
}
这些配置也可通过配置文件来配置:
hystrix:
command:
default: // 这个是commandKey,不填默认为方法名,default为配置全局
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
8.4.通过 feign 来启用熔断
8.4.1.在配置文件中配置开启熔断
feign:
hystrix:
enabled: true
8.4.2.修改 feign 接口
@FeignClient(
name = "ORDER",
// 触发降级的方法逻辑
fallback = OrderFeign.OrderFeignFallback.class
)
public interface OrderFeign {
@GetMapping("/order/msg")
String getMsg();
@Component
static class OrderFeignFallback implements OrderFeign {
@Override
public String getMsg() {
return "触发降级";
}
}
}
8.5.熔断器可视化工具
8.5.1.在 pom.xml 文件中引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
8.5.2.在启动类上添加注解(@EnableHystrix,@EnableHystrixDashboard)
8.5.3.修改配置文件访问路径
management:
context-path: /
8.5.4.访问路径 http://host:port/hystrix