zoukankan      html  css  js  c++  java
  • Feign使用Hystrix

    转自:https://www.cnblogs.com/linjiqin/p/10195442.html

    Feign使用Hystrix开发步骤

    1、导入依赖spring-cloud-starter-hystrix

    2、消费启动类开启@EnableCircuitBreaker

    3、配置yml文件feign.hystrix.enabled=true

    4、实现FeignClient接口或FallbackFactory接口
    4.1、实现FeignClient接口
    4.2、实现FallbackFactory接口

    5、@FeignClient注解配置fallback参数

    1、导入依赖spring-cloud-starter-hystrix

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>

    2、消费启动类开启@EnableCircuitBreaker

    复制代码
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients
    @EnableHystrix //开启Hystrix支持
    public class RibbonConsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(RibbonConsumerApplication.class, args);
        }
        
    }
    复制代码

    3、配置yml文件feign.hystrix.enabled=true

    server:
      port: 8808
    feign: 
      hystrix:
        #开启feign的hystrix支持,默认是false 
        enabled: true

    4、实现FeignClient接口或FallbackFactory接口
    4.1、实现FeignClient接口
    TestClient1.java

    复制代码
    //fallback 指定一个回退类,这个类必须实现@FeignClient声明的接口,并且在spring context中
    @FeignClient(value="cloud-producer",
        configuration=FeignClientsConfiguration.class,
        fallback=TestClient1FallBack.class)
    public interface TestClient1 {
        
            @GetMapping(value = "/get/{id}")
            public String get(@PathVariable("id") String id);
    
            @RequestMapping(value = "/getuser/{id}")
            public User getUser(@PathVariable("id") String id);
            
            @RequestMapping(value = "/getuser2", method = RequestMethod.GET)
            public User getUser2(User user);
    
            @RequestMapping(value = "/postuser")
            public User postUser(@RequestBody User user);
    
            @RequestMapping(value = "/postuser")
            public User postUser2(User user);
    
            @RequestMapping(value = "/postuser", method = RequestMethod.GET)
            public User postUser3(User user);
    
            @GetMapping(value = "/listAll")
            List<User> listAll();
        
    }
    复制代码


    TestClient1FallBack.java

    复制代码
    @Component
    public class TestClient1FallBack implements TestClient1 {
        @Override
        public String get(String id) {
            return "feign hystrix fallback for get method";
        }
    
        @Override
        public User getUser(String id) {
            User errorUser = new User();
            errorUser.setId("getUser.errorId");
            errorUser.setName("getUser.errorName");
            return errorUser;
        }
    
        @Override
        public User getUser2(User user) {
            User errorUser = new User();
            errorUser.setId("getUser2.errorId");
            errorUser.setName("getUser2.errorName");
            return errorUser;
        }
    
        @Override
        public User postUser(User user) {
            User errorUser = new User();
            errorUser.setId("postUser.errorId");
            errorUser.setName("postUser.errorName");
            return errorUser;
        }
    
        @Override
        public User postUser2(User user) {
            User errorUser = new User();
            errorUser.setId("postUser2.errorId");
            errorUser.setName("postUser2.errorName");
            return errorUser;
        }
    
        @Override
        public User postUser3(User user) {
            User errorUser = new User();
            errorUser.setId("postUser3.errorId");
            errorUser.setName("postUser3.errorName");
            return errorUser;
        }
    
        @Override
        public List<User> listAll() {
            User errorUser = new User();
            errorUser.setId("listAll.errorId");
            errorUser.setName("listAll.errorName");
            ArrayList<User> list = new ArrayList<User>();
            list.add(errorUser);
            return list;
        }
    }
    复制代码


    Test1Controller.java

    复制代码
    @RestController
    public class Test1Controller {
        
        @Autowired
        private TestClient1 testClient1;
        
        @GetMapping("/feign1/get/{id}")
        public String get(@PathVariable String id) {
            String result = testClient1.get(id);
            return result;
        }
        
        @GetMapping("/feign1/getuser/{id}")
        public User getUser(@PathVariable String id) {
            User result = testClient1.getUser(id);
            return result;
        }
        
        @GetMapping("/feign1/getuser2")
        public User getUser2(User user) {
            User result = testClient1.getUser2(new User());
            return result;
        }
        
        @GetMapping("/feign1/listAll")
        public List<User> listAll() {
            return testClient1.listAll();
        }
        
        @PostMapping("/feign1/postuser")
        public User postUser(@RequestBody User user) {
            User result = testClient1.postUser(user);
            return result;
        }
        
        @PostMapping("/feign1/postuser2")
        public User postUser2(@RequestBody User user) {
            User result = testClient1.postUser2(user);
            return result;
        }
        
        @PostMapping("/feign1/postuser3")
        public User postUser3(@RequestBody User user) {
            User result = testClient1.postUser3(user);
            return result;
        }
        
    }
    复制代码

    4.2、实现FallbackFactory接口
    TestClient3.java

    复制代码
    //fallbackFactory 指定一个fallback工厂,与指定fallback不同, 此工厂可以用来获取到触发断路器的异常信息,TestClientFallbackFactory需要实现FallbackFactory类
    @FeignClient(value="mima-cloud-producer",configuration=FeignClientsConfiguration.class,fallbackFactory=TestClien3tFallbackFactory.class)
    public interface TestClient3 {
        
        @RequestMapping(value="/get/{id}",method=RequestMethod.GET)
        public String get(@PathVariable("id") String id);
        
        @RequestMapping(value = "/getuser/{id}")
        public User getUser(@PathVariable("id") String id);
        
    }
    复制代码


    TestClien3tFallbackFactory.java——优点是可以获取到异常信息

    复制代码
    //FallbackFactory的优点是可以获取到异常信息
    @Component
    public class TestClien3tFallbackFactory implements FallbackFactory<TestClient3> {
    
        @Override
        public TestClient3 create(Throwable cause) {
            return new TestClient3() {
                @Override
                public String get(String id) {
                    return "get trigger hystrix open! reason:"+cause.getMessage();
                }
    
                @Override
                public User getUser(String id) {
                    User errorUser = new User();
                    errorUser.setId("getUser.errorId");
                    errorUser.setName("getUser.errorName");
                    return errorUser;
                }
            };
        }
        
    }
    复制代码


    Test3Controller.java

    复制代码
    @RestController
    public class Test3Controller {
        
        @Autowired
        private TestClient3 testClient3;
        
        @GetMapping("/feign3/get/{id}")
        public String get(@PathVariable String id) {
            String result = testClient3.get(id);
            return result;
        }
        
        @GetMapping("/feign3/getuser/{id}")
        public User getuser(@PathVariable String id) {
            User result = testClient3.getUser(id);
            return result;
        }
        
    }
    复制代码

    5、@FeignClient注解配置fallback参数

  • 相关阅读:
    WEB服务-Nginx之10-动静分离
    第10课 文件指针及目录的创建与删除
    c++ 中常用类型转换
    编译c++文件时报错:在...中已定义,例如:已经在 .obj 中定义
    No converter found for return value of type: class java.util.ArrayList
    Unable to ping server at localhost:1099
    Failed building wheel for twisted
    第六天-缺陷和缺陷报告
    第五天-黑盒测试用例设计方法(二)
    第四天-测试用例和设计方法(一)
  • 原文地址:https://www.cnblogs.com/sharpest/p/13712661.html
Copyright © 2011-2022 走看看