在微服務架構中,我們將業務拆分成一個個的服務,服務與服務之間可以相互調用(RPC)。爲了保證其高可用,單個服務又必須集羣部署。由於網絡原因或者自身的原因,服務並不能保證服務的100%可用,如果單個服務出現問題,調用這個服務就會出現網絡延遲,此時若有大量的網絡涌入,會形成任務累計,導致服務癱瘓,甚至導致服務“雪崩”。
爲了解決這個問題,就出現斷路器模型。
一、斷路器簡介
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已經創建了一個名爲Hystrix的庫來實現斷路器模式。 在微服務架構中,多層服務調用是非常常見的。
較底層的服務如果出現故障,會導致連鎖故障。當對特定的服務的調用達到一個閥值(hystric 是5秒20次) 斷路器將會被打開。
斷路打開後,可用避免連鎖故障,fallback方法可以直接返回一個固定值。
二、準備工作
基於上一篇文章的工程,首先啓動:
基於上一節的工程,啓動eureka-server 工程;啓動service-hi工程,它的端口爲8762;
三、在ribbon使用斷路器
改造serice-ribbon 工程的代碼:
在pox.xml文件中加入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
在程序的入口類加@EnableHystrix:
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
改造HelloService類,加上@HystrixCommand,並指定fallbackMethod方法。
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}
public String hiError(String name) {
return "hi,"+name+",sorry,error!";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
啓動:service-ribbon 工程,當我們訪問http://localhost:8764/hi?name=forezp,瀏覽器顯示:
hi forezp,i am from port:8762
此時關閉 service-hi ,工程,當我們再訪問http://localhost:8764/hi?name=forezp,瀏覽器會顯示:
hi ,forezp,orry,error!
這就證明斷路器起作用了。
四、Feign中使用斷路器
如果你使用了feign,feign是自帶斷路器的,並且是已經打開了。如果使用feign不想用斷路器的話,可以在配置文件中關閉它,配置如下:
feign.hystrix.enabled=false
基於service-feign我們在改造下,只需要在SchedualServiceHi接口的註解中加上fallback的指定類就行了:
@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
SchedualServiceHiHystric類:
@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
@Override
public String sayHiFromClientOne(String name) {
return "sorry "+name;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
啓動四servcie-feign工程,打開http://localhost:8765/hi?name=forezp,注意此時service-hi還沒打開,網頁顯示:
sorry forezp
打開service-hi,網頁顯示;
>
hi forezp,i am from port:8762
這證明斷路器起到作用了。
五、Circuit Breaker: Hystrix Dashboard (斷路器:hystrix 儀表盤)
基於service-ribbon 改造下:
pom.xml加入:
<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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
在主程序入口中加入@EnableHystrixDashboard註解,開啓hystrixDashboard:
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
打開瀏覽器:訪問http://localhost:8764/hystrix,界面如下:
點擊monitor stream,進入下一個界面,訪問:http://localhost:8764/hi?name=forezp
此時會出現監控界面:
本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter4
六、參考資料
優秀文章推薦:
- 史上最簡單的 SpringCloud 教程 | 終章
- 史上最簡單的 SpringCloud 教程 | 第一篇: 服務的註冊與發現(Eureka)
- 史上最簡單的SpringCloud教程 | 第七篇: 高可用的分佈式配置中心(Spring Cloud Config)
勘誤:有人反映feign的熔斷器不起作用,springcloud版本的問題,用這個:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>