Sentinel入门

简介:

Sentinel是阿里巴巴开源的一款用于流量控制、熔断降级和系统负载保护的库。它可以帮助开发者实现对分布式系统中的服务进行流量控制,避免因为流量激增而导致系统崩溃或服务质量下降。

Sentinel主要功能包括:

  1. 流量控制:Sentinel可以对应用程序的入口流量进行实时的监控和控制,通过设定阈值来限制并发请求的数量,防止系统因为过多请求而崩溃或过载。

  2. 熔断降级:当系统中的某个服务出现故障或不稳定时,Sentinel可以实现熔断,停止向该服务发送请求,避免整个系统的连锁故障,同时提供降级策略来保证系统的可用性。

  3. 系统负载保护:Sentinel可以监控系统的负载情况,根据系统负载自动调整流量控制策略,避免系统负载过高导致性能下降。

  4. 实时统计和监控:Sentinel提供了实时的统计信息和监控面板,方便开发人员实时查看系统的运行状态和流量情况,及时进行调整和优化。

总的来说,Sentinel是一款强大的流量控制和系统保护库,可以帮助开发者构建健壮的分布式系统,提高系统的稳定性和可靠性。

可解决问题:

服务雪崩
是指因为一个服务出现问题,导致依赖它的服务也受到影响,进而让整个系统 延迟或失效的现象
称为雪崩。
雪崩的解决方案
隔离 业务隔离、线程池隔离、信号量隔离?
超时设置 远程调用时设置最大响应时间
限流 限制系统的输入和输出流量,以达到保护系统的目的。
熔断 当下游服务因访问压力过大而变慢时,上游暂时切断对下游服务的调用。 这种牺牲局部
保全部的措施称为熔断。
降级 托底方案。

对比:

具体应用:

下载与启动:

java -jar sentinel-dashboard-1.6.3.jar

引入依赖

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
添加配置
spring:
application:
name: sentinel-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8080 # dashboard服务地址
port: 8719 #??

限流

先访问,后设置规则。
@GetMapping("/byResource")
@SentinelResource(value = "根据资源", blockHandler = "handleRateLimit")
public String getResource() {
return "byResource";
}
@GetMapping("/byUrl")
public String getByUrl() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "byUrl";
}
public String handleRateLimit(BlockException exception) {
return "被流控了!";
}
规则持久化
1. 添加依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
2. 在流控应用中添加配置 所谓流控应用,就是要施加持久流控规则的应用。在其
application.yml 中添加:
spring:
cloud:
sentinel:
datasource:
ds1:
nacos:
server-addr: localhost:8848
data-id: ${spring.application.name}
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
3. nacos 中添加对应的配置 服务名使用上述 ${spring.application.name} 指定的 user( 我的应
用服务名为 user).
[
{
"resource": "/user/{id}",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]

熔断

1. RestTemplate 熔断
使用 @SentinelRestTemplate 修饰 RestTemplate 实现
@Bean
@SentinelRestTemplate
public RestTemplate restTemplate() {
return new RestTemplate();
}
2. Feign 熔断 记得在配置中打开
feign:
sentinel:
enabled: true
定义接口
@FeignClient(value="", fallback=UserServiceFallback.class)
public interface UserService {
}

降级

Feign也支持服务降级,本质上它就是使用Hystrix来实现的。
@FeignClient(name="house", fallback = HouseFeignServiceFallback.class)
降级类要实现上面定义的接口,并实现降级方法,另外注意在降级类上添加@Service注解,以纳入spring管理
@Service
public class OrderServiceCallback implements OrderServiceFeign{
    @Override
    public Integer getOrderNumberOfUser(Long id) {
        return 22;
    }

    @Override
    public Map postOrderNumberOfUser(Long id) {
        Map result = new HashMap();
        result.put("number",23);

        return result;
    }
}

改配置

因为feign也是使用hystrix实现的降级,且默认没有打开降级支持,所以这里需要打开hystrix功能

feign:
  hystrix:
    enabled: true
打开日志

打开Feign调用日志,方便查找问题。

//注意,需要放在被@Configuration修饰的类中
@Bean
Logger.Level feignLogLevel() {
    return Logger.Level.FULL;
}

同时,需要配置yml文件中的日志等级。

logging:
  level:
    org.example.service: debug

相关推荐

  1. Ali-Sentinel-入口控制

    2024-03-29 18:22:02       30 阅读
  2. <span style='color:red;'>Sentinel</span>

    Sentinel

    2024-03-29 18:22:02      59 阅读
  3. <span style='color:red;'>Sentinel</span>

    Sentinel

    2024-03-29 18:22:02      45 阅读
  4. <span style='color:red;'>Sentinel</span>

    Sentinel

    2024-03-29 18:22:02      41 阅读
  5. <span style='color:red;'>Sentinel</span>

    Sentinel

    2024-03-29 18:22:02      38 阅读
  6. <span style='color:red;'>Sentinel</span>

    Sentinel

    2024-03-29 18:22:02      26 阅读
  7. <span style='color:red;'>Sentinel</span>

    Sentinel

    2024-03-29 18:22:02      34 阅读
  8. <span style='color:red;'>Sentinel</span>

    Sentinel

    2024-03-29 18:22:02      30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-29 18:22:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-29 18:22:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-29 18:22:02       87 阅读
  4. Python语言-面向对象

    2024-03-29 18:22:02       96 阅读

热门阅读

  1. 优雅的使用ChromeDriver

    2024-03-29 18:22:02       38 阅读
  2. mysql table_id介绍

    2024-03-29 18:22:02       43 阅读
  3. URLSearchParams

    2024-03-29 18:22:02       46 阅读
  4. zookeeper源码(11)临时节点

    2024-03-29 18:22:02       39 阅读
  5. 【C++】动态内存分配 与 对象的动态建立与释放

    2024-03-29 18:22:02       47 阅读
  6. LeetCode 21

    2024-03-29 18:22:02       43 阅读
  7. Leetcoder Day43| 单调栈1

    2024-03-29 18:22:02       36 阅读
  8. TCP建立连接时,为什么每次的SYN都不一样

    2024-03-29 18:22:02       43 阅读
  9. Qwen1.5模型微调部署全过程

    2024-03-29 18:22:02       36 阅读
  10. 【无标题】

    2024-03-29 18:22:02       33 阅读
  11. AI大模型学习:AI大模型在特定领域的应用

    2024-03-29 18:22:02       41 阅读
  12. HDU水题刷题记录

    2024-03-29 18:22:02       49 阅读
  13. C#WPF将变量或自定义数据类绑定到控件实例

    2024-03-29 18:22:02       39 阅读
  14. vue做一个锁屏禁止页面前进后退

    2024-03-29 18:22:02       45 阅读