Sentinel是是什么?
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性,说白了其实就是我们之前讲过的Hystrix。
Sentinel 主要特征
Sentinel官网地址: https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_sentinel
Sentinel中文介绍: https://github.com/alibaba/Sentinel/wiki
sentinel 分别两部分:
- 核心库(Java 客户端)不依赖任务框架/库,能够运行于所有的Java运行环境,同时对Dubbo/Spring Cloud等框架也有比较好的支持
- 控制台基于Spring Boot开发,打包后可以直接运行,不需要依赖额外的Tomcat等应用容器。
Sentinel 控制台下载地址
https://github.com/alibaba/Sentinel/releases
下载jar包,执行命令 java -jar sentinel-dashboard-1.8.3.jar
.
访问地址: http://localhost:8080
登录账号密码均为 sentinel
我们在项目中新建Module : pcloud-alibaba-sentinel-service8004
修改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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>pcloud-alibaba</artifactId>
<groupId>com.younger.pcloud.alibaba</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.younger.pcloud.alibaba</groupId>
<artifactId>pcloud-alibaba-sentinel-service8004</artifactId>
<version>1.0-SNAPSHOT</version>
<name>pcloud-alibaba-sentinel-service8004</name>
<dependencies>
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel-datasource-nacos 持久化-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
新增application.yml文件:
server:
port: 8004
spring:
application:
name: pcloud-sentinel-service
cloud:
nacos:
discovery:
#Nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置Sentinel dashboard地址
dashboard: localhost:8080
#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
management:
endpoints:
web:
exposure:
include: '*'
新增启动类: SentinelServiceMain8004
package com.younger.pcloud.alibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class SentinelServiceMain8004 {
public static void main(String[] args) {
SpringApplication.run(SentinelServiceMain8004.class,args);
}
}
新增controller :
新增 SentinelServiceController 类:
package com.younger.pcloud.alibaba.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SentinelServiceController {
@GetMapping("/testA")
public String testA() {
return "testA";
}
@GetMapping("/testB")
public String testB() {
return "testB";
}
}
启动项目测试一下:
我们看我们的控制台啥也没有。
访问: http://localhost:8004/testA
访问: http://localhost:8004/testB
我们在看下Sentinel控制台:
这个说明Sentinel采用的是懒加载方式进行流量监控的,这个Sentinel 8080 正在监控微服务 8004.。