GithubHelp home page GithubHelp logo

spring-feign's Introduction

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

spring-feign's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.