报错的原因是feign默认使用ribbon作为负载均衡调用的:
我的feign调用类如下:
-
package .blog_admin.feign;
-
-
import .blog_admin.;
-
import .blog_common.;
-
import .blog_common.;
-
import .blog_common.;
-
import .blog_config.;
-
import ;
-
import .annotation.GetMapping;
-
import .annotation.PathVariable;
-
-
/***
-
* @ClassName: PictureFeignClient
-
* @Description: picture feign调用
-
* @Author: wm_yu
-
* @Create_time: 16:39 2020-3-26
-
*/
-
@FeignClient(value = CommonConstant.PICTURE_MODULE_NAME, configuration = , fallbackFactory = )
-
public interface PictureFeignClient {
-
-
@GetMapping("/web/picture/{id}")
-
Result<TFileDO> get(@PathVariable("id") Long id);
-
-
}
feign被调用的服务的名称:
consul注册服务列表:
没有问题.
大部分网上的解决方法是:
但是我的注册中心是consul,解决方法如下:
-
=false
-
# 示例:=192.168.26.183:8777
-
{替换成需要调用的服务名}.={替换成需要调用服务的IP:端口号}
在调用方(feign服务调用端增加上面的配置): 图中圈出来的配置,
再次访问:
可以看到调用成功了.
服务降级和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 .blog_admin.;
-
-
import .blog_admin.;
-
import .blog_admin.;
-
import ;
-
import .slf4j.Slf4j;
-
import ;
-
-
/**
-
* 图片断路器工厂
-
*
-
* @author tangyi
-
* @date 2019/3/23 23:38
-
*/
-
@Component
-
@Slf4j
-
public class PictureClientFallbackFactory implements FallbackFactory<PictureFeignClient> {
-
-
@Override
-
public PictureFeignClient create(Throwable throwable) {
-
PictureClientFallbackImpl userServiceClientFallback = new PictureClientFallbackImpl();
-
//(throwable); todo
-
PictureClientFallbackImpl pictureClientFallback = new PictureClientFallbackImpl();
-
(throwable);
-
("调用图片服务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<TFileDO> 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;
-
}
-
}
好了,解决了