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秒钟

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值