consul+feign调用报错Caused by: com.netflix.client.ClientException: Load balancer does not have available

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

consul+feign调用报错Caused by: com.netflix.client.ClientException: Load balancer does not have available_第1张图片

 

我的feign调用类如下:

  

package com.wm.blog_admin.feign;

import com.wm.blog_admin.feign.factory.PictureClientFallbackFactory;
import com.wm.blog_common.constatnt.CommonConstant;
import com.wm.blog_common.domain.TFileDO;
import com.wm.blog_common.result.Result;
import com.wm.blog_config.config.CustomFeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/***
 * @ClassName: PictureFeignClient
 * @Description: picture feign调用
 * @Author: wm_yu
 * @Create_time: 16:39 2020-3-26
 */
@FeignClient(value = CommonConstant.PICTURE_MODULE_NAME, configuration = CustomFeignConfig.class, fallbackFactory = PictureClientFallbackFactory.class)
public interface PictureFeignClient {

    @GetMapping("/web/picture/{id}")
    Result get(@PathVariable("id") Long id);

}

consul+feign调用报错Caused by: com.netflix.client.ClientException: Load balancer does not have available_第2张图片

 

feign被调用的服务的名称:

consul+feign调用报错Caused by: com.netflix.client.ClientException: Load balancer does not have available_第3张图片

consul注册服务列表:

consul+feign调用报错Caused by: com.netflix.client.ClientException: Load balancer does not have available_第4张图片

没有问题.

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

consul+feign调用报错Caused by: com.netflix.client.ClientException: Load balancer does not have available_第5张图片

 

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

spring.cloud.consul.ribbon.enabled=false
# 示例:hello-world.ribbon.listOfServers=192.168.26.183:8777
{替换成需要调用的服务名}.ribbon.listOfServers={替换成需要调用服务的IP:端口号}

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

consul+feign调用报错Caused by: com.netflix.client.ClientException: Load balancer does not have available_第6张图片

 

再次访问:

consul+feign调用报错Caused by: com.netflix.client.ClientException: Load balancer does not have available_第7张图片

可以看到调用成功了.

 

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

package com.wm.blog_config.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

/***
 * @ClassName: CustomFeignConfig
 * @Description:  服务间调用携带Authorization请求头  todo   feign调用
 * @Author: wm_yu
 * @Create_time: 16:50 2020-3-26
 */
@Configuration
@Slf4j
public class CustomFeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
      log.info("feign调用添加请求头");
    }
}

 

package com.wm.blog_admin.feign.factory;

import com.wm.blog_admin.feign.PictureFeignClient;
import com.wm.blog_admin.feign.fallback.PictureClientFallbackImpl;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 图片断路器工厂
 *
 * @author tangyi
 * @date 2019/3/23 23:38
 */
@Component
@Slf4j
public class PictureClientFallbackFactory implements FallbackFactory {

    @Override
    public PictureFeignClient create(Throwable throwable) {
        PictureClientFallbackImpl userServiceClientFallback = new PictureClientFallbackImpl();
        //userServiceClientFallback.setThrowable(throwable);   todo
        PictureClientFallbackImpl pictureClientFallback = new PictureClientFallbackImpl();
        pictureClientFallback.setThrowable(throwable);
        log.info("调用图片服务feign异常:{}",throwable);
        return userServiceClientFallback;
    }
}
package com.wm.blog_admin.feign.fallback;

import com.wm.blog_admin.feign.PictureFeignClient;
import com.wm.blog_common.domain.TFileDO;
import com.wm.blog_common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 日志断路器实现   todo
 *
 * @author tangyi
 * @date 2019/3/23 23:39
 */
@Slf4j
@Component
public class PictureClientFallbackImpl implements PictureFeignClient {
    @Override
    public Result get(Long id) {
        log.error("feign调用get异常");
        return null;
    }

    private Throwable throwable;
    
    public Throwable getThrowable() {
        return throwable;
    }

    public void setThrowable(Throwable throwable) {
        this.throwable = throwable;
    }
}

好了,解决了

你可能感兴趣的:(Java)