爲何須要 Hystrix?html
hystrix主要是用來防止服務雪崩效應的。java
在微服務架構中,咱們將業務拆分紅一個個的服務,服務與服務之間能夠相互調用(RPC)。爲了保證其高可用,單個服務又必須集羣部署。因爲網絡緣由或者自身的緣由,服務並不能保證服務的100%可用,若是單個服務出現問題,調用這個服務就會出現網絡延遲,此時如有大量的網絡涌入,會造成任務累計,致使服務癱瘓,甚至致使服務「雪崩」。爲了解決這個問題,就出現斷路器模型。web
Hystrix 是一個幫助解決分佈式系統交互時超時處理和容錯的類庫, 它一樣擁有保護系統的能力.什麼是服務雪崩spring
分佈式系統中常常會出現某個基礎服務不可用形成整個系統不可用的狀況, 這種現象被稱爲服務雪崩效應. 爲了應對服務雪崩, 一種常見的作法是手動服務降級. 而Hystrix的出現,給咱們提供了另外一種選擇.緩存
1.流量控制網絡
2.改進緩存模式架構
3.服務自動擴容app
4.服務調用者降級服務分佈式
引用自:https://www.cnblogs.com/xyhero/p/53852cf0245c229fe3e22756a220508b.htmlide
pom:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
application.properties:
spring.application.name=hello-consumer-ribbon-hystrix
server.port=8041
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/
HelloService:
package cn.demo.service; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class HelloService { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "fallback", commandProperties = { @HystrixProperty(name= "execution.isolation.thread.timeoutInMilliseconds", value="2000") }) public String hello (String name) { return restTemplate.getForEntity("http://USER-SERVICE/hello?name=" + name, String.class).getBody(); } public String fallback (String name) { return "hello, hystrix熔斷啓動=== fail name:" + name; } }
HelloConsumerRibbonHystrixApplication:
package cn.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import cn.demo.service.HelloService; @SpringBootApplication @EnableDiscoveryClient @EnableHystrix @RestController public class HelloConsumerRibbonHystrixApplication { public static void main(String[] args) { SpringApplication.run(HelloConsumerRibbonHystrixApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate () { return new RestTemplate(); } @Autowired private HelloService helloService; @RequestMapping("hello") public String hello (String name) { return helloService.hello(name); } }
測試:
啓動eureka-server:8001, hello-service:8012,8002,hello-consumer-ribbon-hystrix:8041
訪問:http://localhost:8041/hello?name=ribbon_hystrix
返回:hello, ribbon_hystrix
訪問正常,接下來咱們把hello-service服務停了,再次訪問:
返回:hello, hystrix熔斷啓動=== fail name:ribbon_hystrix
------成功觸發熔斷。
而後咱們再次啓動hello-service服務,而後訪問:
hello, ribbon_hystrix
沒有觸發熔斷,正常。
一樣咱們測試訪問超時觸發熔斷的狀況,咱們在hello-service接口加上線程等待1s:
@RequestMapping("hello") public String hello (String name) throws InterruptedException { Thread.sleep(1000); System.out.println("hello, " + name); return "hello, " + name; }
訪問,發現一樣觸發熔斷,由於hystrix默認超時1s觸發熔斷,咱們能夠經過修改屬性來改變超時時間。
這裏咱們把超時時間修改成2s:
@HystrixCommand(fallbackMethod = "fallback", commandProperties = { @HystrixProperty(name= "execution.isolation.thread.timeoutInMilliseconds", value="2000") }) public String hello (String name) { return restTemplate.getForEntity("http://HELLO-SERVICE/hello?name=" + name, String.class).getBody(); }
再次訪問,發現沒有觸發熔斷。
Feign默認是自帶Hystrix的,因此依賴Jar的時候無需再依賴hystrix
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
application.properties:
spring.application.name=hello-consumer-feign-hystrix
server.port=8051
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/
## 開啓hystrix
feign.hystrix.enabled=true
## hystrix熔斷觸發默認超時時間
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000
HelloService:
import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "hello-service", fallback = HelloServiceFallBack.class) public interface HelloService { @RequestMapping("hello") String hello(@RequestParam(value = "name") String name) ; }
熔斷觸發類HelloServiceFallBack:
import org.springframework.stereotype.Component;
@Component
public class HelloServiceFallBack implements HelloService{
@Override
public String hello(String name) {
return "hello, hystrix 觸發熔斷 == fail name : " + name;
}
}
啓動類:
import cn.saytime.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @RestController public class HelloConsumerFeignHystrixApplication { public static void main(String[] args) { SpringApplication.run(HelloConsumerFeignHystrixApplication.class, args); } @Autowired private HelloService helloService; @RequestMapping("hello") public String hello(String name){ return helloService.hello(name); } }
測試:
啓動eureka-server:8001, hello-service:8011,hello-consumer-feign-hystrix:8051
訪問:http://localhost:8051/hello?name=feign_hystrix
接下來關閉hello-service服務,再次訪問:
將hello-service服務從新啓動,訪問正常,沒有觸發熔斷。
將hello-service服務接口加上線程等待3s,重啓hello-service服務,再次調用,一樣成功觸發熔斷
修改application.properties裏面熔斷超時時間爲4s,再次調用,沒有觸發熔斷。
ribbon-hystrix 與 feign-hystrix 兩個項目的pom文件都添加如下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>
啓動類都加上以下註解: @EnableHystrixDashboard
而後訪問
http://localhost:8041/hystrix
http://localhost:8051/hystrix
輸入連接:http://localhost:8041/hystrix.stream
同理,若是是feign-hystrix項目,輸入 http://localhost:8051/hystrix.stream
點擊Monitor Stream
而後咱們訪問一下 http://localhost:8041/hello?name=ribbon_hystrix
會出現以下監控頁面:
這個微服務監控如何查看監控結果。
圖中實心圓共有兩種含義,他經過顏色的變化表明了實例的健康程度,它的健康度顏色從綠色<黃色<橙色<紅色遞減。
該實心圓除了顏色的變化以外,它的大小也會根據實例的請求流量發生變化,流量越大該實心圓就越大。因此經過該實心圓的展現能夠在大量的實例中快速的發現故障實例和高壓力實例。