您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Spring Cloud 中Hystrix有什么用,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.
Netflix創(chuàng)建了一個(gè)名為Hystrix的庫,該庫實(shí)現(xiàn)了斷路器模式。在微服務(wù)架構(gòu)中,通常有多個(gè)服務(wù)調(diào)用層。
較底層的服務(wù)如果出現(xiàn)故障,會(huì)導(dǎo)致連鎖故障。當(dāng)對(duì)特定的服務(wù)的調(diào)用的不可用達(dá)到一個(gè)閥值(Hystric 是5秒20次) 斷路器將會(huì)被打開。
斷路打開后,可用避免連鎖故障,fallback方法可以直接返回一個(gè)固定值。
在之前工程的基礎(chǔ)上, 啟動(dòng)eureka-server,端口為9090;啟動(dòng)eureka-client, 端口為8040。
改造rebbon-service
工程的代碼,首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix
的起步依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
在項(xiàng)目啟動(dòng)類上注解@EnableHystrix, 開啟斷路器能力:
@EnableEurekaClient @SpringBootApplication @EnableHystrix public class RibbonServiceApplication { public static void main(String[] args) { SpringApplication.run(RibbonServiceApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
改造HelloController
類, 在hello
方法加上@HystrixCommand
注解。 該注解給方法低通了熔斷器的能力, 指定fallbackMethod
熔斷方法, 當(dāng)遠(yuǎn)程服務(wù)調(diào)用時(shí)候后執(zhí)行熔斷方法:
@RestController public class HelloController { private final RestTemplate restTemplate; @Autowired public HelloController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @HystrixCommand(fallbackMethod = "helloError") @GetMapping("/hello") public String hello(@RequestParam("name") String name) { return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class); } public String helloError(String name) { return String.format("Hello, %s! Access remote service fail!", name); } }
啟動(dòng)ribbon-service項(xiàng)目,在瀏覽器訪問 http://localhost:8050/hello?name=Mars:
Hello, My name is Mars, I'm from port: 8040
這時(shí)候我們關(guān)閉eureka-service項(xiàng)目, 再次訪問 http://localhost:8050/hello?name=Mars:
Hello, Mars! Access remote service fail!
這就說明eureka-service服務(wù)不可達(dá)時(shí), ribbon-service調(diào)用接口會(huì)快速失敗, 直接調(diào)用熔斷方法, 而不是等待響應(yīng)超時(shí), 這很好的控制了容器的線程阻塞。
Feign已經(jīng)集成了斷路器, 基于feign-service項(xiàng)目進(jìn)行改造, 只需要在@FeignClient注解中加上fallback
的指定類就行:
@FeignClient(value = "hello-eureka-client", fallback = FeignServiceHystrix.class) public interface FeignService { @GetMapping(value = "/hello") String hello(@RequestParam(value = "name") String name); }
FeignServiceHystrix
需要實(shí)現(xiàn)FeignService
接口,并注入到Ioc容器中:
@Component public class FeignServiceHystrix implements FeignService { @Override public String hello(String name) { return String.format("Hello, %s! Access remote service fail!", name); } }
先啟動(dòng)eureka-client和eureka-server項(xiàng)目, 然后再啟動(dòng)feign-service項(xiàng)目,在瀏覽器訪問 http://localhost:8080/hello?name=Mars:
Hello, My name is Mars, I'm from port: 8040
這時(shí)候我們關(guān)閉eureka-service項(xiàng)目, 再次訪問 http://localhost:8080/hello?name=Mars:
Hello, Mars! Access remote service fail!
以上就是Spring Cloud 中Hystrix有什么用,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。