Hello Spring Cloud Alibaba(五)之接入sentinel
Sentinel介绍接入sentinel流量控制
Sentinel介绍
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。官网:https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D。sentinel的功能非常丰富。以下本项目只介绍如何接入sentinel,与进行简单的流控。
接入sentinel
引入依赖包spring-cloud-alibaba-sentinel,完整pom如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0
</modelVersion>
<parent>
<groupId>top.musuixin
</groupId>
<artifactId>hello-alibaba
</artifactId>
<version>0.0.1-SNAPSHOT
</version>
</parent>
<groupId>top.musuixin
</groupId>
<artifactId>hello-alibaba-consumer
</artifactId>
<version>0.0.1-SNAPSHOT
</version>
<name>hello-alibaba-consumer
</name>
<description>Demo project for Spring Boot
</description>
<properties>
<java.version>1.8
</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud
</groupId>
<artifactId>spring-cloud-starter
</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud
</groupId>
<artifactId>spring-cloud-alibaba-nacos-config
</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud
</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery
</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud
</groupId>
<artifactId>spring-cloud-alibaba-sentinel
</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-test
</artifactId>
<scope>test
</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage
</groupId>
<artifactId>junit-vintage-engine
</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-maven-plugin
</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件,添加sentinel服务端
spring:
cloud:
nacos:
config:
server-addr: 192.168.0.100
:8848
file-extension: yaml
discovery:
server-addr: 192.168.0.100
:8848
sentinel:
transport:
dashboard: 192.168.0.100
:8858
client-ip: 192.168.0.1
application:
name: hello
-alibaba
-consumer
server:
port: 8000
建一个接口 访问接口后sentinel控制台显示如下:
流量控制
设置流控规则 主要设置阈值,阈值越小,流控越强。我们设置成1访问 /hello ,多次刷新