栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java Hystrix配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java Hystrix配置

java Hystrix配置
  • springCloud学习记录
  • Hystrix
  • 关键注解
  • pom
  • application.yml
  • 启动类
  • controller
  • Service

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
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        
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";
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号