1、下載使用
sentinel官網
下載sentinel地址:https://github.com/alibaba/Sentinel/releases
下載 sentinel-dashboard-1.7.2.jar
通過java -jar啟動sentinel ,默認sentinel占用的端口是8080 --server.port=8888是將端口設置為8888,如果有改端口需求則更改對應端口即可
java -jar .\sentinel-dashboard-1.7.2.jar --server.port=8888
用戶和密碼都是sentinel
2、maven方式
加入依賴管理和依賴
<dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- spring-cloud-starter-alibaba-nacos-discovery nacos服務注冊中心 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- spring-cloud-starter-alibaba-sentinel sentinel客戶端--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- sentinel-datasource-nacos sentinel持久化數據 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
注意自己修改端口等對應的信息,在這里sentinel使用來自定義的8888端口
server: port: 8400 spring: application: name: sentinel-client #應用名 cloud: nacos: discovery: #服務注冊中心地址 server-addr: 127.0.0.1:8848 config: #nacos的配置中心地址,如果不需要用到則暫時注釋掉 server-addr: 127.0.0.1:8848 file-extension: yml sentinel: transport: dashboard: 127.0.0.1:8888 port: 8719
啟動類
@SpringBootApplication @EnableDiscoveryClient public class SentinelClient8400 { public static void main(String[] args) { SpringApplication.run(SentinelClient8400.class, args); } }
需要流控的Controller類,示例代碼
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SentinelDemoController { @GetMapping("/test001") public String test001(){ return "test001"; } @GetMapping("/test002") public String test002() { return "test002"; } }
然后啟動nacos,再啟動sentinel jar包,然后再啟動上面這個啟動類
需要注意的是sentinel是懶加載的機制,也意味着需要訪問一些上面Controller中的 /test001 才會在sentinel后台看到服務
瀏覽器訪問一下 http://localhost:8400/test001
然后刷新一下sentinel后台 http://localhost:8080/#/dashboard/home
原文鏈接:https://blog.csdn.net/qq_41813208/article/details/106994203