【Spring Cloud Alibaba】Sentinel 服务熔断和限流

1、Sentinel 安装

服务熔断和限流使用 Sentinel 就很方便了,本篇文章主要讲解怎么通过 Sentinel 构建一个服务熔断和限流的详细案例,方便集成到实际项目中使用

Sentinel 下载,https://github.com/alibaba/Sentinel/releases/tag/1.8.2,这里使用的是1.8.2的版本

由于需要使用命令启动,所以写个简单的脚本文件来作为启动类,默认使用的 8080 端口,容易和项目产生冲突,这里我就顺便修改端口为 10000,在 sentinel-dashboard-1.8.2.jar 同级目录创建 sentinel-start.cmd 文件,如果是 Linux 直接把后缀改为.sh 就可以了

Java -Dserver.port=10000 -Dcsp.sentinel.dashboard.server=localhost:10000 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.2.jar

启动日志【Spring Cloud Alibaba】Sentinel 服务熔断和限流浏览器访问:http://localhost:10000/,能看到sentinel的基本信息,到这里就安装完成啦【Spring Cloud Alibaba】Sentinel 服务熔断和限流

2、Sentinel 模块分析

Sentinel 实现的是服务熔断和限流,这里首选需要最基本的两个模块,一个服务提供者,一个服务消费者,所以我们需要创建两个服务来演示服务熔断和限流

回忆一下之前的文章,服务提供者 + 服务消费者我们已经写过很多次了,好几个版本

  • @LoadBalanced + RestTemplate 版本
  • Spring Cloud OpenFeign 版本
  • Spring Cloud Dubbo 版本

本篇文章核心分析问题为 sentinel,所以采用代码最少的 Spring Cloud OpenFeign 版本来进行举例学习

  • spring-cloud-alibaba-sentinel-provider 服务提供者
  • spring-cloud-alibaba-sentinel-consumer 服务消费者

3、构建 Sentinel 服务提供者

创建 spring-cloud-alibaba-sentinel-provider 模块,核心增加依赖 spring-cloud-starter-alibaba-sentinel,其其他的和 Nacos 的配置一样

        <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>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

修改 application.properties 配置文件,一样的注册到 Nacos

server.port=8086
spring.application.name=spring-cloud-alibaba-sentinel-provider
management.server.port=9096
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

#
 Nacos注册信息
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.namespace=sandbox-configuration

启动类增加注解激活

@EnableDiscoveryClient

创建一个控制层方法, 用于处理业务逻辑,便于后续的 Feign 调用

package cn.tellsea.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 服务提供者
 *
 * @author Tellsea
 * @date 2021/12/20
 */

@RestController
public class MessageController {

    @GetMapping("/message/{message}")
    public String printMsg(@PathVariable String message) {
        return "[服务提供者] : " + message;
    }
}

检查 Nacos 服务是否注册成功【Spring Cloud Alibaba】Sentinel 服务熔断和限流

4、构建 Sentinel 服务消费者

创建 spring-cloud-alibaba-sentinel-consumer 模块,依赖比服务提供者多了一个 OpenFeign,这里是为了调用消费者的

        <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>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

修改 application.properties 配置文件,这里增加了 sentinel 配置

server.port=8087
spring.application.name=spring-cloud-alibaba-sentinel-consumer
management.server.port=9097
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

#
 Nacos注册信息
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.namespace=sandbox-configuration

#
 sentinel配置
spring.cloud.sentinel.transport.port=10000
spring.cloud.sentinel.transport.dashboard=localhost:10000

启动类增加注解激活

@EnableDiscoveryClient
@EnableFeignClients

编写远程调用的 Service 的方法

package cn.tellsea.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 指向服务提供者应用
 *
 * @author Tellsea
 * @date 2021/12/20
 */

@FeignClient("spring-cloud-alibaba-sentinel-provider")
public interface MessageService {

    /**
     * 远程方法
     *
     * @param message
     * @return
     */

    @GetMapping("/message/{message}")
    String printMsg(@PathVariable("message") String message);
}

最后就是控制层的方法

package cn.tellsea.controller;

import cn.tellsea.service.MessageService;
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.RestController;

/**
 * Spring Cloud OpenFeign 远程调用
 *
 * @author Tellsea
 * @date 2021/12/20
 */

@RestController
public class OpenFeignController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/feign/message/{message}")
    public String message(@PathVariable String message) {
        return messageService.printMsg(message);
    }
}

然后启动刚创建的消费者模块,检查 Nacos 是否注册成功【Spring Cloud Alibaba】Sentinel 服务熔断和限流到这里生产者消费者都构建完成了,并且成功的注册到了 Nacos 中

5、Sentinel 工作台

浏览器访问消费者提供的接口

http://localhost:8087/feign/message/Tellsea

返回结果

[服务提供者] : Tellsea

这是通过消费者服务,远程 OpenFeign 调用生产者服务的接口返回的结果,此时我们访问

http://localhost:10000/

控制面板多了一个新的模块,就是我们的消费者已经展示到了 sentinel 上了,然后疯狂调用之前的接口,查看面板的日志

【Spring Cloud Alibaba】Sentinel 服务熔断和限流到此,完整的 sentinel 全部配置完成了,后续只需要根据工作中的实际需求,调整左侧的各种规则的配置,就能实现服务之前的各种熔断和限流了,因为网上的文章非常多,这里就不一一举例了,OK

微信公众号


原文始发于微信公众号(花海里):【Spring Cloud Alibaba】Sentinel 服务熔断和限流

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/69499.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!