spring cloud 2021.0.5.0 使用 sentinel‐datasource‐nacos
时间: 2023-08-24 14:08:35 浏览: 179
Spring Cloud 2021.0.5.0 是 Spring Cloud 的一个版本,它可以和 Sentinel-datasource-nacos 模块一起使用。下面是使用 Spring Cloud 2021.0.5.0 和 Sentinel-datasource-nacos 的步骤:
1. 在 pom.xml 文件中引入以下依赖:
```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 的数据源,例如:
```yaml
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
port: 8719
nacos:
server-addr: localhost:8848
groupId: DEFAULT_GROUP
dataId: sentinel
rule-type: flow
```
3. 在 Nacos 中创建对应的命名空间和配置文件,例如:
```yaml
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
port: 8719
nacos:
server-addr: localhost:8848
groupId: DEFAULT_GROUP
dataId: sentinel
rule-type: flow
data-type: json
namespace: 7e1d4f1f-8a31-4f0d-9f8d-0d4a563beba4
```
4. 在应用程序中添加 Sentinel 规则,例如:
```java
@PostConstruct
public void init() throws Exception {
String appName = env.getProperty("spring.application.name");
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(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 的基本步骤,具体实现可以根据项目需要进行调整。
阅读全文
相关推荐










