一,pom文件引入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
二 、配置yml
server:
port: 8871
eureka:
client:
service-url:
# 服务注册地址
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: order-service
feign:
hystrix:
#重点 允许熔断
enabled: true
client:
config:
default:
# 超时
connectTimeout: 2000
readTimeout: 2000
三、启动类添加注释 启用熔断
@EnableCircuitBreaker
四、测试接口
注意:Save 和熔断访问接口参数必须一致SaveFallbackMethod
@GetMapping("/mySave") @HystrixCommand(fallbackMethod = "SaveFallbackMethod") public Object Save(@RequestParam("userID") int userID, @RequestParam("productID") int productID){ Map<String,Object> data=new HashMap<>(); data.put("code","0"); data.put("msg","all this is Ok"); data.put("data",service.save(userID,productID)); return data; } public Object SaveFallbackMethod(@RequestParam("userID") int userID, @RequestParam("productID") int productID){ Map<String,Object> data=new HashMap<>(); data.put("code",-1); data.put("msg","too many people connect!"); return data; }