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

Hystrix主要用于實現微服務體系中斷路器的作用。往往與spring cloud集成使用。但絕大部分的項目現在還沒有完全移植到spring cloud環(huán)境中。所以了解其獨立使用的方式也很必要,好在Netflix出品,必屬精品。開發(fā)過程中使用起來也比較簡便。

以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,用于收集監(jiān)控信息。

被Hystrix監(jiān)控類

@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進行業(yè)務訪問。另外可以通過http://主機/項目名/hystrix.stream查看調用事件。由于hystrix.stream以json格式輸出,可讀性較差,所以需要其他方案以便更優(yōu)雅的展示監(jiān)控結果。

https://github.com/kennedyoliveira/standalone-hystrix-dashboard就是一個非常不錯的項目,將hystrix.stream產生的json以圖形化的形式展現。

在git下載該項目并運行后,配置監(jiān)控url地址即可直觀的查看被@HystrixCommand作用調用情況。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發(fā)現,斷路器,智...
    卡卡羅2017閱讀 136,680評論 19 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,283評論 6 342
  • spring-boot-admin為我們基于spring-boot的基礎數據安全端口提供了基礎的可視化監(jiān)控功能。還...
    Comcen閱讀 10,589評論 8 22
  • 研究了一段時間spring boot了準備向spirng cloud進發(fā),公司架構和項目也全面擁抱了Spring ...
    南山羊閱讀 5,187評論 2 37
  • 為夢想,為生活,你我會不知不覺地走進孤獨。 孤獨讓你放大了一個人的夜和冷,讓你委屈、不安,讓你壓抑、放縱。但,孤獨...
    逆流前行閱讀 198評論 0 0

友情鏈接更多精彩內容