天天看點

spring cloud ----> RibbonClient設定的熔斷器Hystrix不起作用

Ribbon

​​spring.io​​ 官網的簡介:

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients.

自己的翻譯:

Ribbon是一個用來負載均衡的用戶端,對于http和tcp用戶端行為,Ribbon能給與你很多的控制方式。

在給Ribbon設定熔斷器Hystrix的時候發現熔斷器不起作用。

代碼如下:

1 package com.example.demo;

2

3 import org.springframework.boot.SpringApplication;

4 import org.springframework.boot.autoconfigure.SpringBootApplication;

5 import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

6 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

7 import org.springframework.cloud.client.loadbalancer.LoadBalanced;

8 import org.springframework.cloud.netflix.hystrix.EnableHystrix;

9 import org.springframework.context.annotation.Bean;

10 import org.springframework.web.client.RestTemplate;

11 /**

12 * 在工程的啟動類中,通過 @EnableDiscoveryClient 向服務中心注冊;

13 * 并且向程式的ioc注入一個bean: restTemplate;

14 * 并通過 @LoadBalanced 注解表明這個 restRemplate 開啟負載均衡的功能。

15 *

16 *

17 * 熔斷器的使用

18 * 在程式的啟動類RibbonClientApplication 加 @EnableHystrix 注解開啟Hystrix 不起作用

19 *

20 *

21 */

22 @SpringBootApplication

23 @EnableDiscoveryClient //.....

24 //@EnableHystrix //......//注解不起作用

25 @EnableCircuitBreaker //注解起作用,檢視官方文檔可知

26 public class RibbonClientApplication {

27

28 public static void main(String[] args) {

29 SpringApplication.run(RibbonClientApplication.class, args);

30 }

31

32 @LoadBalanced //......

33 @Bean

34 public RestTemplate restTemplate() {

35 return new RestTemplate();

36 }

37 }

繼續閱讀