本文共 3366 字,大约阅读时间需要 11 分钟。
Hystrix
会监控微服务的调用状况,失败调用达到一定阀值时,会启用熔断机制
JDK8+
Gradle4+
Spring Boot 2.0.0.M3
Spring Cloud Starter Netflix Eureka Client Finchley.M2
Spring Cloud Starter OpenFeign Finchley.M2
Spring Cloud Starter Netflix Hystrix Finchley.M2
以之前的micro-weather-eureka-client-feign
为蓝本,创建micro-weather-eureka-client-feign-hystrix
项目
修改build.gradle
配置,添加Hystrix
依赖:
//依赖关系dependencies { //Eureka Client compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') //Feign compile('org.springframework.cloud:spring-cloud-starter-openfeign:2.0.0.M3') //Hystrix compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix') //该依赖用于测试阶段 testCompile('org.springframework.boot:spring-boot-starter-test')}
修改com.study.spring.cloud.weather
包下的Application
类,加入@EnableCircuitBreaker
注解,开启Hystrix
的使用:
package com.study.spring.cloud.weather;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;/* * @SpringBootApplication注解声明Spring Boot应用 * 作用等同于@Configuration, @EnableAutoConfiguration, @ComponentScan, * 简化Spring配置*/@SpringBootApplication//启用可发现的客户端@EnableDiscoveryClient//启用Feign@EnableFeignClients//启用Hystrix@EnableCircuitBreaker//Application类一定要处于整个工程的根目录下,这样它才能根据配置去扫描子节点下的Spring的Beanpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
这里我们还可以使用Spring Cloud
应用中的@SpringCloudApplication
注解来修饰应用主类,该注解中包含了上面我们所引用的@SpringBootApplication
、@EnableDiscoveryClient
、@EnableCircuitBreaker
三个注解,这也意味着一个Spring Cloud
标准应用应包含服务发现以及断路器。
修改com.study.spring.cloud.weather.controller
包下的CityController
类,为具体执行逻辑的函数上增加@HystrixCommand
注解来指定服务降级方法:
package com.study.spring.cloud.weather.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import com.study.spring.cloud.weather.service.CityClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;//用于处理rest请求的controller@RestControllerpublic class CityController { @Autowired private CityClient cityClient; @GetMapping("/cities") //启用断路器 @HystrixCommand(fallbackMethod = "defaultCities") public String listCity() { //通过Feign客户端来查找 String body=cityClient.listCity(); return body; } public String defaultCities(){ return "City Data Server is down!"; } }
上面我们使用了@HystrixCommand
来将具体执行逻辑的函数包装成了Hystrix
命令(Hystrix
命令就是HystrixCommand
,它用来封装具体的依赖服务调用逻辑),这里除了定义服务降级之外,Hystrix
框架会自动的为这个函数实现调用的隔离。所以,依赖隔离、服务降级在使用时候都是一体化实现的,这样利用Hystrix
来实现服务容错保护在编程模型上就非常方便,并且考虑更为全面。
修改application.properties
配置文件:
#应用名称spring.application.name=micro-weather-eureka-client-feign-hystrix#注册服务器的URLeureka.client.service-url.defaultZone=http://localhost:8761/eureka/#请求服务时的超时时间feign.client.config.feignName.connect-timeout=5000#读数据时的超时时间feign.client.config.feignName.read-timeout=5000
先在IDE
上运行micro-weather-eureka-server
再通过命令行指定端口(如8085
)运行msa-weather-city-eureka
最后在IDE
上运行micro-weather-eureka-client-feign-hystrix
访问http://localhost:8080/cities
页面:
将msa-weather-city-eureka
断掉,再刷新http://localhost:8080/cities
页面:
转载地址:http://uwswi.baihongyu.com/