美文网首页
本地调用Feign远程接口

本地调用Feign远程接口

作者: 清蒸三文鱼_ | 来源:发表于2021-04-26 18:03 被阅读0次

背景

本地调试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

HardCodedTarget

相关文章

网友评论

      本文标题:本地调用Feign远程接口

      本文链接:https://www.haomeiwen.com/subject/vbverltx.html