在项目中快速加入Netflix Hystrix特性

Hystrix主要用于实现微服务体系中断路器的作用。往往与spring cloud集成使用。但绝大部分的项目现在还没有完全移植到spring cloud环境中。所以了解其独立使用的方式也很必要,好在Netflix出品,必属精品。开发过程中使用起来也比较简便。

以spring boot项目加入Hystrix为例,看看如果快速的项目中使用Hystrix特性。

maven依赖

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
<version>1.5.9</version>
</dependency>

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-metrics-event-stream</artifactId>
    <version>1.5.9</version>
</dependency>

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-javanica</artifactId>
    <version>1.5.9</version>
</dependency>

spring boot配置类

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;

@Configuration
public class HystrixConfiguration {

    @Bean
    public HystrixCommandAspect hystrixAspect() {
        return new HystrixCommandAspect();
    }

    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registration.addUrlMappings("/hystrix.stream");
        return registration;
    }
}

声明一个HystrixCommandAspect代理类,用于以后被Hystrix相关注解的切面。另外声明一个Servlet,用于收集监控信息。

被Hystrix监控类

@RestController
@RequestMapping({ "/test" })
public class UserController {
    
    @Autowired
    private RemoteService remoteService;
    
    @RequestMapping(value = "/user")
    @HystrixCommand(fallbackMethod = "fallback", threadPoolProperties = {  
            @HystrixProperty(name = "coreSize", value = "30"), @HystrixProperty(name = "maxQueueSize", value = "100"),  
            @HystrixProperty(name = "queueSizeRejectionThreshold", value = "20") }, commandProperties = {  
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),  
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "1")  
  
    })
    public User getUser1() throws InterruptedException{
        User user = remoteService.getUser1();
        return user;
    }

    public User fallback(Throwable e) {  
        e.printStackTrace();
        return new User();  
    } 
}

通过@HystrixCommand设定断路策略,各配置具体作用参考官方解释。

项目运行之后可以通过http://主机/项目名/test/user进行业务访问。另外可以通过http://主机/项目名/hystrix.stream查看调用事件。由于hystrix.stream以json格式输出,可读性较差,所以需要其他方案以便更优雅的展示监控结果。

https://github.com/kennedyoliveira/standalone-hystrix-dashboard就是一个非常不错的项目,将hystrix.stream产生的json以图形化的形式展现。

在git下载该项目并运行后,配置监控url地址即可直观的查看被@HystrixCommand作用调用情况。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,992评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,972评论 6 342
  • spring-boot-admin为我们基于spring-boot的基础数据安全端口提供了基础的可视化监控功能。还...
    Comcen阅读 10,490评论 8 22
  • 研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring ...
    南山羊阅读 5,107评论 2 37
  • 为梦想,为生活,你我会不知不觉地走进孤独。 孤独让你放大了一个人的夜和冷,让你委屈、不安,让你压抑、放纵。但,孤独...
    逆流前行阅读 190评论 0 0