编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

生产环境使用Sentinel(生产环境使用rancher吗)

wxchong 2024-08-16 06:10:10 开源技术 24 ℃ 0 评论

dashborad改造

支持规则配置持久化到nacos

支持规则配置从nacos获取并展示

监控数据默认保留5分钟并且存储到内存中,暂时不修改。

https://gitee.com/sirius19/sentinel-nacos.git

注意看官方文档说明:

https://github.com/alibaba/Sentinel/wiki/%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E4%BD%BF%E7%94%A8-Sentinel

springcloud项目接入sentinel

官方文档说明

https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel


实现源码:https://gitee.com/sirius19/warm-snow

warmsnow-sentinel-starter模块


项目添加依赖warmsnow-sentinel-starter并且增加以下配置
spring:
  cloud:
    sentinel:
      transport:
        dashboard: 192.168.100.141:8080

Springcloud gateway接入sentinel

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
 spring:
   cloud:
    sentinel:
      filter:
        enabled: false
      log:
        dir: /data/logs/${spring.application.name}
      scg:
        fallback:
          response-status: 500
          mode: response
          content-type: application/json
          response-body: "{ \"code\": 500,\"msg\": \"系统繁忙,请稍候!\" }"
   main:
      web-application-type: reactive
spring:
  cloud:
    nacos:
      discovery:
        username: xxx
        password: xxx
        server-addr: xxxxxx
        namespace: dev
        group: xx_GROUP
        metadata:
          hint: dev
    sentinel:
      transport:
        dashboard: 192.168.100.141:8080
      datasource:
        gateway-flowDs:
          nacos:
            server-addr: ${spring.cloud.nacos.discovery.server-addr}
            username: ${spring.cloud.nacos.discovery.username}
            password: ${spring.cloud.nacos.discovery.password}
            namespace: sentinel
            group-id: SENTINEL_GROUP
            data-id: ${spring.application.name}-gateway-flow
            rule-type: gw_flow
        gateway-api-groupDs:
          nacos:
            server-addr: ${spring.cloud.nacos.discovery.server-addr}
            username: ${spring.cloud.nacos.discovery.username}
            password: ${spring.cloud.nacos.discovery.password}
            namespace: sentinel
            group-id: SENTINEL_GROUP
            data-id: ${spring.application.name}-gateway-api
            rule-type: gw_api_group
        degradeDs:
          nacos:
            server-addr: ${spring.cloud.nacos.discovery.server-addr}
            username: ${spring.cloud.nacos.discovery.username}
            password: ${spring.cloud.nacos.discovery.password}
            namespace: sentinel
            group-id: SENTINEL_GROUP
            data-id: ${spring.application.name}-degrade-rules
            rule-type: degrade
// 处理网关经过nginx等代理之后获取ip
@Slf4j
@Configuration
public class SentinelConfig {
    @Bean
    @Primary
    public SentinelGatewayFilter sentinelGatewayFilter() {
        log.info(
                "[Sentinel SpringCloudGateway] register SentinelGatewayFilter with order: {}",
                Ordered.HIGHEST_PRECEDENCE);
        ConfigurableRequestItemParser<ServerWebExchange> parser = new ConfigurableRequestItemParser<>(new ServerWebExchangeItemParser());

        parser.addRemoteAddressExtractor(exchange -> HttpUtils.getIpAddress(exchange.getRequest()));
        return new SentinelGatewayFilter(Ordered.HIGHEST_PRECEDENCE, parser);
    }
}

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表