閱讀文本大概需要3分鐘。
【Sentinel如何進行流量監控】這篇文件是對sentinel-dashboard自己進行監控。本篇看看如何在自己的SpringBoot項目或者SpringCloud項目接入Sentinel。具體參考:
https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel
0x01:新建項目olive-sentinel-client
項目中的pom.xml文件引入
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
0x02:application.properties配置文件添加配置項
server.port=8081
spring.application.name=olive-sentinel-client
spring.cloud.sentinel.transport.dashboard=127.0.0.1:8080
spring.cloud.sentinel.transport.dashboard配置項是配置sentinel-dashboard控制檯的地址和端口;另外Sentinel還提供了其他的配置參數,具體可以參考
https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel
0x03:編寫一個Controller和SpringBoot啓動類
package com.sentinel.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value={"/getTest"},produces="application/json;charset=UTF-8")
public Map<String, Object> getTest() {
Map<String, Object> result = new HashMap<>();
result.put("code", "success");
return result;
}
}
package com.sentinel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
0x04:啓動sentinel-dashboard控制檯
登錄sentinel-dashboard控制檯,可以看到一個空的界面
注,這時啓動sentinel-dashboard無需再添加任何的JVM啓動參數:
0x05:啓動olive-sentinel-client項目
啓動後,訪問接口:http://127.0.0.1:8081/getTest
再次查看sentinel-dashboard控制檯
就這麼簡單springboot項目就添加了sentinel監控
☆
往期精彩
☆
02 Nacos源碼編譯
03 基於Apache Curator框架的ZooKeeper使用詳解
關注我
每天進步一點點
喜歡!在看☟