用asp做网站的流程,岳阳网站开发公司推荐,h5商城模板,企业所得税怎么算2023年文章目录 Sentinel介绍Spring Cloud Gateway集成Sentinelpom依赖Sentinel配置Sentinel集成Nacos作为数据源自定义降级响应 Sentinel介绍
随着微服务的流行#xff0c;服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件主要以流量为切入点从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。 — 摘自官网
Spring Cloud Gateway集成Sentinel
pom依赖
添加spring cloud gateway sentinel的starter依赖。
!-- SpringCloud Alibaba Sentinel Gateway --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-sentinel-gateway/artifactId
/dependencySentinel配置
引入starter依赖后只需要一些配置就可以实现网关对于后端API的流控降级等功能具体的实现starter提供实现Spring Cloud Gateway可以让spring-cloud-alibaba-sentinel-gateway中的自动化配置生效配置前缀前缀为spring.cloud.sentinel.filter.scg。
Spring Cloud Alibaba Sentinel 提供了这些配置选项
配置项含义默认值spring.cloud.sentinel.enabledSentinel自动化配置是否生效truespring.cloud.sentinel.eager是否提前触发 Sentinel 初始化falsespring.cloud.sentinel.transport.port应用与Sentinel控制台交互的端口应用本地会起一个该端口占用的HttpServer可以不配置8719spring.cloud.sentinel.transport.dashboardSentinel 控制台地址可以配置host:port,这样就不用单独配置portspring.cloud.sentinel.transport.heartbeat-interval-ms应用与Sentinel控制台的心跳间隔时间spring.cloud.sentinel.transport.client-ip此配置的客户端IP将被注册到 Sentinel Server 端spring.cloud.sentinel.filter.orderServlet Filter的加载顺序。Starter内部会构造这个filterInteger.MIN_VALUEspring.cloud.sentinel.filter.url-patterns数据类型是数组。表示Servlet Filter的url pattern集合/*spring.cloud.sentinel.filter.enabledEnable to instance CommonFiltertruespring.cloud.sentinel.metric.charsetmetric文件字符集UTF-8spring.cloud.sentinel.metric.file-single-sizeSentinel metric 单个文件的大小spring.cloud.sentinel.metric.file-total-countSentinel metric 总文件数量spring.cloud.sentinel.log.dirSentinel 日志文件所在的目录spring.cloud.sentinel.log.switch-pidSentinel 日志文件名是否需要带上pidfalsespring.cloud.sentinel.servlet.block-page自定义的跳转 URL当请求被限流时会自动跳转至设定好的 URLspring.cloud.sentinel.flow.cold-factorhttps://github.com/alibaba/Sentinel/wiki/%E9%99%90%E6%B5%81— %E5%86%B7%E5%90%AF%E5%8A%A8[冷启动因子]3spring.cloud.sentinel.zuul.order.preSentinelZuulPreFilter 的 order10000spring.cloud.sentinel.zuul.order.postSentinelZuulPostFilter 的 order1000spring.cloud.sentinel.zuul.order.errorSentinelZuulErrorFilter 的 order-1spring.cloud.sentinel.scg.fallback.modeSpring Cloud Gateway 熔断后的响应模式(选择 redirect or response)spring.cloud.sentinel.scg.fallback.redirectSpring Cloud Gateway 响应模式为 ‘redirect’ 模式对应的重定向 URLspring.cloud.sentinel.scg.fallback.response-bodySpring Cloud Gateway 响应模式为 ‘response’ 模式对应的响应内容spring.cloud.sentinel.scg.fallback.response-statusSpring Cloud Gateway 响应模式为 ‘response’ 模式对应的响应码429spring.cloud.sentinel.scg.fallback.content-typeSpring Cloud Gateway 响应模式为 ‘response’ 模式对应的 content-typeapplication/json 请注意。这些配置只有在 Servlet 环境下才会生效RestTemplate 和 Feign 针对这些配置都无法生效 表格中的配置看似很多实际上应用的并不多有些配置使用默认值即可根据实际需求配置响应参数即可这里我给出一个我本地的一个配置示例配置直接使用可能有问题因为我这里只摘取了spring.cloud.gateway.sentinel的相关配置仅供参考。
spring:application:name: ruuby-gatewayprofiles:active: devcloud:gateway:routes:- id: account-svcuri: lb://account-svcpredicates:- Path/gateway/account/**filters:- StripPrefix1sentinel:# 取消控制台懒加载eager: truetransport:# 控制台地址dashboard: 127.0.0.1:8080filter:enabled: true# 配置默认fallback也可以编码自定义fallback scg.fallback:mode: responseresponse-status: 444response-body: 1234scg:order: -100 配置完成后启动网关这时候可以在Sentinel控制台中看到gateway已经注册到了Sentinel控制台但是没有任何资源这时候需要手动创建资源资源就是Sentinel系统保护的一个单元。在Spring Cloud Gateway中配置的资源可以是url也可以是转发服务的服务id也就是服务发现的service如上配置中的account-svc所以我们在Sentinel控制台就可以创建熔断或限流规则我这里创建了一个流控规则API名称就是account-svcAPI类型是Route ID阈值类型是QPS单机的阈值是0如下图
这就意味着一个请求都不会转发到account-svc服务的请求因为阈值设置为0请求结果如下 account-svc是一个使用Spring Cloud Alibaba开发脚手架开发的一个业务服务后面在讲到服务发现注册的时候会把该服务的一些代码写出来也可以参考GitHub。
Sentinel集成Nacos作为数据源
在生产系统中我们往往不会对接Sentinel的控制台而且想在服务启动时就已经把Sentinel控制的资源配置好这个时候我们可以将Sentinel控制的数据源配置起来这里就以Nacos为例将Sentinel的资源配置通过Nacos配置中心管理。
spring-cloud-alibaba-sentinel-gatewaystarter中已经提供了Sentinel DataSource的相关依赖我们在使用时只需要配置即可。配置内容如下
ruuby-gateway-dev.yml配置
server:port: 8081spring:application:name: ruuby-gatewayprofiles:active: devcloud:nacos:username: nacospassword: nacosdiscovery:# 服务注册中心地址server-addr: 127.0.0.1:8848# 阿里云平台aksk# access-key:# secret-key:namespace: 3ef5e608-6ee8-4881-8e50-ed47a5a04af2config:server-addr: 127.0.0.1:8848# 阿里云平台aksk# access-key:# secret-key:# 配置文件格式file-extension: ymlshared-configs:- ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}namespace: 3ef5e608-6ee8-4881-8e50-ed47a5a04af2group: DEFAULT-GROUPgateway:routes:- id: account-svcuri: lb://account-svcpredicates:- Path/gateway/account/**filters:- StripPrefix1sentinel:# 取消控制台懒加载eager: truetransport:# 控制台地址dashboard: 127.0.0.1:8080# nacos配置持久化datasource:ds1:nacos:server-addr: 127.0.0.1:8848dataId: ${spring.application.name}-sentinel-${spring.profiles.active}.jsonnamespace: 3ef5e608-6ee8-4881-8e50-ed47a5a04af2groupId: DEFAULT_GROUPdata-type: jsonrule-type: gw-flowfilter:enabled: true# 配置默认fallback也可以编码自定义fallback scg.fallback:mode: responseresponse-status: 444response-body: 1234scg:order: -100 spring.cloud.sentinel.datasource配置制定数据源数据源可以是多个这里使用的是nacos配置的Sentinel规则的配置文件是${spring.application.name}-sentinel-${spring.profiles.active}.json也就是ruuby-gateway-sentinel-dev.json配置内容格式为json该配置内容如下
[{resource: account-svc,count: 5,grade: 0,limitApp: default}
] 启动网关这时可以在Sentinel控制台上看到我们在json文件中配置好的流控规则如下图 这时如果在Nacos修改参数会同步到Sentinel控制台但是从Sentinel控制台修改参数不会同步到Nacos配置中心所以生产上如果使用Nacos作为Sentinel数据源的话建议从Nacos上修改Sentinel资源参数。
自定义降级响应
通过上面的实验我们可以知道通过配置spring.cloud.gateway.sentinel.filter.scg.fallback可以实现服务降级后的返回Spring Cloud Gateway Sentinel也提供了接口让开发人员实现自定义的服务降级响应只要实现BlockRequestHandler即可代码如下
Slf4j
public class SentinelFallbackHandler implements BlockRequestHandler {Overridepublic MonoServerResponse handleRequest(ServerWebExchange serverWebExchange,Throwable throwable) {log.info(LogUtil.marker(), SCG Sentinel blocked!);return ServerResponse.status(444).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(SCG Sentinel blocked!));}
} 自定义降级配置代码如下
Configuration
public class GatewayConfiguration {BeanOrder(Ordered.HIGHEST_PRECEDENCE)public SentinelFallbackHandler sentinelFallbackHandler() {return new SentinelFallbackHandler();}
}注使用自定义降级时不能配置spring.cloud.gateway.sentinel.filter.scg.fallback配置的优先级是大于自定义实现的 测试结果如下