java Hystrix配置

springCloud学习记录

Hystrix(服务降级)

Hystrix用于服务出现问题时的处理

关键注解

启动类:
@EnableHystrix //开启 Hystrix
@EnableCircuitBreaker // 开启熔断器
实现类:
@HystrixCommand // Hystrix规则
@HystrixProperty 具体规则
例:

@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
           @HystrixProperty(name="circuitBreaker.enabled",value="true"),// 是否开启断路器
           @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value="10"),// 请求次数
           @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="10000"),// 时间窗口期
           @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="60"),// 失败率到多少后跳闸
   })

controller:
@DefaultProperties // 全局配置使用的回调操作

pom

        <!--hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

application.yml

server:
  port: 80
eureka:
  client:
    register-with-eureka: false
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
feign:
  hystrix:
    # 在feign中开启Hystrix
    enabled: true

启动类

@SpringBootApplication
@EnableFeignClients
//@EnableHystrix 开启 Hystrix
@EnableCircuitBreaker // 开启熔断器
public class OrderHystrixMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderHystrixMain80.class, args);
    }
}

controller

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentHystrixService;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
// 全局的降级配置
@DefaultProperties(defaultFallback = "payment_Global_FullbackMethod")
public class OrderHystrixController {
    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_OK(id);
        return result;
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
	// 指定的使用降级内容,用自己的特殊的不用全局的
//    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
//            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "1500")
//    })
	// 表示使用降级
    @HystrixCommand
    public String paymentInfo_TIMEOUT(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_TIMEOUT(id);
        return result;
    }

    public String paymentInfo_TimeOutHandler(Integer id){
        return "80:线程池:"+ Thread.currentThread().getName()+"   paymentInfo_TimeOutHandler,id: "+id +";┭┮﹏┭┮";
    }

    // 全局fallback
    public String payment_Global_FullbackMethod(){
        return "Global异常";
    }
}

Service

@Component
// fallback 如果对应接口服务挂掉,后续操作指向
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id);

    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_TIMEOUT(@PathVariable("id") Integer id);

}
package com.atguigu.springcloud.service;
import org.springframework.stereotype.Component;

@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";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值