Sentinel 是什么?
Sentinel 具有以下特征:
丰富的应用场景: Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景, 例如秒杀(即突发流量控制在系统容量可以承受的范围), 消息削峰填谷, 集群流量控制, 实时熔断下游不可用应用等.
完备的实时监控: Sentinel 同时提供实时的监控功能. 您可以在控制台中看到接入应用的单台机器秒级数据, 甚至 500 台以下规模的集群的汇总运行情况.
广泛的开源生态: Sentinel 提供开箱即用的与其它开源框架 / 库的整合模块, 例如与 Spring Cloud,Dubbo,gRPC 的整合. 您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel.
完善的 SPI 扩展点: Sentinel 提供简单易用, 完善的 SPI 扩展接口. 您可以通过实现扩展接口来快速地定制逻辑. 例如定制规则管理, 适配动态数据源等.
Sentinel 的主要特性:
Sentinel 的开源生态:
我们大概叙述后开始整合我们的分布式项目, 我这里里为一个简单分布式项目, 注册中心为 Nacos, 一个服务提供者 (user-center) 和一个服务消费者(content-center)
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.cloud.client.ServiceInstance;
- import org.springframework.cloud.client.discovery.DiscoveryClient;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Service;
- import org.springframework.web.client.RestTemplate;
- import java.util.List;
- import java.util.concurrent.ThreadLocalRandom;
- import java.util.stream.Collectors;
- @Slf4j
- @Service
- @RequiredArgsConstructor(onConstructor = @__(@Autowired))
- public class ShareService {
- private final ShareMapper shareMapper;
- private final RestTemplate restTemplate;
- public ShareDTO findById(Integer id) {
- // 获取分享详情
- Share share = this.shareMapper.selectByPrimaryKey(id);
- // 发布人 id
- Integer userId = share.getUserId();
- UserDTO userDTO = this.restTemplate.getForObject(
- "http://user-center/users/{userId}",
- UserDTO.class, userId
- );
- ShareDTO shareDTO = new ShareDTO();
- // 消息的装配
- BeanUtils.copyProperties(share, shareDTO);
- // shareDTO.setWxNickname(userDTO.getWxNickname());
- return shareDTO;
- }
- }
可以看到我的 content-center 调用 user-center/users 服务
- import lombok.RequiredArgsConstructor;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.Web.bind.annotation.GetMapping;
- import org.springframework.Web.bind.annotation.PathVariable;
- import org.springframework.Web.bind.annotation.RequestMapping;
- import org.springframework.Web.bind.annotation.RestController;
- @RestController
- @RequestMapping("/shares")
- @RequiredArgsConstructor(onConstructor = @__(@Autowired))
- public class ShareConroller {
- private final ShareService shareService;
- @GetMapping("/{id}")
- public ShareDTO findById(@PathVariable Integer id) {
- return this.shareService.findById(id);
- }
- }
我们现在只启动 content-center 不启动 user 服务, 可想而知肯定报错, 我们来看
没错 500, 找不到 user-center 服务, 我们现在整合 Sentinel, 首先加入两个依赖
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-alibaba-sentinel</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
和 actuator 配合使用, 可通过 http://localhost:8010/actuator/sentinel 端点进行查看
- {
- "blockPage":null,
- "appName":"content-center",
- "consoleServer":"localhost:8333",
- "coldFactor":"3",
- "rules":{
- "systemRules":[
- ],
- "authorityRule":[
- ],
- "paramFlowRule":[
- ],
- "flowRules":[
- ],
- "degradeRules":[
- ]
- },
- "metricsFileCharset":"UTF-8",
- "filter":{
- "order":-2147483648,
- "urlPatterns":[
- "/*"
- ],
- "enabled":true
- },
- "totalMetricsFileCount":6,
- "datasource":{
- },
- "clientIp":"192.168.2.29",
- "clientPort":"8719",
- "logUsePid":false,
- "metricsFileSize":52428800,
- "logDir":"/Users/zhangguowei/logs/csp/",
- "heartbeatIntervalMs":10000
- }
我们暂时先不要管这些是什么我们后面再详细阐述, 从 JSON 的形式看不太方便, Sentinel 为我们整合有 UI 界面
整合 Sentinel 可视化界面
下载 sentinel 的 jar 包然后直接启动 https://github.com/alibaba/Sentinel/releases
启动的时候可以自定义端口号
账号密码默认为 sentinel, 在项目的 YAML 中加入
- spring:
- datasource:
- url: jdbc:MySQL://localhost:3306/content_center
- hikari:
- username: ***
- password: ******
- driver-class-name: com.MySQL.cj.jdbc.Driver
- cloud:
- sentinel:
- transport:
- # 指向 sentinel UI 界面
- dashboard: localhost:8333
- nacos:
- discovery:
- # 指定 nacos server 的地址
- server-addr: localhost:8848
- application:
- # 服务名称尽量用 -, 不要用_, 不要用特殊字符
- name: content-center
- server:
- port: 8010
- management:
- endpoints:
- Web:
- exposure:
- include: "*"
这个时候我们继续刚刚的 500 再执行几次会看到
相对于原来的 Hystrix 的 UI 界面 Sentinel 更人性化
他支持 QPS 的查看, 请求时间等等
那么我们首先来探讨流控规则, 我们现在把 user 服务也启动, 我们点击簇点链路
可以看到 content 曾经访问过的路径, 我们点击流控按钮, 就可以为这路径设置流控规则
我们可以看到资源名默认为我们请求的路径, 其实也不一定是路径, 他只是做唯一标识,
- import org.springframework.Web.client.RestTemplate;
- public class SentinelTest {
- public static void main(String[] args) {
- RestTemplate restTemplate = new RestTemplate();
- for (int i = 0; i <10000 ; i++) {
- String forObject = restTemplate.getForObject("http://localhost:8010/actuator/sentinel", String.class);
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
来源: https://www.cnblogs.com/zgwjava/p/11211525.html