springcloud feign集成hystrix

人盡茶涼 提交于 2020-02-26 09:08:17

本章介绍feign集成hystrix 1、增加pom依赖`

		<dependency>
			<groupid>org.springframework.cloud</groupid>
			<artifactid>spring-cloud-starter-netflix-hystrix</artifactid>
		</dependency>

2、启动类中增加注解@EnableHystrix

3、增加feign接口fallback以及相关配置

DemoService

@ConditionalOnProperty(name = "app.host.abcurl")
@FeignClient(value = "demo-service", url = "${app.host.abcurl}" ,fallback = DemoServiceFallbackImpl .class)
public interface DemoService {

    @GetMapping("/v1/api/getCateData")
    ApiResponse<page<object>&gt; getCateData(@RequestParam Map<string,string> params);

    @GetMapping("/v1/api/getProductData")
    ApiResponse<page<detail>&gt; getProductData(@RequestParam Map<string,string> params);
}

DemoServiceFallbackImpl

@Slf4j
@Component
public class DemoServiceFallbackImpl implements DemoService {

    @Override
    public ApiResponse<page<object>&gt; getCateData (Map<string, string> params) {
        log.warn("DemoServiceFallbackImpl getCateData fail");
        return null;
    }

    @Override
    public ApiResponse<page<detail>&gt; getProductData(Map<string, string> params) {
        log.warn("DemoServiceFallbackImpl getProductData fail");
        return null;
    }
}

3、增加yml相关配置


feign:
  httpclient:
    enabled: true
  hystrix:
    enabled: true
hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 1000 //该配置需要比ribbon超时时间长
      circuitBreaker:
        requestVolumeThreshold: 1000
  threadpool:
    default:
      coreSize: 60
      maxQueueSize: 200
      queueSizeRejectionThreshold: 200

ribbon:
  ReadTimeout: 500
  ConnectTimeout: 500
  ExecTimeout: 500
  MaxAutoRetries: 1 //最好设置超时重试

这里如果需要查看hystrix监控,可以集成Hystrix Dashboard,详情参考 https://blog.csdn.net/u013408188/article/details/100074111

下图是我集成grafana 、prometheus的监控图 在这里插入图片描述在这里插入图片描述 </string,></page<detail></string,></page<object></string,string></page<detail></string,string></page<object>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!