【问题标题】:Examples to use netflix-feign outside of spring-boot在 spring-boot 之外使用 netflix-feign 的示例
【发布时间】:2016-10-21 14:13:42
【问题描述】:
我正在寻找一个如何从 NO-spring-boot 应用程序中使用 netflix-feign 的示例。
我有一个现有的 SpringMVC (4.2) webapp。现在我用 Spring-boot + (eureka, feign) 构建了一些“微服务”,我想将它们用作 webapp 的后端服务。
提前致谢
【问题讨论】:
标签:
spring-mvc
netflix-eureka
netflix-feign
【解决方案1】:
在 github 存储库中,您可以看到一些使用 feign-client 的示例:
基本上,您需要创建一个使用feign 注释而不是Spring 注释的接口,一个示例是(您可以在 github 页面中查看更多信息):
public interface YourClient {
@RequestLine("POST /")
@Headers("Content-Type: application/json")
Response getSomething(@Param("id") String id);
}
然后要实例化您的 feign 客户端,需要使用其构建器 Feign。这很简单,并且可以配置:
YourClient yourClient = Feign.builder()
.decoder(new GsonDecoder()) // you could use Gson, Jackson,...
.target(YourClient .class, "https://your.url");
然后你就可以调用你想要的方法来使用它了:
yourClient.getSomething("myId");