前面文章基于http://www.reibang.com/writer#/notebooks/24441977/notes/35969879文章的內(nèi)容憔辫,搭建了Spring Cloud Ribbon + Eureka的Spring Boot的工程躯嫉,介紹了Ribbon客戶端負載的相關(guān)內(nèi)容训貌。這篇文章在此基礎上作媚,來聊聊Spring Cloud Hystrix蔬充。另外复哆,本文基于Spring Boot的Finchley.SR2版本所構(gòu)建歧沪。
快速入門
Hystrix服務端
- application.yml配置
構(gòu)建兩個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 #切換實例的重試次數(shù)
maxAutoRetries: 2 #對當前實例的重試次數(shù)
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服務
此時調(diào)用http://localhost:9094/hystrix-hello是复,由于Hystrix-server并沒有啟動删顶,所以在執(zhí)行String msg= restTemplate.getForEntity("http://hystrix-server/hello",String.class).getBody()時,發(fā)生異常淑廊,繼而執(zhí)行helloFallback()方法逗余。會打印出 hello , error
現(xiàn)在啟動Hystrix-server服務,Eureka監(jiān)控界面如下:
再次調(diào)用http://localhost:9094/hystrix-hello蒋纬,則會輪番打印出下面的信息
hello,this is :springcloud.hystrixserver2.HystrixServer2Application$$EnhancerBySpringCGLIB$$5b5dd2e6
hello,this is :springcloud.hystrixserver1.HystrixServer1Application$$EnhancerBySpringCGLIB$$92afab75