SpringCloud Alibaba从入门到精通教程(五)- 项目中快速集成·限流组件Sentinel

需求背景

项目中快速集成·限流组件Sentinelhtml

Tips技术点

1. 引入spring-cloud-starter-alibaba-sentinel依赖包java

注:默认监控全部访问接口git

代码演示

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配置文件

配置sentinelgithub

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配置不熟,能够先阅读下面两篇文章web

  1. SpringCloud Alibaba从入门到精通教程(二)- 项目中快速集成配置中心·Nacos服务注册发现功能
  2. 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/hellospring

Sentinel控制台,能够看到下图,实时监控接口访问数据:apache

高级用法

1. 流控规则用法

案例:新增接口流控规则,基于QPS,限定只容许每次一个请求经过json

新增规则成功后bootstrap

再次测试接口:架构

快速访问多几回 http://localhost:9092/hello,接口响应结果以下图:

表示流控规则生效了。在监控控制台上,能够看到“拒绝QPS”的值,而每次经过的QPS只有一个

其余用法基本同理,好比“降级规则”,“热点规则”等等,可自行研究探索其用法了

完整源码下载

个人Github源码地址:

https://github.com/hemin1003/spring-cloud-study/tree/master/spring-cloud-alibaba/microservice-alibaba-sentinel

其余技术

此项目代码案例中,还集成了如下功能:

1. 集成swagger

十分简单、简洁易用的在线接口文档组件swagger

Swagger入门教程用法:SpringBoot从入门到精通教程(二十四)- Swagger集成用法

该系列教程

SpringCloud Alibaba从入门到精通教程

个人专栏

 

 

至此,所有介绍就结束了

 

 

-------------------------------

-------------------------------

 

个人CSDN主页

关于我(我的域名)

个人开源项目集Github

 

指望和你们一块儿学习,一块儿成长,共勉,O(∩_∩)O谢谢

欢迎交流问题,可加我的QQ 469580884,

或者,加个人群号 751925591,一块儿探讨交流问题

不讲虚的,只作实干家

Talk is cheap,show me the code