springCloud(16)-hystrix-熔断框架的provider实现
hystrix是什么?
网上截了个屏幕
官网地址:Https://github.com/Netflix/Hystrix/wiki/How-To-Use
1.三种处理:降级、熔断、限流
2.实现,以cloud-providr-hystrix-payment8001为例
实现
1.依赖POM.XML
<!--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>
2.application.yml
server:
port: 8001
spring:
application:
name: cloud-provider-hystrix-payment
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
defaultZone: http://eureka7001.com:7001/eureka
3.主启动类 多加一个@EnableCircuitBreaker注解
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8001
{
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class, args);
}
4.service+controller 没什么特别
4.1 service正常的方法
public String paymentInfo_OK(Integer id)
{
return "线程池"+Thread.currentThread().getName()+"paymentInfo_ok,id"+id;
}
4.2service异常的方法
//程序异常,出错了
public String paymentInfo_TimeOut(Integer id)
{
int timeNumber=3000;
try {
TimeUnit.MILLISECONDS.sleep(timeNumber);
}
catch (InterruptedException e)
{ e.printStackTrace(); }
return "线程池: "+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t"+"错误" ;
}
4.3controller
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id)
{
String result = paymentService.paymentInfo_OK(id);
Log.info("*****result: "+result);
return result;
}
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id)
{
String result = paymentService.paymentInfo_TimeOut(id);
Log.info("*****result: "+result);
return result;
}
}
5.测试
a) 7001启动
b)success http://localhost:8001/payment/hystrix/ok/1
c)http://localhost:8001/payment/hystrix/timeout/1
每次调用耗费3秒钟

浙公网安备 33010602011771号