1.consumer
1.1POM
1.2YML
1.3主启动类+注解@SpringBootApplication @EnableFeignClients @EnableHystrix
1.4测试
2.consumer端controller 降级处理defaultProperties
2.1yml
2.2主启动类@EnableHystrix
2.3业务类
a)添加降级处理函数
b)关联
c)设定降级条件
2.4.测试
3.consumer端service 降级处理FeignFallback
80->8001 provider 宕机或者关闭了。
3.1.添加类
a)添加一个服务降级类fallbackservice
b)关联实现 两步骤
3.2.YML feign是要打开的
3.3.测试
1.consumer
1.1POM
<!--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>
1.2YML
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
feign:
hystrix:
enabled: true
1.3主启动类+注解@SpringBootApplication @EnableFeignClients @EnableHystrix
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class OrderHystrixMain80 {
public static void main(String[] args)
{
SpringApplication.run(OrderHystrixMain80.class,args);
}
}
1.4测试
7001,8001,80 1.自测8001
http://localhost:8001/payment/hystrix/ok/32
http://localhost:8001/payment/hystrix/timeout/32
2.自测80 feign是否正确 http://localhost/consumer/payment/hystrix/ok/32
3.压力测试:启动7001 8001 80
启动测试工具: 80已经开始等待了
2.consumer端controller 降级处理
2.1yml
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
feign:
hystrix:
enabled: true
2.2主启动类@EnableHystrix
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class OrderHystrixMain80
{
public static void main(String[] args)
{
SpringApplication.run(OrderHystrixMain80.class,args);
}
}
2.3业务类
a)添加降级处理函数
b)关联
c)设定降级条件
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystirxController
{
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
})
//@HystrixCommand
public String paymentInfo_TimeOut(@PathVariable("id") Integer id)
{
int age = 10/0;
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id)
{
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}
// 下面是全局fallback方法
public String payment_Global_FallbackMethod()
{
return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~";
}
}
2.4 测试
8001 3000ms 80 1500ms
Localhost/consumer/payment/hystrix/timeout/31
出的应该是80的paymentTimeOutFallbackMethod()函数表示降级处理了。
3.consumer端service 降级处理FeignFallback
80->8001 provider 宕机或者关闭了。
3.1.添加类
a)添加一个服务降级类fallbackservice
a)添加一个服务降级类fallbackservice
浏览器调用是 ,调用 "CLOUD-PROVIDER-HYSTRIX-PAYMENT"服务名下的
/payment/hystrix/ok/{id}方法降级处理函数其实是 PaymentFallbackService
@Component
public class PaymentFallbackService implements PaymentHystrixService
{
@Override
public String paymentInfo_OK(Integer id)
{
return "-----PaymentFallbackService fall back-paymentInfo_OK ,o(╥﹏╥)o";
}
@Override
public String paymentInfo_TimeOut(Integer id)
{
return "-----PaymentFallbackService fall back-paymentInfo_TimeOut ,o(╥﹏╥)o";
}
}
b)关联实现 两步骤
//1)public class PaymentFallbackService implements PaymentHystrixService
//降级函数必须是原接口的执行函数
//2)原函数添加注解 实现关联
//@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT" ,fallback = PaymentFallbackService.class)
3.2.YML feign是要打开的
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
feign:
hystrix:
enabled: true
3.3.测试
80->8001 provider 宕机或者关闭了。
1.启动7001,8001, 自测访问
2.80 访问 3.8001 关闭 80 访问