其他分享
首页 > 其他分享> > Sentinel 初级使用

Sentinel 初级使用

作者:互联网

1. Sentinel 的组成

1.控制台启动

  1. https://github.com/alibaba/Sentinel/releases 下载 sentinel-dashboard-1.7.0.jar 到本地

  2. 运行:java -jar sentinel-dashboard-1.7.0.jar

  3. 访问:http://localhost:8080

  4. 初始账号密码:sentinel

  5. 启动 nacos (负责 Sentinel 规则持久化)

2. 创建新项目测试

  1. 创建 Module:cloudalibaba-sentinel-service8401

  2. POM

<dependencies>
	<dependency>
		<groupId>com.atguigu</groupId>
		<artifactId>cloud-api-commons</artifactId>
		<version>${project.version}</version>
	</dependency>
	<!-- Nacos -->
	<dependency>
		<groupId>com.alibaba.cloud</groupId>
		<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
	</dependency>
	<!-- 负责 sentinel 持久化 -->
	<dependency>
		<groupId>com.alibaba.csp</groupId>
		<artifactId>sentinel-datasource-nacos</artifactId>
	</dependency>
	
	<!-- sentinel -->
	<dependency>
		<groupId>com.alibaba.cloud</groupId>
		<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
	</dependency>
	<!-- OpenFeign -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-openfeign</artifactId>
	</dependency>
	<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>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<scope>runtime</scope>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>cn.hutool</groupId>
		<artifactId>hutool-all</artifactId>
		<version>4.6.3</version>
	</dependency>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>
  1. YML
server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8080 # 控制台地址
        port: 8719  #默认8719,应用与Sentinel控制台交互的端口,应用本地会起一个该端口占用HttpServer

management:
  endpoints:
    web:
      exposure:
        include: '*'

注:这里的 spring.cloud.sentinel.transport.port 端口配置会在应用对应的机器上启动一个 Http Server ,该 Server 会与 Sentinel 控制台做交互。比如 Sentinel 控制台添加了一个限流规则,会把规则数据 push 给这个 Http Server 接收,Http Server 再将规则注册到 Sentinel 中。

  1. 主启动
@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401{
    public static void main(String[] args) {
        SpringApplication.run(MainApp8401.class, args);
    }
}
  1. 业务类
@RestController
@Slf4j
public class FlowLimitController{
    @GetMapping("/testA")
    public String testA() {
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "------testB";
    }
}
  1. 启动

注:Sentinel 采用的懒加载模式,我们需要访问一次,sentinel 才会加载监控的业务

2. 流控规则

博客:
sentinel流控模式:链路模式失效问题
Spring Cloud Alibaba:Sentinel 流控规则

1. 直接:达到条件直接失败

注:上述配置表示,QPS,即每秒访问次数超过 1 次,就直接快速失败,报默认错误;
注:上述配置表示,每秒线程数超过 1,就直接快速失败,报默认错误;

2. 关联:关联的微服务达到条件时,指定资源失败;

3. 链路

链路流控模式指的是,当从某个接口过来的资源达到限流条件时,开启限流。它的功能有点类似于针对来源配置项,区别在于:针对来源是针对上级微服务,而链路流控是针对上级接口,也就是说它的粒度更细。

下面建议将 spring-cloud-alibaba 的版本改为 2.1.1.RELEASE

@Service
public class FlowControlServiceImpl implements FlowControlService {

    @Override
    @SentinelResource(value = "testC")      // Sentinel 默认会将 controller 映射为资源名,我们也可以用此注解表明资源名
    public void testC() {
        System.out.println("------testC");
    }
    public void testC_Handler(){
        System.out.println("------testC_Handler");
    }
}
@RestController
@Slf4j
public class FlowLimitController{

    @Resource
    private FlowControlService flowControlService;

    @GetMapping("/testA")
    public String testA() {
        flowControlService.testC();
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        flowControlService.testC();
        return "------testB";
    }
}
spring:
    cloud:
        sentinel:
            filter:
                enabled: false
@Configuration
public class FilterContextConfig {

    @Bean
    public FilterRegistrationBean sentinelFilterRegistration() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new CommonFilter());
        registrationBean.addUrlPatterns("/*");
        // 入口资源关闭聚合
        registrationBean.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false");
        registrationBean.setName("sentinelFilter");
        registrationBean.setOrder(1);

        return registrationBean;
    }
}

标签:spring,testC,sentinel,初级,使用,Sentinel,public,cloud
来源: https://www.cnblogs.com/ayiblogs/p/16322509.html