菜单

Juning
发布于 2020-09-16 / 1085 阅读
1
0

五、Spring Cloud Alibaba Sentinel(熔断器)

Spring Cloud Alibaba Sentinel(熔断器)

Sentinel介绍

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。——Sentinel官网

更多介绍可以去Sentinel官网看看,当然也有很详细的使用教程

Feign 中使用 Sentinel

  1. 如果要在您的项目中引入 Sentinel,就要使用 group ID 为 com.alibaba.cloud 和 artifact ID 为 spring-cloud-starter-alibaba-sentinel 的 starter。
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

整体文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>idreamyou4cloud</artifactId>
        <groupId>cn.idreamyou</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>idreamyou4cloud-auth</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--web 模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  1. Sentinel 适配了 Feign 组件。但默认是关闭的。需要在配置文件中配置打开它,在配置文件增加以下代码:
feign:
  sentinel:
    enabled: true

整体文件如下:

server:
  # 服务端口
  port: 8082

spring:
  application:
    # 应用名称,这里采用Maven项目的模块名
    name: @artifactId@
  cloud:
    nacos:
      # 注册中心nacos的地址
      server-addr: localhost:8848

management:
  endpoints:
    web:
      exposure:
        include: "*"

feign:
  sentinel:
    enabled: true
  1. 在 Service 中@FeignClient注解增加 fallback属性用来指定类,整体文件如下:
package cn.idreamyou.idreamyou4cloud.auth.server.feign;

import cn.idreamyou.idreamyou4cloud.auth.server.feign.fallback.FallbackServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * feign客户端
 *
 * @author juning
 * @date 2020/9/14
 */
@FeignClient(value = "idreamyou4cloud-upms", fallback = FallbackServiceImpl.class)
public interface FeignService {

    @GetMapping(value = "/test/{message}")
    String test(@PathVariable("message") String message);

}
  1. 创建熔断器并实现对应的feign接口:
package cn.idreamyou.idreamyou4cloud.auth.server.feign.fallback;

import cn.idreamyou.idreamyou4cloud.auth.server.feign.FeignService;
import org.springframework.stereotype.Component;

/**
 * sentinel熔断
 *
 * @author juning
 * @date 2020/9/14
 */
@Component
public class FallbackServiceImpl implements FeignService {
    @Override
    public String test(String message) {
        return "test fallback";
    }
}

整体解构如下:

image.png

测试熔断器

关闭所有启动的idreamyou4cloud-upms模块,再次浏览器访问http://localhost:8082/test/hello:

image.png

熔断器配置成功!


评论