Spring Cloud 2021.0.5.0 是
Spring Cloud的一个
版本,它可以和 Sentinel-datasource-
nacos模块一起
使用。下面是
使用 Spring Cloud 2021.0.5.0 和 Sentinel-datasource-
nacos的步骤:
1. 在 pom.xml 文件中引入以下依赖:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId><version>2021.0.5.0</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-nacos-discovery</artifactId><version>2.2.5.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-nacos-config</artifactId><version>2.2.5.RELEASE</version></dependency>
2. 配置
Nacos作为 Sentinel 的数据源,例如:
spring:cloud:sentinel:transport:dashboard: localhost:8080port: 8719nacos:server-addr: localhost:8848groupId: DEFAULT_GROUPdataId: sentinelrule-type: flow
3. 在
Nacos中创建对应的命名空间和配置文件,例如:
spring:cloud:sentinel:transport:dashboard: localhost:8080port: 8719nacos:server-addr: localhost:8848groupId: DEFAULT_GROUPdataId: sentinelrule-type: flowdata-type: jsonnamespace: 7e1d4f1f-8a31-4f0d-9f8d-0d4a563beba4
4. 在应用程序中添加 Sentinel 规则,例如:
java@PostConstructpublic void init() throws Exception {String appName = env.getProperty("spring.application.name");ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = newNacosDataSource<>(nacosServerAddr, groupId, appName + "-flow-rule", source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));FlowRuleManager.register2Property(flowRuleDataSource.getProperty());}
以上是
Spring Cloud 2021.0.5.0
使用Sentinel-datasource-
nacos的基本步骤,具体实现可以根据项目需要进行调整。
今天的文章 2026年Nacos配置中心使用(nacos配置中心不生效)分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/51048.html