SpringCloud Hystrix实现断路器,线程隔离等一系列服务保护功能,它是基于Netflix开源框架Hystrix实现的,该框架的目标是用于通过控制远程系统,服务和第三方节点,从而对延迟和故障提供更强大的容错能力。它具备服务降低,熔断,线程和信号隔离,请求缓存,请求合并以及服务监控等强大功能。
该篇文章主要介绍主要快速使用Hystrix断路器。
首先我们来看一下系统架构:
其中:
- HELLO_SERVICE为我们搭建的高可用的注册中心。
- RIBBON-CONSUMER为服务消费者。
要使用Hystrix还需要引入下面的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
接着我们来搭建一个高可用的注册中心,所谓高可用的注册中心,实际上就是两个注册中心相互注册,这样既实现了注册中心的高可用,也实现了客户端的负载均衡。
下面是注册中心项目的目录:
application.yml:
eureka:
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://peer1:1111/eureka,http://peer2:1112/eureka
spring:
application:
name: hello-service
profiles:
active: peer1,peer2
application-peer1.yml:
server:
port: 1111
eureka:
client:
service-url: http://peer2:1112/eureka/
registerWithEureka: false
fetchRegistry: false
instance:
hostname: peer1
application-peer2.yml:
server:
port: 1112
eureka:
client:
service-url: http://peer1:1111/eureka/
registerWithEureka: false
fetchRegistry: false
instance:
hostname: peer2
主类:
@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
MyConfiguration.java配置负载均衡器:
@Configuration
public class MyConfiguration {
/**
* RestTemplate 作为负载均衡器
* @return
*/
@LoadBalanced
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
}
然后将整个项目打成jar包:
java -jar eurekaserver-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar eurekaserver-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
通过以上命令启动服务。
接着看consumer项目。
首先application.yml文件配置如下:
server:
port: 8762
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://peer1:1111/eureka,http://peer2:1112/eureka
spring:
application:
name: eurka-client
创建一个HelloService服务:
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "helloFallback")
public String helloService(){
return restTemplate.getForEntity("http://hello-service/hello",String.class).getBody();
}
public String helloFallback(){
return "error";
}
}
通过上面代码我们可以看到它会调用http://hello-service/hello
,但是我们没有这个hello这个服务,一般情况下会报错,返回错误的信息。但是我们有时希望在请求一个不存在的请求时,可以返回用户友好的信息或者特性的信息,避免一直发送请求,导致服务端线程数爆满,撑爆内存,这时就可以使用断路器来处理。
@HystrixCommand
注解就可以指定一个方法来处理用户的请求。
最后我们需要一个controller类:
@RestController
public class ConsumerController {
@Autowired
HelloService helloService;
@RequestMapping(value="/consumer",method = RequestMethod.GET)
public String hello(){
return helloService.helloService();
}
}
最后完成启动主类:
//@SpringCloudApplication 该注解相当于下面三个注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ConsumerApplication {
@Bean
@LoadBalanced
RestTemplate getRestTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
现在我们启动消费服务,然后在浏览器中输入http://localhost:8762/consumer
,可以看到返回“error”信息:
至此我们可以看到Hystrix起到了作用,成功的返回了用户指定的信息。