Hystrix熔断整合完成

This commit is contained in:
YoVinchen 2023-08-15 20:21:27 +08:00
parent fdbe9cbe62
commit 8d136d0f68
2 changed files with 12 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
/** /**
@ -13,6 +14,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
* @author yovinchen * @author yovinchen
* @Create 2023/8/14 16:25 * @Create 2023/8/14 16:25
*/ */
@EnableHystrix //启用Hystrix
@SpringBootApplication @SpringBootApplication
@EnableFeignClients @EnableFeignClients
public class BorrowApplication { public class BorrowApplication {

View File

@ -1,5 +1,6 @@
package com.test.controller; package com.test.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.test.entity.UserBorrowDetail; import com.test.entity.UserBorrowDetail;
import com.test.service.BorrowService; import com.test.service.BorrowService;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@ -8,6 +9,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Collections;
/** /**
@ -23,8 +25,16 @@ public class BorrowController {
@Resource @Resource
BorrowService service; BorrowService service;
@HystrixCommand(fallbackMethod = "onError") //使用@HystrixCommand来指定备选方案
@RequestMapping("/borrow/{uid}") @RequestMapping("/borrow/{uid}")
UserBorrowDetail findUserBorrows(@PathVariable("uid") int uid){ UserBorrowDetail findUserBorrows(@PathVariable("uid") int uid){
return service.getUserBorrowDetailByUid(uid); return service.getUserBorrowDetailByUid(uid);
} }
//备选方案这里直接返回空列表了
//注意参数和返回值要和上面的一致
UserBorrowDetail onError(int uid){
System.out.println("执行补救措施");
return new UserBorrowDetail(null, Collections.emptyList());
}
} }