Spring Cloud入门系列(十)- 服务熔断与降级之Hystrix(已停更,建议切换到Sentinel)
模块构建
新建 客户端 模块:cloud-consumer-feign-hystrix-order80
结构图,如下:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>cloud201230</artifactId>
<groupId>com.banana</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-feign-hystrix-order80</artifactId>
<dependencies>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入自定义的api通用包,可以使用Payment支付Entity-->
<dependency>
<groupId>com.banana</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 80
eureka:
client:
#是否将自己注册到注册中心, 默认true
register-with-eureka: false
#是否从EurekaServer抓取已有的注册信息,单机无所谓,集群必须设置为true配合ribbon使用负载均衡
#fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka #单机版
feign:
hystrix:
enabled: true
FeignHystrixOrder80
package com.banana.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author layman
* @date 2021/1/18
*/
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class FeignHystrixOrder80 {
public static void main(String[] args) {
SpringApplication.run(FeignHystrixOrder80.class,args);
}
}
OpenFeignOrderService
package com.banana.springcloud.service;
import com.banana.springcloud.service.fallback.OpenFeignOrderFallbackService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* FeignClient注解中的value属性指定了要调用的{服务}名
* @author layman
* @date 2021/1/18
* fallback为value指定的服务名,提供统一的服务降级处理类OpenFeignOrderFallbackService。
* 如果正常访问,不会走fallback指定的OpenFeignOrderFallbackService类,而是直接访问。
* 如果异常访问,需要服务降级,才会走fallback指定的OpenFeignOrderFallbackService。
* A:如果是feignSuccess方法出现异常,那么就会去OpenFeignOrderFallbackService.class找对应的feignSuccess方法,处理fallback
*/
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = OpenFeignOrderFallbackService.class)
public interface OpenFeignOrderService {
/**
* 模拟成功访问
* @param id:主键
* @return CommonResult<Payment>
*/
@GetMapping(value = "/payment/hystrix/success/{id}")
String feignSuccess(@PathVariable("id") Long id);
/**
* 模拟openFeign访问超时
* @param id:主键
* @return String
*/
@GetMapping("/payment/hystrix/timeout/{id}")
String feignTimeout(@PathVariable("id") Long id);
}
OpenFeignOrderFallbackService
package com.banana.springcloud.service.fallback;
import com.banana.springcloud.service.OpenFeignOrderService;
import org.springframework.stereotype.Component;
/**
* 全局服务降级处理类
* @author layman
* @date 2021/1/18
*/
@Component
public class OpenFeignOrderFallbackService implements OpenFeignOrderService {
/**
* 模拟成功访问
* @param id :主键
* @return CommonResult<Payment>
*/
@Override
public String feignSuccess(Long id) {
return "-------OpenFeignOrderFallbackService, feignSuccess出错啦 , o(╥﹏╥)o";
}
/**
* 模拟openFeign访问超时
* @param id :主键
* @return String
*/
@Override
public String feignTimeout(Long id) {
return "-------OpenFeignOrderFallbackService, feignTimeout出错啦 , o(╥﹏╥)o";
}
}
OpenFeignOrderController
package com.banana.springcloud.controller;
import com.banana.springcloud.service.OpenFeignOrderService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author layman
* @date 2021/1/18
*/
@RestController
@RequestMapping("/consumer/hyxtrix")
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OpenFeignOrderController {
@Resource
private OpenFeignOrderService openFeignOrderService;
@GetMapping("/success/{id}")
public String feignSuccess(@PathVariable("id") Long id){
return openFeignOrderService.feignSuccess(id);
}
@GetMapping("/timeout/{id}")
@HystrixCommand(fallbackMethod = "feignTimeoutHandler", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
})
public String feignTimeout(@PathVariable("id") Long id){
return openFeignOrderService.feignTimeout(id);
}
public String feignTimeoutHandler(@PathVariable("id") Long id){
return "对方支付系统繁忙,请稍后重试!";
}
/**
* HystrixCommand注解没有配置属性,走全局fallback方法
*/
@GetMapping("/global/fallback")
@HystrixCommand
public String feignGlobalFallback(){
String result = null;
return result.toString();
}
/**
* 全局fallback 降级方法
*/
public String payment_Global_FallbackMethod() {
return "Global异常处理信息,请稍后再试,o(╥﹏╥)o";
}
}