consul+feign调用报错Caused by: : Load balancer does not have available

时间:2024-10-09 20:27:27

报错的原因是feign默认使用ribbon作为负载均衡调用的:

 

我的feign调用类如下:

  

  1. package .blog_admin.feign;
  2. import .blog_admin.;
  3. import .blog_common.;
  4. import .blog_common.;
  5. import .blog_common.;
  6. import .blog_config.;
  7. import ;
  8. import .annotation.GetMapping;
  9. import .annotation.PathVariable;
  10. /***
  11. * @ClassName: PictureFeignClient
  12. * @Description: picture feign调用
  13. * @Author: wm_yu
  14. * @Create_time: 16:39 2020-3-26
  15. */
  16. @FeignClient(value = CommonConstant.PICTURE_MODULE_NAME, configuration = , fallbackFactory = )
  17. public interface PictureFeignClient {
  18. @GetMapping("/web/picture/{id}")
  19. Result<TFileDO> get(@PathVariable("id") Long id);
  20. }

 

feign被调用的服务的名称:

consul注册服务列表:

没有问题.

大部分网上的解决方法是:

 

但是我的注册中心是consul,解决方法如下:

  1. =false
  2. # 示例:=192.168.26.183:8777
  3. {替换成需要调用的服务名}.={替换成需要调用服务的IP:端口号}

在调用方(feign服务调用端增加上面的配置): 图中圈出来的配置,

 

再次访问:

可以看到调用成功了.

 

服务降级和feign调用前添加的配置,如下:

  1. package com.wm.blog_config.config;
  2. import feign.RequestInterceptor;
  3. import feign.RequestTemplate;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.context.annotation.Configuration;
  6. /***
  7. * @ClassName: CustomFeignConfig
  8. * @Description: 服务间调用携带Authorization请求头 todo feign调用
  9. * @Author: wm_yu
  10. * @Create_time: 16:50 2020-3-26
  11. */
  12. @Configuration
  13. @Slf4j
  14. public class CustomFeignConfig implements RequestInterceptor {
  15. @Override
  16. public void apply(RequestTemplate requestTemplate) {
  17. log.info("feign调用添加请求头");
  18. }
  19. }

 

  1. package .blog_admin.;
  2. import .blog_admin.;
  3. import .blog_admin.;
  4. import ;
  5. import .slf4j.Slf4j;
  6. import ;
  7. /**
  8. * 图片断路器工厂
  9. *
  10. * @author tangyi
  11. * @date 2019/3/23 23:38
  12. */
  13. @Component
  14. @Slf4j
  15. public class PictureClientFallbackFactory implements FallbackFactory<PictureFeignClient> {
  16. @Override
  17. public PictureFeignClient create(Throwable throwable) {
  18. PictureClientFallbackImpl userServiceClientFallback = new PictureClientFallbackImpl();
  19. //(throwable); todo
  20. PictureClientFallbackImpl pictureClientFallback = new PictureClientFallbackImpl();
  21. (throwable);
  22. ("调用图片服务feign异常:{}",throwable);
  23. return userServiceClientFallback;
  24. }
  25. }
  1. package com.wm.blog_admin.feign.fallback;
  2. import com.wm.blog_admin.feign.PictureFeignClient;
  3. import com.wm.blog_common.domain.TFileDO;
  4. import com.wm.blog_common.result.Result;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. * 日志断路器实现 todo
  9. *
  10. * @author tangyi
  11. * @date 2019/3/23 23:39
  12. */
  13. @Slf4j
  14. @Component
  15. public class PictureClientFallbackImpl implements PictureFeignClient {
  16. @Override
  17. public Result<TFileDO> get(Long id) {
  18. log.error("feign调用get异常");
  19. return null;
  20. }
  21. private Throwable throwable;
  22. public Throwable getThrowable() {
  23. return throwable;
  24. }
  25. public void setThrowable(Throwable throwable) {
  26. this.throwable = throwable;
  27. }
  28. }

好了,解决了