

Sentinel整合Feign对远程调用限流并降级
source link: https://blog.51cto.com/lianghecai/5755864
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sentinel整合Feign对远程调用限流并降级
推荐 原创微服务提供者demo-pay
第一步:创建模块demo-pay添加依赖:
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
第二步:修改application.yml:
server:
port: 8801
servlet:
context-path: /pay
spring:
application:
name: nacos-pay
cloud:
nacos:
discovery:
server-addr: http://localhost:8848 # 配置Nacos地址
management:
endpoints:
web:
exposure:
include: '*' # 对外暴露出所有的端点
第三步:项目主启动类添加注解
@EnableDiscoveryClient
第四步:提供控制器PayController:
@RestController
public class PayController {
@GetMapping("/fun/{id}")
public String fun(@PathVariable("id") Long id) {
System.out.println("1111111111111111");
return "支付 id: " + id;
}
}
微服务消费者demo-sentinel-openfeign
第一步:创建模块demo-sentinel-openfeign并添加依赖:
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--避免出现警告信息:Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.-->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.1.1</version>
</dependency>
<!-- nacos自从2020版本之后不再整合的是Netflix,也就没有ribbon了 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.1</version>
</dependency>
第二步:修改application.yml
server:
port: 6601
servlet:
context-path: /sentinelfeign
spring:
application:
name: sentinel-feign
cloud:
nacos:
discovery:
server-addr: localhost:8848 # 配置nacos地址
sentinel:
transport:
# 配置sentinel dashboard地址
dashboard: localhost:8080
# 默认端口8719,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
management:
endpoints:
web:
exposure:
include: '*' #暴露出所有的端点
# 激活Sentinel对Feign的支持
feign:
sentinel:
enabled: true
第三步:在项目主启动类上添加注解:
@EnableFeignClients
@EnableDiscoveryClient
第四步:创建Feign接口:
@Service
//name:指定调用Rest接口所对应的服务名
//path:指定要调用的Rest接口所在的Controller指定的RequestMapping,如果Rest接口所在的Controller没有指定RequestMapping,则不用指定
@FeignClient(name = "nacos-pay", path = "/pay", fallback = PayFeignServiceFallback.class)
public interface PayFeignService {
@GetMapping("/fun/{id}")
public String fun(@PathVariable("id") Long id);
}
第五步:创建兜底类:
@Component
public class PayFeignServiceFallback implements PayFeignService {
@Override
public String fun(Long id) {
return "兜底 服务降级: " + id;
}
}
第六步:创建测试Controller
@RestController
public class DemoCtroller {
@Resource
private PayFeignService payFeignService;
@GetMapping("/fun1")
public String fun1(Long a) {
String res = payFeignService.fun(a);
return "fun1 "+res;
}
}
第一步:依次启动软件Nacos和Sentinel,然后再分别启动项目demo-pay和demo-sentinel-openfeign。
第二步:结果:

添加流控规则:

请求消费者中的示例控制器中的api接口:
低频访问:

高频访问:

- 打赏
- 赞
- 收藏
- 评论
- 分享
- 举报
Recommend
-
69
-
33
限流、熔断与降级 限流、熔断与降级,此三者都是流量过大时,通过一定的方式去保护系统的手段,是应对海量服务的三大“神器”
-
8
Sentinel进阶之熔断降级 | 南国薏米 ...
-
6
Spring Cloud 整合 Feign 的原理 发表于 2021-07-04 | 分类于 Java , Feign
-
8
LocalDateTime Feign 调用序列化失败 ...
-
5
最近在学习如何使用springcloud,当学习到跨服务调用接口时接触到Feign和Ribbon,网上有
-
8
Sentinel流控和降级方案 发表于 2020-04-14 ...
-
7
V2EX › 问与答 docker 容器化部署与 feign 服务调用问题
-
9
在我们公司里,不同的服务之间通过Feign进行远程调用,但是,我们在尝试使调用可重试时遇到了一个小问题,Feign框架本身可以配置的自己的重试机制,但是它是一刀切的方式,所有的调用都是同样的机制,没有办法像我们希望的那样在每个方法...
-
7
简化本地Feign调用,老手教你这么玩 作者:Dr Hydra 2023-06-09 13:56:42 本文提供了一个在本地开发过程中简化Feign调用的思路,相比之前需要麻烦的修改FeignClient中的url而言,能够节省不少的无效劳动,并且通过...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK