
在分布式架构中,当某个服务单元发生故障后,能通过断路器的故障监控,向调用方返回一个错误响应,而不是长时间的等待。
Netflix Hystrix
在Spring Cloud中使用了Hystrix 来实现断路器的功能。Hystrix是Netflix开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。
一 准备工作
依次启动eureka-server、compute-service、eureka-ribbon工程;
访问http://localhost:1111/可以看到注册中心的状态;
访问http://localhost:3333/add,调用eureka-ribbon的服务,该服务会去调用compute-service的服务,计算出10+20的值,页面显示30;
关闭compute-service的服务,再次访问http://localhost:3333/add,会得到以下错误
二、ribbon引入Hystrix
1、ribbon工程的pom.xml中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2、在启动类中添加注解开启断路器功能
package com.daqsoft; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
//用来发现注册服务
@EnableDiscoveryClient
//开启断路器功能
@EnableCircuitBreaker
public class CustomerDemoApplication { @Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(CustomerDemoApplication.class, args);
}
}
3、改造原来的服务消费方式,新增ComputeService
类,在使用ribbon消费服务的函数上增加@HystrixCommand
注解来指定回调方法
package com.daqsoft; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; /**
* @Description Created by liaoxx on 2017-6-13.
*/
@Service
public class ComputeService { @Autowired
RestTemplate restTemplate; /**
* 调用服务发生故障时回调addServiceFallback方法
* @return
*/
@HystrixCommand(fallbackMethod = "addServiceFallback")
public String addService(){
return restTemplate.getForEntity("http",String.class).getBody();
} public String addServiceFallback(){
return "error";
} }
4、提供rest接口的Controller改为调用ComputeService的addService
package com.daqsoft; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* @Description Created by liaoxx on 2017-6-12.
*/
@RestController
public class CustomController { @Autowired
private ComputeService computeService; @RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(){
return computeService.addService(); }
}
5、启动服务,访问 http://localhost:3333/add 进行验证(成功拦截并进行回调)