[英]Using Feign in a standalone library vs a Springboot application
I want to create a client for Some-Micro-Service as a library (Some-Micro-Service-Client) that way it can easily be included in the pom of Some-Other-Micro-Service. 我想为Some-Micro-Service创建一个客户端库(Some-Micro-Service-Client),这样就可以轻松地将其包含在Some-Other-Micro-service的pom中。
I would like to use Feign because it makes things easier, but I am not sure if this is possible with my architecture. 我想使用Feign,因为它使事情变得容易,但是我不确定我的体系结构是否可以实现。 All of the Feign examples that I see start with using the @EnableFeignClient annotation on the SpringBootAppplication class, but since I don't want the client library to have to be "started up" I want to know if it is possible just to include it in the library without using the EnableFeignClient annotation.
我看到的所有Feign示例都从在SpringBootAppplication类上使用@EnableFeignClient批注开始,但是由于我不希望客户端库必须“启动”,所以我想知道是否可以仅包含它在库中使用,而不使用EnableFeignClient批注。
Yes, you can use feign without @EnableFeingClient
annotation. 是的,您可以在没有
@EnableFeingClient
批注的情况下使用伪装 。 Assume, we want to receive data from this API. 假设,我们想从接收数据这个 API。 In below example I used Feign Core and Feign Gson dependencies.
在下面的示例中,我使用了Feign Core和Feign Gson依赖项。
First of all we need to create class, in which we will get the json result: 首先,我们需要创建类,在其中我们将获得json结果:
public class TODO {
private long id;
private long userId;
private String title;
private boolean completed;
\\ getters and setters ...
}
After that we declare the interface with the future rest-client methods: 之后,我们使用将来的rest-client方法声明接口:
public interface TaskApi {
@RequestLine("GET /todos/{id}")
TODO getTODO(@Param("id") int id);
}
And in conclusion let's build desired rest client and make test request: 最后,让我们构建所需的其余客户并提出测试请求:
public class FeignTest {
private static final String API_PATH = "https://jsonplaceholder.typicode.com";
public static void main(String[] args) {
TaskApi taskApi = Feign.builder()
.decoder(new GsonDecoder())
.target(TaskApi.class, API_PATH);
TODO todo = taskApi.getTODO(1);
}
}
For more information and possibilities you can read in official repository . 有关更多信息和可能性,您可以在官方资料库中阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.