以前咱們zuul 網關服務使用的接入方式是按照 Sentinel 方式接入方式。其實在Spring Cloud alibaba 體系裏面 有這個很是好用的Sentinel starter 依賴。只須要依賴一個jar 包。而後配置好Sentinel 服務器地址。java
以前的篇章有講過怎麼搭建。此次在貼一次。直接下載官網已經打好的jar包git
release地址 https://github.com/alibaba/Sentinel/releasesgithub
源碼編譯
git clone https://github.com/alibaba/Sentinel.gitweb
而後進入目錄執行 mvn clean packagespring
命令啓動docker
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
若是須要docker 的話 可編寫 docker Dockerfilebootstrap
# 基於哪一個鏡像 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 鏡像製做api
docker build --tag sentinel:1.0 .
--tag projectname:version 注意寫法bash
而後docker run 啓動鏡像。這裏做者使用的docker鏡像方式啓動
docker run -d -p8890:8080 -p8891:8080 304342c105e9服務器
而後控制檯輸入 http://localhost:9088/ 用戶名密碼 sentinel/123456
登陸參數配置:
從 Sentinel 1.6.0 起,Sentinel 控制檯引入基本的登陸功能,默認用戶名和密碼都是 sentinel。能夠參考 鑑權模塊文檔 配置用戶名和密碼。
輸入密碼登陸
登陸完成之後,咱們開始整合 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:fangjian0423@gmail.com">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
實時監控數據
簇點鏈路
在右側能夠設置流控、降級、熱點、受權操做
流控設置
對應的參數屬性
以上就是對Spring Cloud gateway 與 Sentinel 的整合方案。心細的同窗可能會想到,咱們設置的限流規則若是重啓服務都將不復存在,這樣確定是咱們不能接受的。下一篇將如何將Sentinel 設置進行存儲。
摘自參考 spring cloud 官方文檔
服務器nacos 地址 http://47.99.209.72:8848/nacos
往期地址 spring cloud alibaba 地址
Spring Cloud Alibaba (nacos 註冊中心搭建)
Spring Cloud Alibaba 使用nacos 註冊中心
Spring Cloud Alibaba nacos 配置中心使用
Spring Cloud alibaba網關 sentinel zuul 四 限流熔斷
Spring Cloud gateway 網關服務二 斷言、過濾器
Spring Cloud gateway 三 自定義過濾器GatewayFilter
如何喜歡能夠關注分享本公衆號。