springBoot整合 sentinel限流 nacos動態規則配置


版本說明:(被版本坑慘了)

有一個圖可以參考

 

 

springboot:2.1.3.RELEASE 后來改成2.1.2.RELEASE 了 但應該都可以

nacos:本地安裝的1.4.0

sentinel:1.7.1 

對應的包也引入支持sentinel1.7.1的

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>

 

首先講一些遇到的坑點吧。

剛開始引入的這個包  就本地連接sentinel還是可以成功的  但后面連接nacos就會報錯

仔細觀察可以看到引入該包 下面的sentinel相關的版本是1.5.2 所以出現的問題可能和版本有關

<!--sentinel提供的一個微服務開發的起步依賴-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
   <version>0.9.0.RELEASE</version>
</dependency>

 

言歸正傳開始整合

1.首先創建springBoot項目

配置文件application.yml

#端口號
server:
  port: 8089

#項目名稱
spring:
  application:
    name: sentinel-demo

  cloud:
      #配置sentinel客戶端,注冊該項目進控制台里
      sentinel:
        eager: true
        transport:
          #配置Sentin dashboard地址
          dashboard: localhost:8080
          # 默認8719端口,假如被占用了會自動從8719端口+1進行掃描,直到找到未被占用的 端口
          port: 8719
        #nacos  從nacos拉取數據需要配置
        datasource:
          #名稱隨意
          flow:
            nacos:
              server-addr: localhost:8848
              dataId: ${spring.application.name}-flow-rules
              groupId: SENTINEL_GROUP
              rule-type: flow

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot-sentinel-start</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-sentinel-start</name>
    <description>Sentinel for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
      
   <!-- springBoot包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
      <!--sentinel的包-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

        <!--使用 Nacos 配置規則-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>1.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!--<scope>test</scope>-->
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>    

 

 

 

然后啟動nacos 

配置完這些之后就可以從nacos拉去規則配置了

 

 啟動sentinel-dashboard

進入sentinel可以看到 nacos中的規則已經展現在頁面上

 

但是呢 

限制在nacos修改規則 sentinel 控制台可以更新,而在控制台修改了之后 nacos並不會同步,如果我們重啟springBoot項目后規則還是會變成nacos上面的值。

所以我們要對sentinel-dashboard的源碼進行修改。

首先下載sentinel源碼 https://github.com/alibaba/Sentinel/tree/1.7.1

然后打開sentinel-dashboard項目

1.首先 將1處的nacos文件夾復制到2處的rule文件夾下面

 

2.修改FlowControllerV2中的   flowRuleDefaultProvider 改為flowRuleNacosProvider  、flowRuleDefaultPublisher改為 flowRuleNacosPublisher

 

 3.修改 FlowServiceV1 為FlowServiceV2

 

 4.修改sidebar.html

 

 5.修改flow_v2.html

 

 以上就大功告成了。

 

改完之后可以將sentinel-dashboard 打一個jar包

使用 mvn clean package 進行源碼編譯

碼編譯通過后在Sentinel-release/sentinel-dashboard/target 下產生 sentinel-dashboard.jar 的jar包

需要時啟動就可以 java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -jar sentinel-dashboard.jar

java -Dserver.port=8080 \ 指定端口號
-Dcsp.sentinel.dashboard.server=localhost:8080 \ 外部服務連接sentinel控制台地址
-jar -sentinel-dashboard.jar 啟動控制台

然后訪問就可以了

 


坑點:

當控制台重啟后,在已有的規則上在添加 規則會出現覆蓋之前規則的情況。

原因:

 

 

 

說明 每次重啟之后再添加 都會從id為1 的開始覆蓋,如果原來有id為1得規則 就會被覆蓋掉。

解決方案:修改  InMemFlowRuleStore.java  相應得 nextId()方法都要加上對應的參數

 @Autowired
    private InMemoryRuleRepositoryAdapter<FlowRuleEntity> repository;

    @Override
    protected long nextId(FlowRuleEntity entity)
    {
        if(ids.intValue()==0){//如果是重啟后 且存在已有規則則賦值為最大id+1
            if(!CollectionUtils.isEmpty( repository.findAllByApp(entity.getApp()))){
                long maxId=repository.findAllByApp(entity.getApp()).stream().max(Comparator.comparingLong(FlowRuleEntity::getId)).get().getId();
                ids.set(maxId);
            }
        }
      return ids.incrementAndGet();
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM