在微服務(wù)架構(gòu)中,我們將業(yè)務(wù)拆分成一個(gè)個(gè)的服務(wù),服務(wù)與服務(wù)之間可以相互調(diào)用(RPC)惩阶。為了保證其高可用,單個(gè)服務(wù)又必須集群部署扣汪。由于網(wǎng)絡(luò)原因或者自身的原因断楷,服務(wù)并不能保證服務(wù)的100%可用,如果單個(gè)服務(wù)出現(xiàn)問題崭别,調(diào)用這個(gè)服務(wù)就會出現(xiàn)網(wǎng)絡(luò)延遲冬筒,此時(shí)若有大量的網(wǎng)絡(luò)涌入,會形成任務(wù)累計(jì)茅主,導(dǎo)致服務(wù)癱瘓舞痰,甚至導(dǎo)致服務(wù)“雪崩”。
為了解決這個(gè)問題诀姚,就出現(xiàn)斷路器模型响牛。
一、斷路器簡介
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.
. ----摘自官網(wǎng)
Netflix已經(jīng)創(chuàng)建了一個(gè)名為Hystrix的庫來實(shí)現(xiàn)斷路器模式。 在微服務(wù)架構(gòu)中娃善,多層服務(wù)調(diào)用是非常常見的论衍。
較底層的服務(wù)如果出現(xiàn)故障,會導(dǎo)致連鎖故障聚磺。當(dāng)對特定的服務(wù)的調(diào)用達(dá)到一個(gè)閥值(hystric 是5秒20次) 斷路器將會被打開。
斷路打開后炬丸,可用避免連鎖故障瘫寝,fallback方法可以直接返回一個(gè)固定值。
二稠炬、準(zhǔn)備工作
基于上一篇文章的工程焕阿,首先啟動(dòng):
基于上一節(jié)的工程,啟動(dòng)eureka-server 工程首启;啟動(dòng)service-hi工程暮屡,它的端口為8762;
三、在ribbon使用斷路器
改造serice-ribbon 工程的代碼:
在pox.xml文件中加入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
在程序的入口類加@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();
}
}
改造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!";
}
}
啟動(dòng):service-ribbon 工程钥飞,當(dāng)我們訪問http://localhost:8764/hi?name=forezp,瀏覽器顯示:
hi forezp,i am from port:8762
此時(shí)關(guān)閉 service-hi ,工程莺掠,當(dāng)我們再訪問http://localhost:8764/hi?name=forezp,瀏覽器會顯示:
hi ,forezp,orry,error!
這就證明斷路器起作用了读宙。
四彻秆、Feign中使用斷路器
如果你使用了feign,feign是自帶斷路器的结闸,并且是已經(jīng)打開了唇兑。如果使用feign不想用斷路器的話,可以在配置文件中關(guān)閉它桦锄,配置如下:
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);
}
SchedualServiceHiHystric類:
@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
@Override
public String sayHiFromClientOne(String name) {
return "sorry "+name;
}
}
啟動(dòng)四servcie-feign工程扎附,打開http://localhost:8765/hi?name=forezp,注意此時(shí)service-hi還沒打開,網(wǎng)頁顯示:
sorry forezp
打開service-hi,網(wǎng)頁顯示察纯;
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>
在主程序入口中加入@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();
}
}
打開瀏覽器:訪問http://localhost:8764/hystrix,界面如下:
點(diǎn)擊monitor stream香伴,進(jìn)入下一個(gè)界面,訪問:http://localhost:8764/hi?name=forezp
此時(shí)會出現(xiàn)監(jiān)控界面:
本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter4