1. 程式人生 > >Feign Hystrix 支援

Feign Hystrix 支援

如果Hystrix在類路徑上,feign.hystrix.enable=true,Feign將用斷路器包裝所有方法。

Hystrix支援回退的概念:當電路斷開或出現錯誤時執行的預設程式碼路徑。要為給定的@FeignClient啟用回退,請將fallback屬性設定為實現回退的類名。

示例程式碼:

這裡寫圖片描述

UserFeignClientHystrix 需要實現UserFeignClient

UserFeignClient.java

@FeignClient(name = "springcloud-userservcie", fallback = UserFeignClientHystrix.class)
public
interface UserFeignClient { @RequestMapping(value = "userService/getUser/{userId}", method = RequestMethod.GET) public User getUser(@PathVariable("userId") String userId); @RequestMapping(value = "userService/saveUser", method = RequestMethod.POST) public String saveUser(User user); @RequestMapping(value
= "userService/deleteUser/{userId}", method = RequestMethod.GET) public Boolean deleteUser(@PathVariable("userId") String userId); @RequestMapping(value = "userService/findUserByUserNameAndPassword", method = RequestMethod.GET) public User findUserByUserNameAndPassword(@RequestParam("userName") String userName, @RequestParam
("password") String password); }

這裡寫圖片描述

UserFeignClientHystrix.java

@Component
public class UserFeignClientHystrix implements UserFeignClient {

    @Override
    public User getUser(String userId) {
        System.out.println("打開了斷路器--->" + userId);
        return null;
    }

    @Override
    public String saveUser(User user) {
        return null;
    }

    @Override
    public Boolean deleteUser(String userId) {
        return null;
    }

    @Override
    public User findUserByUserNameAndPassword(String userName, String password) {
        return null;
    }

}

這裡寫圖片描述