在独立库中使用 Feign 与 Springboot 应用程序 [英] Using Feign in a standalone library vs a Springboot application

查看:44
本文介绍了在独立库中使用 Feign 与 Springboot 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 Some-Micro-Service 创建一个客户端作为库(Some-Micro-Service-Client),这样它就可以很容易地包含在 Some-Other-Micro-Service 的 pom 中.

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.

我想使用 Feign,因为它使事情变得更容易,但我不确定这是否适用于我的架构.我看到的所有 Feign 示例都是从在 SpringBootAppplication 类上使用 @EnableFeignClient 注释开始的,但是由于我不希望必须启动"客户端库,所以我想知道是否可以只包含它在库中而不使用 EnableFeignClient 注释.

I would like to use Feign because it makes things easier, but I am not sure if this is possible with my architecture. 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 没有 @EnableFeingClient 注释.假设,我们希望从 this API 接收数据.在下面的示例中,我使用了 Feign CoreFeign Gson 依赖项.

Yes, you can use feign without @EnableFeingClient annotation. Assume, we want to receive data from this API. In below example I used Feign Core and Feign Gson dependencies.

首先我们需要创建类,我们将在其中得到json结果:

First of all we need to create class, in which we will get the json result:

public class TODO {
    private long id;
    private long userId;
    private String title;
    private boolean completed;

    \\ getters and setters ...
}

之后,我们使用未来的 rest-client 方法声明接口:

After that we declare the interface with the future rest-client methods:

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.

这篇关于在独立库中使用 Feign 与 Springboot 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆