Sentinel使用Nacos持久化并动态更新配置

Sentinel使用Nacos持久化并动态更新配置

  • Sentinel注解限流
  • Sentinel Dashboard控制台限流
  • Nacos存储Sentinel配置
  • Nacos与Sentinel Dashboard动态更新配置
    • 推模式:使用 Nacos 配置规则

Sentinel注解限流

  • 引入pom
		
        <dependency>
            <groupId>com.alibaba.cspgroupId>
            <artifactId>sentinel-annotation-aspectjartifactId>
            <version>1.8.0version>
        dependency>
  • 全局配置
@Configuration
public class SentinelAutoConfiguration {
   
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
   
        return new SentinelResourceAspect();
    }
}
  • 对某个接口加上@SentinelResource注解
	@SentinelResource(value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback")
    @GetMapping("/sentinel")
    public String sentinel() {
   
        int i = (int) (Math.random() * 10);
        if (i % 2 == 0) {
   
            throw new RuntimeException("偶数异常");
        }
        return "hello world!!!";
    }

    // Fallback 函数,函数签名与原函数一致或加一个 Throwable 类型的参数.
    public String helloFallback(Throwable throwable) {
   
        throwable.printStackTrace();
        return "helloFallback";
    }

    // Block 异常处理函数,参数最后多一个 BlockException,其余与原函数一致.
    public String exceptionHandler(BlockException ex) {
   
        ex.printStackTrace();
        return "exceptionHandler";
    }
}
<

你可能感兴趣的:(架构,sentinel,Sentinel动态配置)