Sentinel 服务熔断 OpenFeign
Feign组件一般是消费侧
1.POM中增加openfeign引入
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud-nacos</artifactId> <groupId>com.ckfuture.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloudalibaba-consumer-nacos-order84</artifactId> <dependencies> <!--SpringCloud alibaba nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--SpringCloud alibaba sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!--SpringCloud openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--web+actuator--> <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> </dependencies> </project>
2.YML中激活Sentinel对Feign的支持
server: port: 84 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848 sentinel: transport: #配置Sentinel dashboard地址 dashboard: localhost:8080 port: 8719 #消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者) service-url: nacos-user-service: http://nacos-payment-provider #激活Sentinel 对Feign的支持 feign: sentinel: enabled: true
3.主启动类激活@EnableFeignClients
package com.ckfuture.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class OrderNacosMain84 { public static void main(String[] args) { SpringApplication.run(OrderNacosMain84.class,args); } }
4.新建带@FeignClient注解的业务接口
Feign就是接口+注解
接口中方法调用地址要和生产者中的调用地址一样。参考Feign调用方式
package com.ckfuture.springcloud.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** * @author ckfuture */ @FeignClient(value = "nacos-payment-provider") public interface PaymentService { @GetMapping(value = "/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id); }
5.创建Feign兜底类
创建名为“PaymentFallbackService”的类,实现PaymentService接口方法
package com.ckfuture.springcloud.service; import org.springframework.stereotype.Component; /** * Feign * 兜底类 */ @Component public class PaymentFallbackService implements PaymentService{ @Override public String getPayment(Integer id) { return "服务降级返回,---PaymentFallbackService"; } }
6.接口中添加fallback(兜底)方法
package com.ckfuture.springcloud.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** * @author ckfuture */ @FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class) public interface PaymentService { @GetMapping(value = "/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id); }
7.添加controller方法
//=============OpenFeign============ @Resource private PaymentService paymentService; @GetMapping(value = "/consumer/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id){ return paymentService.getPayment(id); }
8.测试
启动9001生产者微服务,在Nacos中查看。
启动84端口的消费者,在Nacos中查看
此时故意关闭9001端口微服务,再次请求查看效果。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!