项目示例 - 3.降级熔断 - 2.Sentinel

项目示例 - 3.降级熔断 - 2.Sentinel

关联知识:

  • 分布式微服务 - 3.降级熔断 - 3.Sentinel

内容提要:

  • 注册、持久化
  • 授权

注册到sentinel

  1. 建Module:微服务起名为sentinel-provider
  2. 改pom:引入以下依赖
    <dependencies>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
  1. 写yml:在resources目录下创建application.yml文件,并做以下配置
server:
  port: 8001

spring:
  application:
    # 服务注册时使用的别名
    name: sentinel-provider
  cloud:
    nacos:
      discovery:
        # nacos的地址
        server-addr: localhost:8848
    sentinel:
      # 取消控制台懒加载
      eager: true
      transport:
        # 配置sentinel dashboard地址
        dashboard: localhost:8080
        # sentinel客户端启动的端口,默认8719端口(占用时会依次+1找未占用端口)
        port: 8719
  1. 主启动:在src下创建如下主启动类
package learn.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SentinelProvider {

    public static void main(String[] args) {
        SpringApplication.run(SentinelProvider.class, args);
    }

}
  1. 业务构建:创建如下controller类
package learn.demo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/sentinel/provider/")
public class ProviderController {

    @Value("${spring.application.name} ${server.port}")
    private String serverInfo;

    @GetMapping("test")
    public String test() {
        return serverInfo+"\t"+ UUID.randomUUID();
    }

}
  1. 测试:
    1. 启动nacos,能正常打开nacos首页
    2. 启动sentinel dashboard,能正常打开首页并登陆
    3. 启动本微服务,在浏览器中输入localhost:8001/sentinel/provider/test 访问接口,能正确返回接口信息
    4. nacos首页中,能在服务列表中找到sentinel-provider服务。sentinel dashboard中,能在左侧列表中找到sentinel-provider服务。

sentinel持久化

  1. 引入依赖:为sentinel-provider微服务引入两个新的依赖
        <dependency>
            
            <groupId>com.alibaba.cspgroupId>
            <artifactId>sentinel-datasource-nacosartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
  1. 修改配置:为sentinel-provider微服务配置nacos配置中心和sentinel持久化配置
server:
  port: 8001

spring:
  application:
    # 服务注册时使用的别名
    name: sentinel-provider
  profiles:
    active: test
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        file-extension: yaml
      discovery:
        # nacos的地址
        server-addr: localhost:8848
    sentinel:
      # 取消控制台懒加载
      eager: true
      transport:
        # 配置sentinel dashboard地址
        dashboard: localhost:8080
        # sentinel客户端启动的端口,默认8719端口(占用时会依次+1找未占用端口)
        port: 8719
      datasource:
        # 配置的数据源名称,可任意起名
        flow:
          # 数据源类型
          nacos:
            # nacos地址
            server-addr: ${spring.cloud.nacos.config.server-addr}
            # 配置文件名称
            data-id: ${spring.application.name}-flow
            # namespace(默认用public命名空间时不需要显示声明)和groupId
            group-id: DEFAULT_GROUP
            data-type: json
            # 读取来的json字符串用于sentinel的哪项配置
            rule-type: flow
  1. 测试
    1. 启动nacos和sentinel dashboard,并检查正常启动
    2. 启动本微服务,在浏览器中输入localhost:8001/sentinel/provider/test 访问接口,能正确返回接口信息
    3. 在nacos配置列表中新建dataId为sentinel-provider-flow,group为DEFAULT_GROUP,配置格式是json的配置文件,配置文件内容如下
[
    {
        "resource":"/sentinel/provider/test",
        "limitApp":"default",
        "grade":1,
        "count":1,
        "strategy":0,
        "controlBehavior":0,
        "clusterMode":false
    }
]
  1. 在sentinel dashboard中查看sentinel-provider微服务的流控规则中多了一个规则
  2. 一秒内多次访问localhost:8001/sentinel/provider/test 接口,发现有几次提示Blocked by Sentinel (flow limiting)

授权规则(黑白名单)

  1. 修改配置:修改sentinel-provider微服务的application.yml配置如下
server:
  port: 8001

spring:
  application:
    # 服务注册时使用的别名
    name: sentinel-provider
  profiles:
    active: test
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        file-extension: yaml
      discovery:
        # nacos的地址
        server-addr: localhost:8848
    sentinel:
      # 取消控制台懒加载
      eager: true
      transport:
        # 配置sentinel dashboard地址
        dashboard: localhost:8080
        # sentinel客户端启动的端口,默认8719端口(占用时会依次+1找未占用端口)
        port: 8719
      datasource:
        # 配置的数据源名称,可任意起名
        # 热点参数
        authority:
          # 数据源类型
          nacos:
            # nacos地址
            server-addr: ${spring.cloud.nacos.config.server-addr}
            # 配置文件名称
            data-id: ${spring.application.name}-authority
            # namespace(默认用public命名空间时不需要显示声明)和groupId
            group-id: DEFAULT_GROUP
            data-type: json
            rule-type: authority
  1. 实现接口:实现RequestOriginParser接口来判断请求来源
package learn.demo.config;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

@Component
public class SentinelOriginParser implements RequestOriginParser {
    @Override
    public String parseOrigin(HttpServletRequest request) {
        String source = request.getHeader("source");
        if (source==null||"".equals(source)) {
            source = "unknown";
        }
        return source;
    }
}
  1. 规则配置:在nacos中添加配置,dataId为sentinel-provider-authority,group为DEFAULT_GROUP,配置格式为JSON,配置内容如下
[
  {
    "resource": "/sentinel/provider/test",
    "limitApp": "test"
  }
]
  1. 测试:
    1. 使用api工具向localhost:8001/sentinel/provider/test 发送get请求,提示Blocked by Sentinel (flow limiting)
    2. 在请求头中添加key为source,value为test的参数后,再次发送请求,能正确看到响应信息

你可能感兴趣的:(分布式微服务,sentinel,分布式,微服务,后端,spring,cloud)