Hystrix是SpringCloud非常方便并且使用率非常高的一个组件。简单点说,它的主要在微服务中担当“守护者”角色,具体作用及原理可以参考Hystrix介绍。
要使用Hystrix组件,只要在普通的基础服务(参考:SpringCloud-搭建微服务提供者-客户端-Client)上增加配置即可,通常和远程服务调用Feign配合使用,处理远程调用失败的结果:
1.pom.xml文件增加配置:
<!-- 引入服务容错 Hystrix 的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.启动类添加注解:@EnableCircuitBreaker
远程调用接口实例:GoodsClient.java
package com.lg.psd.client;
import com.lg.psd.GoodsInfo;
import com.lg.psd.GoodsInfosRequest;
import com.lg.psd.client.hystrix.GoodsClientHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
/**
* <h1>通过feign调用Goods所在的微服务</h1>
*value:zuul里面对应的serviceId
*/
@FeignClient(value = "eureka-client-psd-service-goods",fallback = GoodsClientHystrix.class)
public interface GoodsClient {
@RequestMapping(value="/psd-service-goods/goods/goods",method = RequestMethod.GET)
GoodsInfo getGoodsInfo(Long goodsId);
@RequestMapping(value="/psd-service-goods/goods/goodsList",method = RequestMethod.POST)
List<GoodsInfo> getGoodsInfos(@RequestBody GoodsInfosRequest request);
}
熔断处理实例:GoodsClientHystrix.java
package com.lg.psd;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* <h1>用户服务启动类</h1>
* EnableJpaAuditing:声明该应用使用JPA
* EnableEurekaClient:声明这是一个客户服务端-服务提供者
* EnableFeignClients:声明使用Feign调用其他服务
* EnableCircuitBreaker:声明该服务使用到Hytrix熔断服务
*/
@EnableJpaAuditing
@EnableFeignClients
@EnableCircuitBreaker
@EnableEurekaClient
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
3.创建熔断处理实例,implements需要处理的远程调用接口。
package com.lg.psd.client.hystrix;
import com.alibaba.fastjson.JSON;
import com.lg.psd.GoodsInfo;
import com.lg.psd.GoodsInfosRequest;
import com.lg.psd.client.GoodsClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.List;
/**
* <h1>熔断降级</h1>
* 当远程调用Goods服务失败时,避免雪崩效应,进入熔断降级
*/
@Slf4j
@Component
public class GoodsClientHystrix implements GoodsClient {
@Override
public GoodsInfo getGoodsInfo(Long goodsId) {
log.info("熔断:GoodsClientHystrix-->getGoodsInfo--Param {}", JSON.toJSONString(goodsId));
return GoodsInfo.invalid();
}
@Override
public List<GoodsInfo> getGoodsInfos(GoodsInfosRequest request) {
log.info("熔断:GoodsClientHystrix-->getGoodsInfos--Param {}", JSON.toJSONString(request));
return Collections.emptyList();
}
}
4.流程解析
当使用GoodsClient访问远程服务,如果远程访问服务出现无法访问或者访问出错的时候,熔断机制将会进去对应处理方法里面,这样我们就能更好捕获服务调用失败的节点,能更好处理这个问题,能最大限度地降低对其他业务的影响。
附上Gitee的源码: