Spring Cloud gateway 五 Sentinel整合

  • 2019 年 11 月 11 日
  • 筆記

微服務當前這麼火爆的程度,如果不能學會一種微服務框架技術。怎麼能升職加薪,增加簡歷的籌碼?spring cloud 和 Dubbo 需要單獨學習。說沒有時間?沒有精力?要學倆個框架?而Spring Cloud alibaba只需要你學會一個就會擁有倆種微服務治理框架技術。何樂而不為呢?加油吧!騷猿年

Sentinel 熔斷限流

之前我們zuul 網關服務使用的接入方式是按照 Sentinel 方式接入方式。其實在Spring Cloud alibaba 體系裡面 有這個非常好用的Sentinel starter 依賴。只需要依賴一個jar 包。然後配置好Sentinel 伺服器地址。

Sentinel 的服務搭建和啟動

Sentinel的快速搭建

之前的篇章有講過怎麼搭建。這次在貼一次。直接下載官網已經打好的jar包

release地址 https://github.com/alibaba/Sentinel/releases

源碼編譯
git clone https://github.com/alibaba/Sentinel.git

然後進入目錄執行 mvn clean package

命令啟動

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

如果需要docker 的話 可編寫 docker Dockerfile

# 基於哪個鏡像  FROM java:8  # 拷貝文件到容器,也可以直接寫成ADD microservice-discovery-eureka-0.0.1-SNAPSHOT.jar /app.jar  ADD ./*.jar app.jar  RUN mkdir -p /var/logs/Sentinel  RUN mkdir -p /var/logs/jvm  RUN mkdir -p /var/logs/dump  RUN bash -c 'touch /app.jar'  # 開放8080埠  EXPOSE 8080  # 配置容器啟動後執行的命令  ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dsentinel.dashboard.auth.username=sentinel","-Dsentinel.dashboard.auth.password=123456","-Dserver.servlet.session.timeout=7200","-XX:-PrintGCDetails","-XX:-PrintGCTimeStamps","-XX:-HeapDumpOnOutOfMemoryError","-XX:HeapDumpPath=/var/logs/dump/oom_dump.dump","-Xloggc:/var/logs/jvm/app.log","-Dfile.encoding=UTF8","-Duser.timezone=GMT+08","-XX:CMSInitiatingOccupancyFraction=90","-XX:MaxGCPauseMillis=200","-XX:StringTableSize=20000","-XX:+UseG1GC","-Xss256k","-Xmx1024m","-Xms512m","-jar","/app.jar"]  

執行 docker 鏡像製作

docker  build --tag sentinel:1.0 .

–tag projectname:version 注意寫法

file

file
然後docker run 啟動鏡像。這裡作者使用的docker鏡像方式啟動
docker run -d -p8890:8080 -p8891:8080 304342c105e9

然後控制台輸入 http://localhost:9088/ 用戶名密碼 sentinel/123456

登錄參數配置:

從 Sentinel 1.6.0 起,Sentinel 控制台引入基本的登錄功能,默認用戶名和密碼都是 sentinel。可以參考 鑒權模組文檔 配置用戶名和密碼。

  • Dsentinel.dashboard.auth.username=sentinel 用於指定控制台的登錄用戶名為 sentinel;
  • Dsentinel.dashboard.auth.password=123456 用於指定控制台的登錄密碼為 123456;如果省略這兩個參數,默認用戶和密碼均為 sentinel;
  • Dserver.servlet.session.timeout=7200 用於指定 Spring Boot 服務端 session 的過期時間,如 7200 表示 7200 秒;60m 表示 60 分鐘,默認為 30 分鐘;

輸入密碼登錄

登錄完成以後,我們開始整合 gateway的整合操作。

在我們gateway 服務中 引入pom

        <dependency>              <groupId>com.alibaba.cloud</groupId>              <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>          </dependency>                  <dependency>              <groupId>com.alibaba.cloud</groupId>              <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>          </dependency>

創建RulesController 暴露介面

package com.xian.cloud.controller;    import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;  import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;  import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;  import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;  import com.alibaba.csp.sentinel.annotation.SentinelResource;  import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;  import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;  import org.springframework.web.bind.annotation.GetMapping;  import org.springframework.web.bind.annotation.RestController;    import java.util.List;  import java.util.Set;    /**   * @author <a href="mailto:[email protected]">Jim</a>   */  @RestController  public class RulesController {        @GetMapping("/api")      @SentinelResource("api")      public Set<ApiDefinition> apiRules() {          return GatewayApiDefinitionManager.getApiDefinitions();      }        @GetMapping("/gateway")      @SentinelResource("gateway")      public Set<GatewayFlowRule> apiGateway() {          return GatewayRuleManager.getRules();      }        @GetMapping("/flow")      @SentinelResource("flow")      public List<FlowRule> apiFlow() {          return FlowRuleManager.getRules();      }  }

bootstrap.yml 文件添加制定sentinel服務地址

spring:      cloud:      sentinel:        transport:          dashboard: localhost:8890          port: 8890        # 服務啟動直接建立心跳連接        eager: true

啟動服務

多次請求 curl http://localhost:9000/api

實時監控數據

file

簇點鏈路
file

在右側可以設置流控、降級、熱點、授權操作

流控設置
file

對應的參數屬性

  • resource:資源名,即限流規則的作用對象
  • count: 限流閾值
  • grade: 限流閾值類型(QPS 或並發執行緒數)
  • limitApp: 流控針對的調用來源,若為 default 則不區分調用來源
  • strategy: 調用關係限流策略
  • controlBehavior: 流量控制效果(直接拒絕、Warm Up、勻速排隊)

總結

以上就是對Spring Cloud gateway 與 Sentinel 的整合方案。心細的同學可能會想到,我們設置的限流規則如果重啟服務都將不復存在,這樣肯定是我們不能接受的。下一篇將如何將Sentinel 設置進行存儲。

往期資料、參考資料

Sentinel 官方文檔地址

摘自參考 spring cloud 官方文檔

Spring Cloud alibaba 官網地址

示例程式碼地址

伺服器nacos 地址 http://47.99.209.72:8848/nacos

往期地址 spring cloud alibaba 地址

spring cloud alibaba 簡介

Spring Cloud Alibaba (nacos 註冊中心搭建)

Spring Cloud Alibaba 使用nacos 註冊中心

Spring Cloud Alibaba nacos 配置中心使用

spring cloud 網關服務

Spring Cloud zuul網關服務 一

Spring Cloud 網關服務 zuul 二

Spring Cloud 網關服務 zuul 三 動態路由

Spring Cloud alibaba網關 sentinel zuul 四 限流熔斷

Spring Cloud gateway 網關服務 一

Spring Cloud gateway 網關服務二 斷言、過濾器

Spring Cloud gateway 三 自定義過濾器GatewayFilter

Spring Cloud gateway 網關四 動態路由

如何喜歡可以關注分享本公眾號。
file

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。轉載請附帶公眾號二維碼