在微服务架构中,当某个服务不可用时,其他服务调用故障的服务时会导致线程挂起,当有大量的请求去请求故障的服务时,此时会有大量线程挂起,导致其他服务可能崩溃,所以引入Hystrix(断路器)。通过断路器的故障监控,向调用方返回一个错误响应,而不是一个长时间的等待,这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。
这里依然采用上一节的代码,然后引入Hystrix模块。
Hystrix的依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
代码依然采用provider+consumer节的代码。
一 Hystrix的使用
在consumer模块添加Hystrix的依赖,并在启动类上添加@EnableCircuitBreaker注解表示开启Hystrix。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.SpringCloudApplication;
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.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class SpringCloudConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringCloudConsumerApplication.class, args);
}
}
1 添加UserService类
在helloService()方法中添加@HystrixCommand注解指定回调方法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class UserService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod="fallBack")
public String helloService(){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return restTemplate.getForEntity("http://eureka-provider/getHello", String.class).getBody();
}
public String fallBack(){
return "error";
}
}
3 修改ConsumerController类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.hdu.server.service.UserService;
import com.netflix.infix.lang.infix.antlr.EventFilterParser.regex_predicate_return;
@RestController
public class ConsumerController {
@Autowired
private UserService userService;
@GetMapping("/ribbon-consumer")
public String helloConsumer(){
return userService.helloService();
}
}
4 启动项目模拟断路器
访问http://localhost:8001/ribbon-consumer,成功返回"hello world"。
然后断开provider服务,再次访问http://localhost:8001/ribbon-consumer,就可以看到由断路器回调方法返回的信息"error"。
在helloService()方法中,加入Thread.sleep(100)是为了模拟服务器阻塞的情况,Hystrix默认的超时时间为2000毫秒,通过修改Thread.sleep()时间来触发断路器。
二 Hystrix Dashboard监控