sentinel环境搭建
- sentinel环境搭建 推荐度:
- 相关推荐
sentinel环境搭建
简单搭建sentinel环境
sentinel 是什么?
主要目标是流量控制
sentinel 分两个部分:
1、 服务端,是一个 spring boot 服务
2、客户端API,需要 应用接入
首先需要启动 sentinel服务。
1. 下载 sentinel 服务端jar包
2. 像启动自己写的应用一样,使用 java -jar 来启动,具体可以参考java -server -Xms64m -Xmx256m -Dserver.port=8849 -Dcsp.sentinel.dashboard.server=localhost:8849 -Dproject.name=sentinel-dashboard -jar /work/sentinel-dashboard-1.7.1.jar 这个命令
3. 启动 就可以使用 浏览器 访问了
其次,被 sentinel 管控的 应用 需要接入:
1. 在spring boot项目中增加依赖及配置
<!-- sentinel --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId><version>2.1.1.RELEASE</version></dependency><!-- sentinel-nacos规则持久化 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId><version>1.7.0</version></dependency>
2. .yml文件增加配置
spring:cloud:sentinel:transport:dashboard: localhost:8080datasource:ds1:nacos:server-addr: localhost:8848 # nacos 地址(数据源地址)data-id: sentinel-test-config # 配置项名称data-type: json # 数据格式rule-type: flow
3. 项目中增加资源
@Service
public class TestService {@SentinelResource("hello,wxj")public String sayHello(String name){return "hello," + name;}
}
4. 对资源的使用
@RestController
@RequestMapping("/v1")
public class TestController {@AutowiredTestService testService;@GetMapping("/hellotest")public String t1(HttpServletRequest request){return testService.sayHello("ok");}
}
5. 在nacos 增加对资源的控制的 配置,如下
[{"resource": "hello,wxj","limitApp": "default","grade": 1,"count": 1,"strategy": 0,"controlBehavior": 0,"clusterMode": false}
]
6. 启动项目
在sentinel控制台可以看到 nacos的配置信息,访问资源时会被有效的控制