Spring Cloud gateway 五 Sentinel整合

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

Sentinel 熔斷限流

以前咱們zuul 網關服務使用的接入方式是按照 Sentinel 方式接入方式。其實在Spring Cloud alibaba 體系裏面 有這個很是好用的Sentinel starter 依賴。只須要依賴一個jar 包。而後配置好Sentinel 服務器地址。java

Sentinel 的服務搭建和啓動

Sentinel的快速搭建

以前的篇章有講過怎麼搭建。此次在貼一次。直接下載官網已經打好的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

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: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

實時監控數據

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

相關文章
相關標籤/搜索