1. 程式人生 > 其它 >spring cloud 之 feign

spring cloud 之 feign

spring cloud的feign是用來發送http,https請求的,他的特點:

1.可插拔的註解支援,包括Feign註解和JAX-RS註解。

2.支援可插拔的HTTP編碼器和解碼器。

3.支援Hystrix和它的Fallback。

4.支援Ribbon的負載均衡。

5.支援HTTP請求和響應的壓縮。

Feign是一個宣告式的Web Service客戶端,它的目的就是讓Web Service呼叫更加簡單。它整合了Ribbon和Hystrix,從而不需要開發者針對Feign對其進行整合。Feign還提供了HTTP請求的模板,通過編寫簡單的介面和註解,就可以定義好HTTP請求的引數、格式、地址等資訊。Feign會完全代理HTTP的請求,在使用過程中我們只需要依賴注入Bean,然後呼叫對應的方法傳遞引數即可。

以下程式碼是一個完整的maven專案,demo版:演示瞭如何利用feign向github傳送get請求。

package mr.li.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;


@SpringBootApplication
@EnableFeignClients
public class HelloWorld { public static void main(String[] args) { SpringApplication.run(HelloWorld.class, args); } }
package mr.li.hello.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import feign.Logger;

@Configuration
public class FeignConfigDemo { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
package mr.li.hello.demo;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import mr.li.hello.config.FeignConfigDemo;

@FeignClient(name = "github-client", url = "https://api.github.com", configuration = FeignConfigDemo.class)
public interface FeignDemo {

    @RequestMapping(value = "/search/repositories", method = RequestMethod.GET)
    String searchRepo(@RequestParam(name = "q") String param);
}
package mr.li.hello.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import mr.li.hello.demo.FeignDemo;

@RestController
public class FeignDemoController {

    @Autowired
    private FeignDemo feignDemo;
    
    @GetMapping(value = "/search/github")
    public String search(@RequestParam("str") String param) {
        String str1 = feignDemo.searchRepo(param);
        return str1;
    }
}
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.mr.li</groupId>
    <artifactId>hello-world</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <!-- 父依賴為spring2.1.6 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>
     <properties>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring Cloud OpenFeign的Starter的依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
server:
  port: 8888
  
spring:
  application:
    name: hello-world
logging:
     level:
       mr.li.hello.demo.FeignDemo: debug