相关网站:
https://github.com/alibaba/Sentinel
https://github.com/alibaba/Sentinel/wiki
https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#spring_cloud_alibaba_sentinel
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Sentinel 具有以下特征:
Sentinel 的主要特性:
Sentinel 的开源生态:
Sentinel 分为两个部分:
解决服务使用中的各种问题:
Hystrix
需要手工搭建监控平台
没有可以进行更加细粒度化配置的 Web 界面,比如流控、速率控制、服务熔断、服务降级等
Sentinel
单独一个组件,可以独立出来,搭建难度降低
提供了可以进行细粒度统一配置的 Web 界面
https://github.com/alibaba/Sentinel/releases
找到 sentinel-dashboard-1.7.0.jar 进行下载
确保 Java 8 运行环境正常,确保 8080 端口没有被占用。
运行命令:
java -jar sentinel-dashboard-1.7.0.jar
http://localhost:8080
默认 用户名 / 密码 为 sentinel / sentinel
1、建 Module
cloudalibaba-sentinel-server8401
2、改 POM
<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">
<parent>
<artifactId>cloud2020artifactId>
<groupId>demo.yangxu.springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloudalibaba-sentinel-server8401artifactId>
<dependencies>
<dependency>
<groupId>demo.yangxu.springcloudgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cspgroupId>
<artifactId>sentinel-datasource-nacosartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>4.6.3version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
3、写 YML
application.yaml
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服务注册中心地址
sentinel:
transport:
dashboard: localhost:8080 #配置Sentinel dashboard地址
#默认8719端口,假如被占用会自动从8719开始依次+1扫描
#直至找到未被占用的端口
port: 8719
management:
endpoints:
web:
exposure:
include: '*'
4、主启动类
SentinelMain8401
package demo.yangxu.springcloud.alibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelMain8401 {
public static void main(String[] args) {
SpringApplication.run(SentinelMain8401.class, args);
}
}
5、业务类
FlowLimitController
package demo.yangxu.springcloud.alibaba.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class FlowLimitController {
@GetMapping("/testA")
public String testA()
{
return "------testA";
}
@GetMapping("/testB")
public String testB()
{
log.info(Thread.currentThread().getName()+"\t"+"...testB");
return "------testB";
}
}
由于 Sentinel 采用的是懒加载,需要至少访问一次 Controller 中的相关网址,才能在 Sentinel 的控制台中看到信息。
访问:
http://localhost:8401/testA
http://localhost:8401/testB
访问完成后再次访问 Sentinel 控制台
http://localhost:8080/#/dashboard