ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Sentinel 之 整合SpringCloud

2021-10-31 01:35:14  阅读:176  来源: 互联网

标签:feignHello return SpringCloud sentinel 限流 整合 Sentinel public


Spring Cloud Alibaba Sentinel 是阿里巴巴提供的,致力于提供微服务一站式解决方案,Spring Cloud Alibaba 默认为 Sentinel 整合了,ServeLet、RestTemplate、FeignClient 和 Spring Flux。在 Spring 的生态中不仅不全了 Hystrix 在 ServeLet 和 RestTemplate 这一块的空白,而且还完美的兼容了 Hystrix 在 Feign 中的限流降级用法,并支持运行时灵活的配置和调整限流降级规则。

引入依赖:

<!--Sentinel 整合SpringCloud 的依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

配置文件:

(入门使用中,应用名称使用的 JVM 参数设置的,整合 SpringCloud 就不需要那样了,配置文件中配置了应用的名称后,Sentinel 会自动加载)

# 设置应用的名称
spring:
  application:
    name: springCloudSentinel
  cloud:
    sentinel:
      transport:
 
        #设置Sentinel控制台的主机地址和端口号
        dashboard: localhost:9000

编写测试 Controller ,控制台添加 Sentinel_Cloud 资源 限流测试

@SentinelResource(value = "Sentinel_Cloud",blockHandler = "exceptionHandler")
@GetMapping("/sentinelCloud")
public String sentinelCloud(){
    //使用限流规则
    return "Sentinel_Cloud,成功调用";
}

限流时调用的方法:

/**
 * 定义降级 / 限流 的处理函数
 *
 * @param exception
 * @return
 */
public String exceptionHandler(BlockException exception) {
    exception.printStackTrace();
    return "Sentinel_Cloud,访问限流";
}

Sentinel整合Feign (OpenFeign)

Sentinel适配了Feign组件。如果想要使用,除了引用spring-cloud-starter-alibaba-sentinel的依赖,还需要两个步骤:

 

配置打开Sentinel对Feign的支持:feign.sentinel.enable=true

加入spring-cloud-starter-openfeign依赖使Sentinel starter自动化配置类生效。

# 设置应用的名称
spring:
  application:
    name: springCloudSentinel
  cloud:
    sentinel:
      transport:
 
        #设置Sentinel控制台的主机地址和端口号
        dashboard: localhost:9000
 
# 开启 Sentinel 对 Feign 的支持
feign:
  sentinel:
    enabled: true

服务端调用方Controller

@GetMapping("/feignHello")
public String feignHello(){
    return feignClient.feignHello();
}

服务提供方 FeignClient

@FeignClient(contextId = "testFeignClient", value = "注册中心中服务的名称", fallback = FeignFallbackService.class)
public interface TestFeignClient {
 
   /**
    * OpenFeign 远程调用的方法
    *
    * @return
    */
   @GetMapping("/test/feignHello")
   String feignHello();
}

提供一个 FeignClient 接口的实现类,作为限流的处理方法

@Service
public class FeignFallbackService  implements TestFeignClient{
   @Override
   public String feignHello() {
      return "Feign 远程调用限流了";
   }
}

Sentinel 控制台添加限流规则:

请求方式:http://服务模块注册中心名称/test/feignHello

 

>>>>>>>>>>  接下一篇学习:https://www.cnblogs.com/Alay/p/15488123.html  <<<<<<<<<<<<

 

标签:feignHello,return,SpringCloud,sentinel,限流,整合,Sentinel,public
来源: https://www.cnblogs.com/Alay/p/15488116.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有