springcloudalibaba架构(2):Sentinel服务容错与微服务集成Sentinel使用入门

发布于:2023-01-30 ⋅ 阅读:(833) ⋅ 点赞:(0)

前言

在微服务架构中,我们将业务拆分成很多的服务,服务与服务之间可以互相调用,但是由于一些原因,服务并不能保证服务的100%可用,如果单个服务出现问题,调用这个服务就会出现网络延迟,此时如果有大量的请求,会形成任务堆积,导致服务雪崩。

如何解决服务雪崩?
Spring Cloud提供了一系列的组件。

  • Hystrix(已闭源): Netflix开源的延迟和容错库,用于隔离访问远程系统、服务或者第三方库,防止级联失败,提供系统的可用性和容错性。
  • Resilience4J:一款非常轻量、简单,并且文档非常清晰、丰富的熔断工具,这是Hystrix官方推荐的替代品。不仅如此,Resilicence4j还原生支持Spring Boot 1.x/2.x,而监控也支持和prometheus等多款主流产品进行整合。
  • Sentinel :阿里开源的一款断路器实现,被阿里大规模使用,稳定可靠。

本章代码已分享至Gtiee :https://gitee.com/lengcz/springcloudalibaba01.git

Sentinel

Sentinel是阿里开源的一套用于服务容错的解决方案。它以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度来保护服务的稳定性。
Sentinel分为两部分

  • 核心库(Java客户端)不依赖于任何框架/库,能够运行于所有Java运行时环境,同时对Dubbo/Spring Cloud 等框架也可以很好的支持。
  • 控制台(Dashboard)基于Spring Boot开发,打包后可以直接运行,不需要哦额外的Tomcat等容器。

sentinel-dashboard控制台

下载地址:https://github.com/alibaba/Sentinel/releases
在这里插入图片描述
启动dashboard,sentinel-dashboard 是一个springboot项目

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.4.jar

直接访问 http://localhost:8080/,默认账号名sentinel,密码是sentinel
在这里插入图片描述

微服务集成Sentinel

在order模块中

  1. 添加依赖(这一步已经整合好了sentinel)
 <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
  1. 添加一个测试的controller
/**
 * 服务容错示例
 */
@RestController
@Slf4j
public class HelloController {

    @GetMapping("/hello1")
    public String hello() {
        log.info("say hello------------1");
        return "hello1";
    }

    @GetMapping("/hello2")
    public String hello2() {
        log.info("say hello------------2");
        return "hello2";
    }
}
  1. 配置dashboard地址
spring:
  cloud:
    sentinel:
      transport:
        port: 12345 #与控制台交流的端口,可随意指定,不冲突即可。
        dashboard: localhost:8080 # sentinel-dashboard的地址
  1. 启动服务,dashboard没有显示我们的应用,因为dashboard使用的懒加载方式,需要请求一次order服务的接口(任意接口),示例:http://localhost:8080/hello1,即可。

在这里插入图片描述
可以看到实时请求的数据
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到