开发者社区 > 云原生 > 中间件 > 正文

Sentinel结合gateway服务发现时,没有办法动态根据服务名称生成熔断器,请问要怎么做?

Sentinel结合gateway服务发现时,router使用lb:server 动态根据服务路由时,熔断器不生效,配置只能指定固定的资源,没有办法动态根据服务名称生成熔断器,请问要怎么做?

展开
收起
真的很搞笑 2024-03-04 11:57:00 37 0
3 条回答
写回答
取消 提交回答
  • 面对过去,不要迷离;面对未来,不必彷徨;活在今天,你只要把自己完全展示给别人看。

    Sentinel结合Gateway服务发现时,可以通过自定义路由规则来实现动态熔断器。具体操作如下:

    1. application.ymlapplication.properties中配置Sentinel的相关信息,例如:
    spring:
      cloud:
        sentinel:
          transport:
            dashboard: localhost:8080 # Sentinel控制台地址
            port: 8719 # Sentinel客户端与控制台通信端口
    
    1. 创建一个自定义的GatewayFilterFactory,用于生成熔断器规则。例如:
    import org.springframework.cloud.gateway.filter.GatewayFilter;
    import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
    import org.springframework.stereotype.Component;
    import reactor.core.publisher.Mono;
    
    @Component
    public class SentinelGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {
    
        private final SentinelGatewayBlockExceptionHandler exceptionHandler;
    
        public SentinelGatewayFilterFactory(SentinelGatewayBlockExceptionHandler exceptionHandler) {
            super(Object.class);
            this.exceptionHandler = exceptionHandler;
        }
    
        @Override
        public GatewayFilter apply(Object config) {
            return (exchange, chain) -> {
                String serviceName = exchange.getRequest().getURI().getHost();
                if (SphU.isClusterMode()) {
                    // 集群模式
                    try (Entry entry = SphU.entry(serviceName)) {
                        if (!entry.tryAcquire()) {
                            return exceptionHandler.handle(exchange, null);
                        }
                        return chain.filter(exchange).then(a -> {
                            entry.exit();
                            return Mono.empty();
                        });
                    }
                } else {
                    // 单实例模式
                    try (Entry entry = SphU.entry(serviceName)) {
                        if (!entry.tryAcquire()) {
                            return exceptionHandler.handle(exchange, null);
                        }
                        return chain.filter(exchange).then(a -> {
                            entry.exit();
                            return Mono.empty();
                        });
                    }
                }
            };
        }
    }
    
    1. application.ymlapplication.properties中配置自定义的GatewayFilterFactory
    spring:
      cloud:
        gateway:
          routes:
            - id: my_route
              uri: lb://my-service
              filters:
                - name: SentinelGatewayFilterFactory
                  args:
                    name: my-service
              predicates:
                - Path=/my-service/**
    

    这样,当请求匹配到my_route时,会根据服务名称my-service动态生成熔断器规则。

    2024-03-04 21:51:46
    赞同 展开评论 打赏
  • Sentinel目前对于基于服务名动态创建资源并实现熔断器功能,可以配合Spring Cloud Gateway的插件来实现。在配置中通常需要通过BlockHandlerfallback机制,并结合服务名动态生成规则,确保熔断器能随服务实例变化而生效。请参阅Sentinel官方文档关于微服务治理部分的内容。

    2024-03-04 14:48:46
    赞同 展开评论 打赏
  • 可以用最新版的正则表达式 ,此回答整理自钉群“Sentinel开源讨论群”

    2024-03-04 12:00:56
    赞同 展开评论 打赏

为企业提供高效、稳定、易扩展的中间件产品。

热门讨论

热门文章

相关电子书

更多
阿里云容器 AHAS Sentinel 网关流控揭秘 立即下载
workshop专场-微服务专场-开发者动手实践营-微服务-使用Sentinel进行微服务流量控制 立即下载
Sentinel分布式系统下的流量防卫兵 立即下载