SpringBoot&SpringCloud项目接入Sentinel监控

阅读文本大概需要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监控

往期精彩

01 Sentinel如何进行流量监控

02 Nacos源码编译

03 基于Apache Curator框架的ZooKeeper使用详解

04 spring boot项目整合xxl-job

05 互联网支付系统整体架构详解

关注我

每天进步一点点

喜欢!在看☟

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章