1. 程式人生 > 其它 >springcloud 整合nacos 和sentinel

springcloud 整合nacos 和sentinel

1.下載nacos 和sentinel 對應的jar並安裝 ,下載地址:

sentinel: https://github.com/alibaba/Sentinel/releases
nacos: https://github.com/alibaba/nacos/releases

2.父pom 檔案引入

<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-alibaba-dependencies</artifactId>
   <version>2021.1</version>
   <type>pom</type>
   <scope>import
</scope> </dependency>

子pom檔案引入

       <!--nacos 配置-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!--bootstrap.yaml配置 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <!--sentinel配置 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--nacos資料庫 配置-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>


為了bootStrap.yaml 中@生效,pom.xml中新增

<build>
        <finalName>community</finalName>
        <!-- bootstrap.yaml 中@生效-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true
</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>

方便生產環境切換,新增profiles 配置:

  <profiles>
        <profile>
            <id>local</id>
            <properties>
                <!--當前環境-->
                <spring.active>local</spring.active>
                <config.server-addr>nacos地址</config.server-addr>
                <config.namespace>namespace地址</config.namespace>
                <config.username>nacos賬號</config.username>
                <config.password>nacos密碼</config.password>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
       
    </profiles>

3.新增bootstrap.yaml 配置檔案

server:
  # 埠
  port: 8888

spring:
  application:
    name: zwz-community
  profiles:
    active: @spring.active@
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      config:
        server-addr: @config.server-addr@
        username: @config.username@
        password: @config.password@
        file-extension: yaml
        namespace: @config.namespace@
    sentinel:
      transport:
        dashboard: localhost:8080
      eager: true
      datasource:
        ds:
          nacos:
            server-addr: ${spring.cloud.nacos.config.server-addr}
            namespace: ${spring.cloud.nacos.config.namespace}
            dataId: ${spring.application.name}-rules
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

4.啟動類新增@RefreshScope nacos中配置變化及時同步重新整理

5.在nacos 中新增專案配置

新增sentinel 配置

啟動專案登入sentinel我們可以看到新增的配置資訊