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我们可以看到添加的配置信息