背景
本地调试Feign远程接口时, 依赖注册中心, Spring上下文环境, 在项目比较庞大的时候, 调试缓慢不利于问题排查; 下面的案例用最简单的配置来实现Feign的远程调用
maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
code
public class FeignDemo {
public static void main(String[] args) {
RemoteService service = Feign.builder()
.contract(new SpringMvcContract())
.requestInterceptor(requestTemplate -> {
//编辑请求信息,如header, body
}).target(RemoteService.class,"http://localhost:8080/user-server");
String userInfo = service.getUserInfo();
}
}
interface RemoteService{
@GetMapping("getUserInfo")
String getUserInfo();
}
构造Feign的配置可以按需加入编码解码器, 拦截器等, 通过动态代理的方式进行接口的调用, 具体参考Target实现类HardCodedTarget
![](https://img.haomeiwen.com/i18249296/6607c7597ebc6f61.png)
网友评论