Skip to content

wangqifox/spring-feign

Repository files navigation

spring-feign

Build Status License

spring-feign是一个用于接口调用的工具。

Background

Feign(https://github.com/OpenFeign/feign)是一个基于注解来生成HTTP请求,并且能自动处理请求返回的工具。

缺点是Feign不支持Spring,这意味着无法享受到Spring依赖注入的便利。spring-cloud-starter-openfeign组件对Feign进行了包装,让Feign可以方便的用于Spring。

不过spring-cloud-starter-openfeign组件依赖整套spring-cloud系统,只有项目中使用spring-cloud才能用spring-cloud-starter-openfeign组件来请求注册的服务。

很多情况下,我们需要调用一个第三方服务,此时spring-cloud-starter-openfeign组件就无法使用了。这个时候我们就需要一个方便的、能够简化http接口请求的工具。spring-feign就是这样的工具。

spring-feign是对spring-cloud-starter-openfeign组件的裁剪与修改,剥离其中有用的部分。

Usage

  1. pom.xml文件中添加feign-spring-boot-starter

    <dependency>
        <groupId>io.github.wangqifox</groupId>
        <artifactId>feign-spring-boot-starter</artifactId>
        <version>3.0-SNAPSHOT</version>
    </dependency>
    
  2. 在配置类中添加@EnableFeignClients注释

  3. 使用@FeignClient注释定义feign客户端:

    @FeignClient(name = "client", url = "http://127.0.0.1:8080")
    public interface Client {
        @GetMapping("/1")
        String get1();
    }

    注意:其中url可以是这样的形式#{my-service.url},然后可以在配置文件中添加my-service.url,这样client对象生成的时候就会去配置文件中读取my-service.url配置。

  4. 最后就可以在需要使用的地方注入feign客户端:

    @Autowired
    Client client;
  5. Clientget1()方法的调用会由代理类来完成,请求http://127.0.0.1:8080/1接口然后返回结果。

License

Apache License 2.0

About

combine feign with spring

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages