Feign【入門】

  • 2019 年 10 月 13 日
  • 筆記

feign簡介:

feign是一種聲明式,模板化的HTTP客戶端,spring cloud對feign進行了增強,使其支持SpringMvc的相關註解,並整合了ribbon做負載均衡。在spring cloud中使用feign做HTTP遠程服務請求,可以做到就像調用本地方法一樣,完全感知不到是在調用遠程方法,具體特性如下:

  • 可插拔的註解支持,包括feign註解和Jax-rs註解、
  • 支持可插拔的HTTP編碼器和解碼器、
  • 支持hystrix和它的fallback、
  • 支持ribbon負載均衡、
  • 支持HTTP請求和響應的壓縮、

1、創建feign-demo工程

1.1、工程依賴

<parent>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-parent</artifactId>          <version>2.0.3.RELEASE</version>          <relativePath/>  </parent>    <properties>          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>          <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>          <java.version>1.8</java.version>          <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>  </properties>    <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>
<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>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

1.2、工程啟動類:

import org.springframework.boot.SpringApplication;  import org.springframework.boot.autoconfigure.SpringBootApplication;  import org.springframework.cloud.openfeign.EnableFeignClients;    @SpringBootApplication  @EnableFeignClients  public class SpringCloudFeignApplication {        public static void main(String[] args) {          SpringApplication.run(SpringCloudFeignApplication.class, args);      }  }

可以看到啟動類上加了 @EnableFeignClients 註解,意思是當該工程在啟動的時候,會進行包掃描,掃描該啟動類包以下,子包中所有帶 @FeignClient 註解的類(包括啟動類所在包),並進行處理。

1.3、編寫相關代碼

HelloFeignService接口:

import cn.springcloud.book.feign.config.HelloFeignServiceConfig;  import org.springframework.cloud.openfeign.FeignClient;  import org.springframework.web.bind.annotation.RequestMapping;  import org.springframework.web.bind.annotation.RequestMethod;    //https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/forecast.json 彩雲天氣API  @FeignClient(name = "caiyunapp", url = "https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552", configuration = HelloFeignServiceConfig.class)  public interface HelloFeignService {          @RequestMapping(value = "/forecast.json", method = RequestMethod.GET)      String searchRepo();    }

如上所示,@FeignClient註解手動指定了URL,最終會根據指定的URL和@RequestMapping對應的方法轉換成完整的請求地址。如下:https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/forecast.json

HelloFeignServiceConfig配置類:

import feign.Logger;  import org.springframework.context.annotation.Bean;  import org.springframework.context.annotation.Configuration;    @Configuration  public class HelloFeignServiceConfig {        /**       * Logger.Level 的具體級別如下:       * NONE:不記錄任何信息       * BASIC:僅記錄請求方法、URL以及響應狀態碼和執行時間       * HEADERS:除了記錄 BASIC級別的信息外,還會記錄請求和響應的頭信息       * FULL:記錄所有請求與響應的明細,包括頭信息、請求體、元數據       *       * @return       */      @Bean      Logger.Level feignLoggerLevel() {          return Logger.Level.FULL;      }    }

controller類:

import cn.springcloud.book.feign.service.HelloFeignService;  import org.springframework.beans.factory.annotation.Autowired;  import org.springframework.http.ResponseEntity;  import org.springframework.web.bind.annotation.GetMapping;  import org.springframework.web.bind.annotation.RequestParam;  import org.springframework.web.bind.annotation.RestController;    @RestController  public class HelloFeignController {        @Autowired      private HelloFeignService helloFeignService;        // 服務消費者對位提供的服務      @GetMapping(value = "/search")      public String searchGithubRepoByStr(@RequestParam("str") String queryStr) {          return helloFeignService.searchRepo(queryStr);      }    }

如上所示,controller類中注入了上面編寫的接口類,直接調用了相關方法。

2、啟動工程

2.1、執行命令:

mvn spring-boot:run

2.2、訪問:losthost:8080/search/

 

 說明訪問成功!