美文网首页springcloud学习笔记
SpringCloud学习六:Hystrix断路器客户端

SpringCloud学习六:Hystrix断路器客户端

作者: Bertram_Wang | 来源:发表于2019-03-28 14:59 被阅读0次

    服务注册到注册中心后,被消费着调用。服务宕机后消费者不能及时的排除服务。还是会分发请求到该服务,到时出现了链接异常。

    错误页面

    返回一个不可控的错误信息,当遇到这种情况我们希望能返回一个可控的错误信息,这时候就需要添加断路器;
    引用文:
    要在项目中包含Hystrix,请使用组org.springframework.cloud和artifact id spring-cloud-starter-netflix-hystrix的启动器。
    spring-cloud-api项目:pom.xml文件添加依赖:

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

    启动类添加注解@EnableCircuitBreaker;全局启用断路器。
    源码如下:

    /**
     * Annotation to enable a CircuitBreaker implementation.
     * http://martinfowler.com/bliki/CircuitBreaker.html
     * @author Spencer Gibb
     */
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @Import(EnableCircuitBreakerImportSelector.class)
    public @interface EnableCircuitBreaker {
    
    }
    

    添加类:

    package bertram.springcloud.study.api.service.impl;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.web.client.RestTemplate;
    
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    
    import bertram.springcloud.study.api.service.HelloService;
    
    /**
     * <p> 断路器测试<p>
     * @Author Bertram.Wang
     * @Date 2019年3月19日
     */
    @Component
    public class HelloServiceImpl implements HelloService {
        private static final Logger log = LoggerFactory.getLogger(HelloServiceImpl.class);
        @Autowired
        private RestTemplate restTemplate;
        
        @Override
        @HystrixCommand(fallbackMethod = "requestGetFallback")
        public String requestGet(String url) {
            return restTemplate.getForEntity(url, String.class).getBody();
        }
        
        @SuppressWarnings("unused")
        private String requestGetFallback(String url, Throwable throwable) {
            log.error(url);
            log.error(throwable.getMessage());
            return "error:系统繁忙";
        }
    }
    

    调整控制器hello方法:

    @GetMapping("/hello")
    public String hello() {
        log.info("========:api 执行hello===========");
        String url = "http://SPRING-CLOUD-NETFLIX-EUREKA-SERVERONE/serverone/hello";
        return helloService.requestGet(url);
    }
    

    启动项目测试:访问已关闭的服务时,提示指定的错误信息。

    测试

    @EnableCircuitBreaker注解不只可以加在启动类上。也可以添加在具体的实现类上
    示例:

    @Component
    // 开启断路器
    @EnableCircuitBreaker
    public class HelloServiceImpl implements HelloService{
        ......
    }
    

    或者不使用注解方式 使用自定义继承类HystrixCommand写法示例:

    package bertram.springcloud.study.api.service.impl;
    
    import org.springframework.web.client.RestTemplate;
    
    import com.netflix.hystrix.HystrixCommand;
    
    /**
     * <p> 自定义<p>
     * @Author Bertram.Wang
     * @Date 2019年3月19日
     */
    public class HelloServiceImpl2 extends HystrixCommand<String> {
    
        private RestTemplate restTemplate;
        
        public HelloServiceImpl2(Setter setter, RestTemplate restTemplate) {
            super(setter);
            this.restTemplate = restTemplate;
        }
    
        @Override
        protected String run() throws Exception {
            return restTemplate.getForObject("http://SPRING-CLOUD-NETFLIX-EUREKA-SERVERONE/serverone/hello", String.class);
        }
    
        @Override
        protected String getFallback() {
            return "错误:网络异常";
        }
    }
    

    相关文章

      网友评论

        本文标题:SpringCloud学习六:Hystrix断路器客户端

        本文链接:https://www.haomeiwen.com/subject/cxmamqtx.html