简体   繁体   English

在独立库和Springboot应用程序中使用Feign

[英]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 CoreFeign 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.

相关问题 Springboot 独立应用部署在生产中 - Springboot standalone application deploy in production Springboot独立JAR应用程序的JNDI配置 - JNDI configuration for Springboot standalone JAR application 在独立应用程序中使用OpenID - Using OpenID in Standalone Application 在独立应用程序中使用Spring - Using Spring in a standalone application springboot应用程序打包为库,在另一个springboot应用程序中使用 - Springboot application packaged as a library to be used in another springboot application 在独立springboot java应用程序的introscope中获取JDBC连接监控数据 - get JDBC connection monitoring data in introscope for standalone springboot java application 序列化与嵌入式数据库相比,可实现简单的独立应用程序 - Serialization vs Embedded Database for simple standalone application 在桌面独立应用程序中使用 OSGi - Using OSGi in a desktop standalone application 使用具有独立上下文和SpringBoot 1.2.5的MockMvcBuilders进行文件上传的单元测试 - Unit Test of file upload using MockMvcBuilders with standalone context and SpringBoot 1.2.5 使用 Springboot 和 feign 客户端进行 rest 调用时获取选择性字段作为响应 - Getting selective fields in response when making a rest call using Springboot and feign client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM