demo
Spring boot 与 Sentinel
Quick Start
运行控制台
github 地址
https://github.com/alibaba/Sentinel
文档地址:
https://github.com/alibaba/Sentinel/wiki/
下载 sentinel-dashboard-1.8.1.jar
运行 java -jar sentinel-dashboard-1.8.1.jar
访问web地址:http://localhost:8080 默认用户名和密码都是sentinel/sentinel,然后进入首页
项目添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
添加配置
server.port=8081
# sentinel 配置
spring.application.name=hitol-sentinel
spring.cloud.sentinel.transport.dashboard=localhost:8080
spring.cloud.sentinel.transport.heartbeat-interval-ms=500
编写一个 controller 用于测试
@RestController
public class SentinelController {
@RequestMapping("/")
public String sentinel() {
return "sentinel ....";
}
}
浏览器访问 localhost:8081,多刷新几次,控制台实时监控

流量控制

添加规则后多刷新几次接口

Dubbo 与 Sentinel
服务提供者修改
增加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-apache-dubbo-adapter</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.7.1</version>
</dependency>
修改配置
server:
port: 8070
spring:
application:
name: service-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
group: test
namespace: fed092ce-956c-4792-977c-affc5d8f1dc7
sentinel:
transport:
port: 8070
dashboard: localhost:8080
服务消费者修改
增加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-apache-dubbo-adapter</artifactId>
<version>1.7.0</version>
</dependency>
因为消费者是通过 http 调用的,所以比服务提供者少了一个依赖
修改配置
server:
port: 8071
spring:
application:
name: service-consumer
cloud:
nacos:
discovery:
group: test
namespace: fed092ce-956c-4792-977c-affc5d8f1dc7
server-addr: 127.0.0.1:8848
sentinel:
transport:
port: 8071
dashboard: localhost:8080
sentinel.transport.port 跟程序运行的 port 一致才行。
运行结果
添加完依赖,修改完配置,启动即可。
调用 服务消费者提供的 api,多刷新几次


