Hystrix降级策略和超时调整

订单服务调用商品服务,商品服务接口需要做一些复杂的安全认证等需要2秒时间超时

produce-server:

package net.xdclass.product_service.controller;

import net.xdclass.product_service.domain.Product;
import net.xdclass.product_service.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/v1/product")
public class ProductController {
    @Value("${server.port}")
    private String port;

    @Autowired
    private ProductService productService;

    /**
     * 获取所有商品列表
     * @return
     */
    @RequestMapping("list")
    public Object list(){
        return productService.listProduct();
    }


    /**
     * 根据id查找商品详情
     * @param id
     * @return
     */
    @RequestMapping("find")
    public Object findById(@RequestParam("id") int id){
        //设置两秒的睡眠,模拟超时
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        Product product=productService.findById(id);
        Product result=new Product();
        /**
         * BeanUtils提供copyProperties方法,把product的属性全部复制给result
         * */
        BeanUtils.copyProperties(product,result);

        result.setName(result.getName()+"data from port"+port);
        return result;
    }



}

这时候订单服务调用商品服务接口会导致超时异常做告警处理,如何解决这问题呢?

order-server配置文件设置超时处理,4秒

超时时间调整

    hystrix:
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 4000

server:
  port: 8781


#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

#服务的名称
spring:
  application:
    name: order-service
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    timeout: 2000

#自定义负载均衡策略
product-service:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule


#修改调用超时时间
feign:
  hystrix:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 4000
        readTimeout: 4000

#把hystrix超时时间禁用
#hystrix:
#  command:
#    default:
#      execution:
#        timeout:
#          enabled: false

#execution.isolation.thread.timeoutInMilliseconds=4000

#设置超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 4000

2.策略调整:

  查看类HystrixCommandProperties

  1)execution.isolation.strategy   隔离策略
            THREAD 线程池隔离 (默认)
            SEMAPHORE 信号量
                信号量适用于接口并发量高的情况,如每秒数千次调用的情况,导致的线程开销过高,通常只适用于非网络调用,执行速度快

        2)execution.isolation.thread.timeoutInMilliseconds  超时时间
            默认 1000毫秒

        3)execution.timeout.enabled 是否开启超时限制 (一定不要禁用)

        4)execution.isolation.semaphore.maxConcurrentRequests 隔离策略为 信号量的时候,如果达到最大并发数时,后续请求会被拒绝,默认是10

官方文档:https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.strategy

原文地址:https://www.cnblogs.com/big-cut-cat/p/9907919.html