需求背景
項目中快速集成·限流組件Sentinel
Tips技術點
1. 引入spring-cloud-starter-alibaba-sentinel依賴包
注:默認監控所有訪問接口
代碼演示
1. 項目目錄結構
2. pom.xml依賴組件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-alibaba-sentinel</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.minbo.cloud.alibaba</groupId>
<artifactId>spring-cloud-alibaba-study-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib-ext-spring</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>
</project>
3. bootstrap.yaml配置文件
配置sentinel
spring:
application:
name: microservice-alibaba-sentinel
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: 2bc5565f-453f-4b1c-bde4-eafe23bdb2f0
config:
server-addr: 127.0.0.1:8848
namespace: 2bc5565f-453f-4b1c-bde4-eafe23bdb2f0
file-extension: properties
shared-dataids: microservice-alibaba-sentinel.properties
refreshable-dataids: microservice-alibaba-sentinel.properties
sentinel:
transport:
port: 8719 # 默認數據端口
dashboard: 127.0.0.1:9191
# server
server:
port: 9092
對Nacos配置不熟,可以先閱讀下面兩篇文章
- SpringCloud Alibaba從入門到精通教程(二)- 項目中快速集成配置中心·Nacos服務註冊發現功能
- SpringCloud Alibaba從入門到精通教程(三)- 項目中快速集成配置中心·Nacos配置中心管理功能
4. 接口類
package com.minbo.cloud.alibaba.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
/**
* @author Minbo
*/
@RestController
@Slf4j
@Api(tags = { "init" })
public class InitController {
@ApiOperation(value = "歡迎入口", httpMethod = "GET")
@GetMapping("/hello")
public String hello() {
log.info("microservice-alibaba-sentinel");
return "Hello, greetings from microservice-alibaba-sentinel";
}
}
5. 啓動類
package com.minbo.cloud.alibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
/**
* 程序主入口
*
* @author Minbo
*
*/
@SpringBootApplication
@EnableSwaggerBootstrapUI
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
6. 測試
多訪問幾次接口:http://localhost:9092/hello
Sentinel控制檯,可以看到下圖,實時監控接口訪問數據:
高級用法
1. 流控規則用法
案例:新增接口流控規則,基於QPS,限定只允許每次一個請求通過
新增規則成功後
再次測試接口:
快速訪問多幾次 http://localhost:9092/hello,接口響應結果如下圖:
表示流控規則生效了。在監控控制檯上,可以看到“拒絕QPS”的值,而每次通過的QPS只有一個
其他用法基本同理,比如“降級規則”,“熱點規則”等等,可自行研究探索其用法了
完整源碼下載
其他技術
此項目代碼案例中,還集成了以下功能:
1. 集成swagger
十分簡單、簡潔易用的在線接口文檔組件swagger
Swagger入門教程用法:SpringBoot從入門到精通教程(二十四)- Swagger集成用法
該系列教程
我的專欄
至此,全部介紹就結束了
-------------------------------
-------------------------------
關於我(個人域名)
期望和大家一起學習,一起成長,共勉,O(∩_∩)O謝謝
歡迎交流問題,可加個人QQ 469580884,
或者,加我的羣號 751925591,一起探討交流問題
不講虛的,只做實幹家
Talk is cheap,show me the code