springcloud 微服务网关
zuul.strip-prefix=true
zuul.routes.postgres-client.path =/ps01/**
zuul.routes.postgres-client.service-id=postgres-client
zuul.routes.postgres-client02.path=/ps02/**
zuul.routes.postgres-client02.service-id=postgres-client02
zuul.semaphore.max-semaphores=100
zuul.ribbon-isolation-strategy=semaphore
@Component
public class MyRateLimiter extends ZuulFilter {
// 令牌桶
static RateLimiter rateLimiter
= RateLimiter.create(8, 2, TimeUnit.SECONDS);
@Override
public String filterType() {
return PRE_TYPE;
}
@Override
public int filterOrder() {
return SERVLET_DETECTION_FILTER_ORDER - 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext context = RequestContext.getCurrentContext();
boolean allowed = rateLimiter.tryAcquire();
if (!allowed) {
context.setSendZuulResponse(false);
context.setResponseStatusCode(501);
}
return null;
}
}
ribbon.eureka.enabled=true
ribbon.ReadTimeout = 10000
ribbon.ConnectTimeout= 10000
ribbon.MaxAutoRetries= 0
ribbon.MaxAutoRetriesNextServer=1
ribbon.OkToRetryOnAllOperations=false
spring.cloud.sentinel.transport.port=8719
spring.cloud.sentinel.transport.dashboard=192.168.1.4:8080
nacos-gateway.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.routes[0].id=order-servicespring.cloud.gateway.routes[0].predicates[0]=Path=/order/**
spring.cloud.gateway.routes[0].uri=lb://order-service
spring.cloud.gateway.redis-rate-limiter.include-headers=true
spring.cloud.gateway.routes[0].filters[0]=RewritePath=/order(?<segment>/?.*), $\{segment}
spring.cloud.gateway.routes[0].filters[0].name=RequestRateLimiter
# 如果返回的key是空的话,则不进行限流
spring.cloud.gateway.routes[0].filters[0].args[0]=deny-empty-key=false
# 每秒产生多少个令牌
spring.cloud.gateway.routes[0].filters[0].args[1]=redis-rate-limiter.replenishRate=0
# 1秒内最大的令牌,即在1s内可以允许的突发流程,设置为0,表示阻止所有的请求
spring.cloud.gateway.routes[0].filters[0].args[2]=redis-rate-limiter.burstCapacity=0
# 每次请求申请几个令牌
spring.cloud.gateway.routes[0].filters[0].args[3]=redis-rate-limiter.requestedTokens=0
# 自定义限流规则
spring.cloud.gateway.routes[0].filters[0].args[0]=rate-limiter="#{@defaultGatewayRateLimiter}"
# 返回限流的key
spring.cloud.gateway.routes[0].filters[0].args[1]=key-resolver="#{@defaultGatewayKeyResolver}"
spring.cloud.gateway.routes[0].filters[0].args[2]=deny-empty-key=false
# 限流后向客户端返回的响应码429,请求太多
spring.cloud.gateway.routes[0].filters[0].args[3]=status-code=TOO_MANY_REQUESTS
# 每次请求申请几个令牌 default-gateway-rate-limiter 的值是在 defaultGatewayRateLimiter 中定义的
spring.cloud.gateway.routes[0].filters[0].args[4]=default-gateway-rate-limiter.requestedTokens=1
1.使用hystrix
@HystrixCommand(
commandKey = "saveStudent",
commandProperties = {
@HystrixProperty(name="execution.isolation.strategy",value = "THREAD")
},
threadPoolKey = "saveStudent",
threadPoolProperties = {
@HystrixProperty(name= CORE_SIZE,value = "3"),
@HystrixProperty(name= MAX_QUEUE_SIZE,value = "5"),
@HystrixProperty(name= QUEUE_SIZE_REJECTION_THRESHOLD,value = "7"),
@HystrixProperty(name = CIRCUIT_BREAKER_ENABLED, value = "true"),// 是否开启断路器
@HystrixProperty(name = CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "10"),// 请求次数
@HystrixProperty(name = CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value = "10000"), // 时间窗口期
@HystrixProperty(name = CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "60"),
@HystrixProperty(name = EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "4000"),
//统计滚动窗口的持续时间,以毫秒为单位。
// 该时间用于断路器判断健康度时需要收集信息的持续时间,断路器在收集指标信息的时候会根据设置的时间窗长度拆分成多个“桶”来累计各个度量值,每个“桶”记录一段时间内的采集指标。)
@HystrixProperty(name = METRICS_ROLLING_STATS_TIME_IN_MILLISECONDS, value = "10000")
},
fallbackMethod = "saveStudent"
)
feign.hystrix.enabled=true
hystrix.threadpool.default.coreSize=100 #并发执行的最大线程数,默认10
hystrix.threadpool.default.maxQueueSize=5 #BlockingQueue的最大队列数,当设为-1,会使用 SynchronousQueue,值为正时使用LinkedBlcokingQueue。该设置只会在初始化时有效,之后不能修改
hystrix.threadpool.default.queueSizeRejectionThreshold=1 #即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝。因为maxQueueSize不能被动态修改,这个参数将允许我们动态设置该值。if maxQueueSize == ?1,该字段将不起作用
hystrix.command.default.execution.isolation.strategy=SEMAPHORE #隔离策略,默认是Thread, 可选Thread|Semaphor
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000 #命令执行超时时间,默认1000ms
hystrix.command.default.execution.timeout.enabled=true #执行是否启用超时,默认启用true
hystrix.command.default.execution.isolation.strategy.semaphore.max-semaphores=100
ribbon 的负载均衡测罗
service-B.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
ribbon.eureka.enabled=true
ribbon.ReadTimeout = 10000
ribbon.ConnectTimeout= 10000
ribbon.MaxAutoRetries= 0
ribbon.MaxAutoRetriesNextServer=1
ribbon.OkToRetryOnAllOperations=false
public class GlobalRule implements IRule {
private ILoadBalancer iLoadBalancer;
@Override
public Server choose(Object o) {
List<Server> servers = iLoadBalancer.getAllServers();
return servers.get(0);
}
@Override
public void setLoadBalancer(ILoadBalancer iLoadBalancer) {
this.iLoadBalancer = iLoadBalancer;
}
@Override
public ILoadBalancer getLoadBalancer() {
return this.iLoadBalancer;
}
}
sentinel 限流熔断
sentinel 配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-apollo</artifactId>
<version>1.8.0</version>
</dependency>
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*
spring.cloud.sentinel.transport.port=8719
spring.cloud.sentinel.transport.dashboard=localhost:8480
feign.sentinel.enabled=true
#spring.cloud.sentinel.datasource.ds.file.file=classpath:flowrule.json
#spring.cloud.sentinel.datasource.ds.file.data-type=json
#spring.cloud.sentinel.datasource.ds2.file.rule-type=flow
spring.cloud.sentinel.datasource.ds2.apollo.namespace-name=application
spring.cloud.sentinel.datasource.ds2.apollo.flow-rules-key=flowRules
spring.cloud.sentinel.datasource.ds2.apollo.default-flow-rule-value=[]
spring.cloud.sentinel.datasource.ds2.apollo.rule-type=flow
@SentinelResource(value = "sayHello",blockHandler ="handleException",blockHandlerClass = ExceptionUtil.class)
Field 说明 默认值 resource 资源名,资源名是限流规则的作用对象 count 限流阈值 grade 限流阈值类型,QPS 或线程数模式 QPS 模式 limitApp 流控针对的调用来源 default,代表不区分调用来源 strategy 判断的根据是资源自身,还是根据其它关联资源 (refResource),还是根据链路入口 根据资源本身 controlBehavior 流控效果(直接拒绝 / 排队等待 / 慢启动模式) 直接拒绝 在Spring Cloud Sentinel中,降级(degrade)是指当资源被大量调用,且每个调用都执行了较长时间,为了保护系统不被打垮,Sentinel会对资源进行降级处理,即停止该资源的访问。降级规则可以通过以下参数进行配置: Field 说明 默认值 grade:降级规则的级别,分为0(允许),1(警告),2(错误)。 count:触发降级的最小请求数。 timeWindow:时间窗口,单位为毫秒,请求次数在这个时间窗口内达到阈值则触发降级。 minRequestAmount:降级后每分钟允许的最小请求数,用于自动恢复服务。 slowRatioThreshold:慢请求比例阈值,当慢请求比例超过这个值就会进行降级。 statIntervalMs:统计时间窗口,用于计算慢请求比例,单位为毫秒。
flow 规则
[
{
"resource": "sayHello",
"controlBehavior": 0,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0
}
]
degrade.json
[{
"resource": "sayHello",
"count": 100.0,
"timeWindow": 10,
"grade": 1,
"countType": 1
}]
zuul 源码解析
ribbon源码解析
zuul 的四种filterType: pre post error route
-
动态路由:route
-
流量控制和负载均衡,权限验证:pre
-
请求日志记录:post
-
异常处理:error
hystrix
gateway 源码解析
sentinel 源码解析
遇到问题解决方式
springcloud 2020.0.3重大改变
当前组件 替代品
Hystrix Resilience4j
Hystrix Dashboard / Turbine Micrometer + Monitoring System
Ribbon Spring Cloud Loadbalancer
Zuul 1 Spring Cloud Gateway
Archaius 1 Spring Boot external config + Spring Cloud Config
以下模块已从spring-cloud-netflix中删除:
spring-cloud-netflix-archaius
spring-cloud-netflix-concurrency-limits
spring-cloud-netflix-core
spring-cloud-netflix-dependencies
spring-cloud-netflix-hystrix
spring-cloud-netflix-hystrix-contract
spring-cloud-netflix-hystrix-dashboard
spring-cloud-netflix-hystrix-stream
spring-cloud-netflix-ribbon
spring-cloud-netflix-sidecar
spring-cloud-netflix-turbine
spring-cloud-netflix-turbine-stream
spring-cloud-netflix-zuul
spring-cloud-starter-netflix-archaius
spring-cloud-starter-netflix-hystrix
spring-cloud-starter-netflix-hystrix-dashboard
spring-cloud-starter-netflix-ribbon
spring-cloud-starter-netflix-turbine
spring-cloud-starter-netflix-turbine-stream
spring-cloud-starter-netflix-zuul
Support for ribbon, hystrix and zuul was removed across the release train projects.
获取接口start.spring.io/actuator/info
maven地址Maven Repository: org.mybatis.spring.boot » mybatis-spring-boot-starter » 2.2.2 (mvnrepository.com)
https://start.aliyun.com/
参考文献
Sentinel 流量控制限流框架详解_限流框架sentinel-CSDN博客
在生产环境中使用 Sentinel · alibaba/Sentinel Wiki · GitHub