前面文章基于https://www.jianshu.com/writer#/notebooks/24441977/notes/35969879文章的内容,搭建了Spring Cloud Ribbon + Eureka的Spring Boot的工程,介绍了Ribbon客户端负载的相关内容。这篇文章在此基础上,来聊聊Spring Cloud Hystrix。另外,本文基于Spring Boot的Finchley.SR2版本所构建。
快速入门
Hystrix服务端
- application.yml配置
构建两个Spring Boot项目,分别为hystrix-server:9091、hystrix-server:9092,并配置分布式注册中心Eureka
server:
port: 9091
spring:
application:
name: hystrix-server
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
instance:
hostname: ${spring.application.name}
instance-id: ${spring.application.name}:${server.port}
appname: hystrix-server
Hystrix-server启动类,端口为9091、9092的Hystrix-server启动类一样,并提供了Restful风格的hello接口,这里就只列出Hystrix-server:9091的启动类。
@SpringBootApplication
@EnableEurekaClient
@RestController
public class HystrixServer1Application {
public static void main(String[] args) {
SpringApplication.run(HystrixServer1Application.class, args);
}
@GetMapping("/hello")
public String hello(){
return "hello,this is :"+this.getClass().getName();
}
}
Hystrix客户端:Hystrix-client
pom配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml配置
server:
port: 9094
spring:
application:
name: hystrix-client
cloud:
loadbalancer:
retry:
enabled: true #开启重试机制
hystrix-server:
ribbon:
ConnectTimeout: 250 #请求连接超时
ReadTimeout: 1000 #请求处理的超时时间
OkToRetryOnAllOperations: true #对所有操作请求都进行重试
MaxAutoRetriesNextServer: 2 #切换实例的重试次数
maxAutoRetries: 2 #对当前实例的重试次数
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
instance:
hostname: ${spring.application.name}
instance-id: ${spring.application.name}:${server.port}
appname: hystrix-client
客户端代码:
@SpringBootApplication
@RestController
@EnableCircuitBreaker
@EnableEurekaClient
public class HystrixClient2Application {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
@Resource
HelloService helloService;
@GetMapping("hystrix-hello")
public String hello(){
return helloService.hello();
}
public static void main(String[] args) {
SpringApplication.run(HystrixClient2Application.class, args);
}
}
@Component
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "helloFallback")
public String hello(){
String msg= restTemplate.getForEntity("http://hystrix-server/hello",String.class).getBody();
return msg;
}
public String helloFallback(){
return "hello , error";
}
}
依次启动Eureka-server、hystrix-client服务
此时调用http://localhost:9094/hystrix-hello,由于Hystrix-server并没有启动,所以在执行String msg= restTemplate.getForEntity("http://hystrix-server/hello",String.class).getBody()时,发生异常,继而执行helloFallback()方法。会打印出 hello , error
现在启动Hystrix-server服务,Eureka监控界面如下:
再次调用http://localhost:9094/hystrix-hello,则会轮番打印出下面的信息
hello,this is :springcloud.hystrixserver2.HystrixServer2Application$$EnhancerBySpringCGLIB$$5b5dd2e6
hello,this is :springcloud.hystrixserver1.HystrixServer1Application$$EnhancerBySpringCGLIB$$92afab75