springCloud(17)-Hystrix-consumer实现+controller层降级+service层降级

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 访问

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值