前言
在互联网开发中经常会听到雪崩效应,比如某明星发一些状态某猿就要回去加班了!那么为了应对雪崩我们经常会进行服务扩容、添加缓存、优化流程但往往突发的事件依然有击穿缓存、应用负载、数据库IO、网络异常等等带来的风险,所以一些常见的做法有服务降级、限流、熔断,在逐步恢复系统可用率来保护系统。
Hystrix 是一种熔断降级的中间件,由 Spring Cloud 集成整合后在Ribbon与Fegin中提供使用。
web/EurekaClientController.java | 注意@EnableEurekaClient用于向注册中心提供服务
java
@EnableEurekaClient
@RestController
public class EurekaClientController {
@Value("${server.port}")
private int port;
@RequestMapping(path = "/api/queryUserInfo", method = RequestMethod.GET)
public String queryUserInfo(@RequestParam String userId) {
return "Hi 明哥 | " + userId + " >: from eureka client port: " + port;
}
}
EurekaClientApplication.java | 服务启动类
java
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
application.yml | 配置文件链接服务注册中心,8001\8002分别配置启动
yaml
server:
port: 8001 / 8002
spring:
application:
name: demo-springcloud-eureka-client
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7397/eureka/
EurekaServerApplication.java | 通过注解@EnableEurekaServer启动服务注册与发现中心
java
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run( EurekaServerApplication.class, args );
}
}
application.yml | 服务注册中心配置文件,端口7397和我们之前写netty的服务的端口一致
yaml
server:
port: 7397
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: demo-springcloud-eureka-server
Feign服务调用方,添加熔断Hystrix
Feign 具有如下特性:
可插拔的注解支持,包括 Feign 注解和 JAX-RS 注解 支持可插拔的 HTTP 编码器和解码器 支持 Hystrix 和它的 Fallback 支持 Ribbon 的负载均衡 支持 HTTP 请求和响应的压缩
service/FeignService.java | 注解方式调用,方便易用。@FeignClient会在调用时进行解析服务到具体的http://ip:port/
java
@FeignClient(value = "demo-springcloud-eureka-client", fallback = FeignServiceHystrix.class)
public interface FeignService {
@RequestMapping(value = "/api/queryUserInfo", method = RequestMethod.GET)
String queryUserInfo(@RequestParam String userId);
}
service/hystrix/FeignServiceHystrix.java | 提供熔断服务,当发生异常时主动返回预定结果
java
@Component
public class FeignServiceHystrix implements FeignService {
@Override
public String queryUserInfo(String userId) {
return "queryUserInfo by userId:" + userId + " err!from feign hystrix";
}
}
web/FeignController.java | 使用接口提供服务 From Feign
java
@RestController
public class FeignController {
@Resource
private FeignService ribbonService;
@RequestMapping(path = "/api/queryUserInfo", method = RequestMethod.GET)
public String queryUserInfo(@RequestParam String userId) {
return ribbonService.queryUserInfo(userId) + " From Feign";
}
}
FeignApplication.java | 注解@EnableEurekaClient、@EnableFeignClients、@EnableDiscoveryClient获取调用注册中心服务
java
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
application.yml | eureka服务配置,从注册中心获取可用服务。开启hystrix=true
yaml
server:
port: 9001
spring:
application:
name: demo-springcloud-feign
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7397/eureka/
feign.hystrix.enabled: true
Ribbon服务调用方
Ribbon是一个基于 HTTP 和 TCP 的客户端负载均衡器。它可以通过在客户端中配置 ribbonServerList 来设置服务端列表去轮询访问以达到均衡负载的作用。
当 Ribbon 与 Eureka 联合使用时,ribbonServerList 会被 DiscoveryEnabledNIWSServerList 重写,扩展成从 Eureka 注册中心中获取服务实例列表。同时它也会用 NIWSDiscoveryPing 来取代 IPing,它将职责委托给 Eureka 来确定服务端是否已经启动。
java
@Service
public class RibbonService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "queryUserInfoFallback")
public String queryUserInfo(String userId) {
return restTemplate.getForObject("http://DEMO-SPRINGCLOUD-EUREKA-CLIENT/api/queryUserInfo?userId=" + userId, String.class);
}
public String queryUserInfoFallback(String userId) {
return "queryUserInfo by userId:" + userId + " err!from ribbon hystrix";
}
}
web/RibbonController.java | 使用接口提供服务 From Ribbon
java
@RestController
public class RibbonController {
@Resource
private RibbonService ribbonService;
@RequestMapping(path = "/api/queryUserInfo", method = RequestMethod.GET)
public String queryUserInfo(@RequestParam String userId) {
return ribbonService.queryUserInfo(userId) + " From Ribbon";
}
}
RibbonApplication.java | 通过注解@LoadBalanced注册rest模版,用于Ribbon接口调用。并启动@EnableHystrix
java
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
application.yml | eureka服务配置,从注册中心获取可用服务
yaml
server:
port: 9002
spring:
application:
name: demo-springcloud-ribbon
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7397/eureka/
测试验证
1:启动服务注册中心demo-springcloud-eureka-server
2:本地测试不启动eureka-client,以达到服务不可以用的效果
3:启动demo-springcloud-feign
4:启动demo-springcloud-ribbon
5:访问Feign服务调用方,在熔断的保护下会返回预定熔断结果:http://localhost:9001/api/queryUserInfo?userId=1024
访问Ribbon服务调用方,在熔断的保护下会返回预定熔断结果:http://localhost:9002/api/queryUserInfo?userId=1024
总结
1:Spring Cloud 将Hystrix整合后提供非常简单的使用方式,并且提供了丰富的配置可以满足实际应用开发
2:Hystrix Git开源代码;https://github.com/Netflix/Hystrix
3:在熔断降级就像是电闸的保险丝,可以在非常重要的时刻快速失败保护系统.
好了到这里就结束了springcloud之应用服务快速失败熔断降级保护 Hystrix的学习,大家一定要跟着动手操作起来。需要源码的 可si我获取;