官网地址
sentinel官网: https://github.com/alibaba/Sentinel/wiki/%E4%B8%BB%E9%A1%B5
sentinel 下载地址: https://github.com/alibaba/Sentinel/releases
nacos官网: https://nacos.io/zh-cn/docs/deployment.html
nacos下载地址: https://github.com/alibaba/nacos/releases
部署
容器部署
简化的示例,展示了如何在Docker Compose文件中定义Sentinel和Nacos:
version: '3'
services:
nacos:
image: nacos/nacos-server:latest
ports:
- "8848:8848"
environment:
- MODE=standalone
# 其他Nacos配置...
sentinel:
image: your-sentinel-image:latest # 替换为您的Sentinel镜像
ports:
- "8080:8080" # Sentinel控制台端口
depends_on:
- nacos
environment:
- SPRING_CLOUD_SENTINEL_DATASOURCE_DS1_NAME=nacos
- SPRING_CLOUD_SENTINEL_DATASOURCE_DS1_TYPE=nacos
- SPRING_CLOUD_SENTINEL_DATASOURCE_DS1_NAMESPACE=public # 您的Nacos命名空间
- SPRING_CLOUD_SENTINEL_DATASOURCE_DS1_SERVER-ADDR=nacos:8848 # 指向Nacos服务的地址
# 其他Sentinel配置...
启动Docker Compose
docker-compose up -d
jar启动
- 下载Sentinel JAR包
- 访问Sentinel的官方GitHub地址或Maven仓库,下载适合您项目的Sentinel JAR包。例如,sentinel-dashboard-xxx.jar。
- 配置Nacos数据源:
- 在Spring Cloud项目中,需要添加Nacos数据源相关的Maven依赖。例如:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>您的Sentinel版本</version>
</dependency>
- 在项目的配置文件(如application.properties或application.yml)中,配置Nacos数据源的相关参数,如Nacos服务器的地址、端口、命名空间、数据ID等。例如:
spring.cloud.sentinel.datasource.ds1.nacos.server-addr=127.0.0.1:8848
spring.cloud.sentinel.datasource.ds1.nacos.dataId=sentinel-rules
spring.cloud.sentinel.datasource.ds1.nacos.group=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds1.nacos.namespace=public
# 其他配置项...
- 启动Sentinel:
- 在命令行中,进入包含Sentinel JAR包的目录。
- 使用java -jar命令启动Sentinel,并指定配置文件(如果需要)。例如:
java -jar sentinel-dashboard-xxx.jar --spring.config.location=classpath:application.properties
注意:这里假设已经将Nacos数据源的配置写入了application.properties文件,并且该文件位于类路径(classpath)下。如果配置文件位置不同,需要相应地修改–spring.config.location参数的值。
DEMO
官网demo地址:https://github.com/alibaba/Sentinel/blob/master/sentinel-demo/sentinel-demo-nacos-datasource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/nacos/NacosDataSourceDemo.java
public class NacosDataSourceDemo {
private static final String KEY = "TestResource";
// nacos server ip
private static final String remoteAddress = "localhost:8848";
// nacos group
private static final String groupId = "Sentinel_Demo";
// nacos dataId
private static final String dataId = "com.alibaba.csp.sentinel.demo.flow.rule";
// if change to true, should be config NACOS_NAMESPACE_ID
private static boolean isDemoNamespace = false;
// fill your namespace id,if you want to use namespace. for example: 0f5c7314-4983-4022-ad5a-347de1d1057d,you can get it on nacos's console
private static final String NACOS_NAMESPACE_ID = "${namespace}";
public static void main(String[] args) {
if (isDemoNamespace) {
loadMyNamespaceRules()