eureka-client
依赖和spring-boot-web
依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.friendsgroupId>
<artifactId>hystrix-clientartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>hystrix-clientname>
<description>Demo project for Spring Clouddescription>
<parent>
<groupId>org.friendsgroupId>
<artifactId>centralparkartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
@SpringBootApplication
@EnableDiscoveryClient
public class HystrixClientApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixClientApplication.class, args);
}
}
HystrixClientController.java
用于提供接口服务@RestController
public class HystrixClientController {
@Qualifier("eurekaRegistration")
@Autowired
private Registration registration;
@GetMapping("getInstanceInfo")
public String getInstanceInfo(){
String serviceId = registration.getServiceId();
String host = registration.getHost();
int port = registration.getPort();
return serviceId+":"+port+"; "+"\n host:"+host;
}
}
Registration
:用于获取本实例信息,例如服务名称,host,端口等
application.yml
中,写入注册中心地址和服务名称spring:
profiles:
active: hystrixClient8601
application:
name: hystrixClient
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
---
server:
port: 8601
spring:
profiles: hystrixClient8601
---
server:
port: 8602
spring:
profiles: hystrixClient8602
spring-cloud-starter-netflix-hystrix
依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.friendsgroupId>
<artifactId>hystrix-consumerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>hystrix-consumername>
<description>Demo project for Spring Clouddescription>
<parent>
<groupId>org.friendsgroupId>
<artifactId>centralparkartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
ribbon
负载均衡的模板和@SpringCloudApplication
的注解@SpringCloudApplication
public class HystrixConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
- 开启hystrix可以使用
@EnableHystrix
注解也可以使用@EnableCircuitBreaker
,两者是等价的@SpringCloudApplication
是一个全家桶的注解,包含了@SpringBootApplication
,@EnableDiscoveryClient
(服务发现),@EnableCircuitBreaker
(服务容错)的功能
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public @interface SpringCloudApplication {
}
HystrixConsumerApplicationController.java
用于实现hystrix具体功能@RestController
public class HystrixConsumerApplicationController {
@Autowired
HystrixConsumerApplicationServer hystrixConsumerApplicationServer;
@GetMapping("getInstanceInfo")
public String getInstanceInfo(){
return hystrixConsumerApplicationServer.getInstanceInfo();
}
@Service
class HystrixConsumerApplicationServer{
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "rollBack")
public String getInstanceInfo(){
return restTemplate.getForObject("http://hystrixClient/getInstanceInfo",String.class);
}
public String rollBack(){
return "降级处理";
}
}
}
HystrixConsumerApplicationServer.java
:作为内部类直接嵌入到了HystrixConsumerApplicationController.java
中
@HystrixCommand(fallbackMethod = "rollBack")
指定了降级保护方法为rollBack()
application.yml
中spring:
application:
name: hystrixConsumer
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8101
eureka-server-standalone
,接口服务hystrix-client
,受保护的消费服务hystrix-consumer
http://localhost:8101/getInstanceInfo
返回
hystrixClient:8601; host:localhost
hystrix-client
再次访问http://localhost:8101/getInstanceInfo
,进入到了降级方法中,返回
降级处理
getInstanceInfo
方法@HystrixCommand(fallbackMethod = "rollBack")
指定了降级方法为rollBack
,当其请求的服务失败后,进入回退方法中。如果回退方法也可能抛出异常,则回退方法中也可以再次标注@HystrixCommand
进行降级。服务降级面临的问题,当上游服务宕机,每一个请求都要去尝试请求上游服务,达到超时时间后再执行降级逻辑,这样也会产生请求堆积
服务熔断的断路器就是为了解决上述问题。断路器有三个重要参数:快照时间窗、请求总数下限、错误百分比下限。这个参数的作用分别是:
那么当断路器打开之后会发生什么呢?我们先来说说断路器未打开之前,对于之前那个示例的情况就是每个请求都会在当hystrix超时之后返回fallback,每个请求时间延迟就是近似hystrix的超时时间,如果设置为5秒,那么每个请求就都要延迟5秒才会返回。当熔断器在10秒内发现请求总数超过20,并且错误百分比超过50%,这个时候熔断器打开。打开之后,再有请求调用的时候,将不会调用主逻辑,而是直接调用降级逻辑,这个时候就不会等待5秒之后才返回fallback。通过断路器,实现了自动地发现错误并将降级逻辑切换为主逻辑,减少响应延迟的效果。
在断路器打开之后,处理逻辑并没有结束,我们的降级逻辑已经被成了主逻辑,那么原来的主逻辑要如何恢复呢?对于这一问题,hystrix也为我们实现了自动恢复功能。当断路器打开,对主逻辑进行熔断之后,hystrix会启动一个休眠时间窗,在这个时间窗内,降级逻辑是临时的成为主逻辑,当休眠时间窗到期,断路器将进入半开状态,释放一次请求到原来的主逻辑上,如果此次请求正常返回,那么断路器将继续闭合,主逻辑恢复,如果这次请求依然有问题,断路器继续进入打开状态,休眠时间窗重新计时。
Throwable t
用来捕获异常@HystrixCommand
中除了自身HystrixBadRequestException
异常为其余的异常都会进入降级方法中,如果们希望忽略某个异常添加@HystrixCommand
的ignoreExceptions
属性进行忽略HystrixConsumerApplicationServer
中添加方法 @HystrixCommand(fallbackMethod = "rollBackException",ignoreExceptions = {NullPointerException.class})
public String hystrixHandleException(String name) {
throw new RuntimeException("throw hystrixHandleException");
}
public String rollBackException(String name,Throwable t){
return name+": "+t.getMessage();
}
HystrixConsumerApplicationController.java
中 @GetMapping("hystrixHandleException")
public String hystrixHandleException(String name){
return hystrixConsumerApplicationServer.hystrixHandleException(name);
}
hystrixHandleException
接口可以看到降级方法获取到了抛出的异常上一篇:Spring Cloud 分布式配置中心,配置文件子目录存储以及高可用 ,Spring Cloud Config(Finchley版本)
下一篇:Spring Cloud Hystrix熔断的监控面板,Dashboard详解(Finchley版本)