grpc斷路器之sentinel

背景

爲了防止下游服務雪崩,這裏考慮使用斷路器git

技術選型

因爲是springboot服務且集成了istio,這裏考慮三種方案github

  • istio
  • hystrix
  • sentinel

這裏分別有這幾種方案的對比spring

微服務斷路器模式實現:Istio vs Hystrixspringboot

Sentinel 與 Hystrix 的對比微服務

首先考慮的是istio,可是在使用istio進行熔斷、分流時,流量不穩定,而且返回狀態以及數據達不到預取效果,後面考慮到sentinel自動集成了grpc,因此這裏使用sentinel。ui

步驟

一、增長相關依賴

<dependencies>
  <dependency>
           <groupid>com.alibaba.cloud</groupid>
           <artifactid>spring-cloud-starter-alibaba-sentinel</artifactid>
       </dependency>
       <dependency>
           <groupid>com.alibaba.csp</groupid>
           <artifactid>sentinel-grpc-adapter</artifactid>
           <version>1.7.1</version>
       </dependency>
   </dependencies>

<dependencymanagement>
       <dependencies>
           <dependency>
               <groupid>com.alibaba.cloud</groupid>
               <artifactid>spring-cloud-alibaba-dependencies</artifactid>
               <version>2.0.1.RELEASE</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
          </dependencies>
   </dependencymanagement>

二、增長配置類

方案一:增長全局攔截器code

/**
 * @Auther: lipeng
 * @Date: 2020/3/23 17:24
 * @Description:
 */
@Order(Ordered.LOWEST_PRECEDENCE)
@Configuration
@Slf4j
public class SentinelConfig {
    @Bean
    public ClientInterceptor sentinelGrpcClientInterceptor(){
        return new SentinelGrpcClientInterceptor();
    }
    @Bean
    public GlobalClientInterceptorConfigurer globalInterceptorConfigurerAdapter(ClientInterceptor sentinelGrpcClientInterceptor) {
        return registry -&gt; registry.addClientInterceptors(sentinelGrpcClientInterceptor);
    }
}

方案二:調用時增長攔截器 固然這裏也能夠換一種方式增長攔截器,以下,在啓動類中增長server

@Bean
    public ClientInterceptor sentinelGrpcClientInterceptor(){
        return new SentinelGrpcClientInterceptor();
    }

而後在調用類中增長依賴blog

@Autowired
    private ClientInterceptor sentinelGrpcClientInterceptor;

   public void doXXX(){
         XXXGrpc.XXXBlockingStub stub = XXXGrpc.newBlockingStub(serverChannel).withInterceptors(sentinelGrpcClientInterceptor);
         XXXApi.XXXResponse response = stub.withDeadlineAfter(grpcTimeout, TimeUnit.MILLISECONDS).doXXX(request);

}

三、增長sentinel dashboard配置

spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:7080

四、部署sentinel dashboard

部署參考下面連接:圖片

Sentinel 控制檯

啓動後訪問下相關連接,grpc自動集成上去了,效果以下:

在這裏插入圖片描述 在這裏插入圖片描述

詳細配置能夠參考官方wiki:https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8

相關文章
相關標籤/搜索