随着微服务的流行,服务和服务之间的稳定性变得越来越重要。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文件:
4.0.0 pcloud-alibaba com.younger.pcloud.alibaba 1.0-SNAPSHOT com.younger.pcloud.alibaba pcloud-alibaba-sentinel-service8004 1.0-SNAPSHOT pcloud-alibaba-sentinel-service8004 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.csp sentinel-datasource-nacos com.alibaba.cloud spring-cloud-starter-alibaba-sentinel org.springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true cn.hutool hutool-all 4.6.3 org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
新增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.。



