注解式HTTP請求Feign (F版)


Spring Cloud 為開發者提供了在分布式系統中的一些常用的組件(例如配置管理,服務發現,斷路器,智能路由,微代理,控制總線,一次性令牌,全局鎖定,決策競選,分布式會話集群狀態)。使用Spring Cloud開發人員可以快速地完成實現這些模式的服務和應用程序。它們在任何分布式環境中都能很好地工作

Feign

注解式的 Feign 使得 Java HTTP 客戶端編寫更方便。Feign 靈感來源於安卓網絡編程框架 RetrofitJAXRS-2.0 和 WebSocket,支持可插拔編碼器和解碼器,降低 HTTP API 的復雜度,通過最少的資源和代碼來實現和 HTTP API 的連接。通過可定制的解碼器和錯誤處理,可以編寫任意的HTTP API。Spring Cloud Feign 封裝了 Ribbon 這一組件,所以在使用 Feign 同時還能提供負載均衡的功能,這一切只需要一個 @FeignClient 即可完成。

早期版本的 Feign 被 Spring Cloud 團隊集成在 spring-cloud-netflix 子項目下,但如今 Spring Cloud 團隊將 Spring Cloud Feign獨立成一個單獨的 spring-cloud-openfeign 項目

Try

准備三個工程,分別是 eureka-serverorder-serverproduct-server

Eureka Server

詳情參考第一章,或從文末的 GITHUB 鏈接獲取對應篇幅的完整代碼

Product Server

一個普通的 Eureka Client 即可,詳情參考上一章,或從文末的 GITHUB 鏈接獲取對應篇幅的完整代碼

Order Server

這個例子也是在上一章的基礎之上做了擴展

依賴

對比上一章,此處多了一個 spring-cloud-starter-openfeign 的依賴

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>

配置文件

在 src/main/resources 目錄下創建一個 bootstrap.yml 的文件,寫上 eureka 相關配置信息

1
2
3
4
5
6
7
8
9
10
11
12
server:
port: 7072
spring:
application:
name: order-server
eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
client:
service-url:
defaultZone: http://localhost:7071/eureka/

ProductClient 接口

創建一個 ProductClient ,是不是感覺和 XxxxService 看起來類似(用法都類似),都是接口文件只不過在這個文件的上方多了一個 @FeignClient 注解,多種寫法,總有一款適合你

  • name:指定 FeignClient 的名稱,該屬性會作為微服務的名稱,用於服務發現
  • value:同 name 字段互通
  • serviceId:指定服務ID,每個注冊到注冊中心上的客戶端都會有對應的 serviceId 一般是 spring.application.name,與 name 和 value 互通
  • url: 一般用於調試,可以指定一個詳細地址(http://localhost:8080/products)
  • path: 請求統一路徑,可以看成 @RequestMapping("/products")
  • decode404:404 錯誤時,調用 decoder 進行解碼,否則拋出 FeignException
  • fallback:發生錯誤時,回調 hystrix 類/方法(后面會詳細介紹)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.battcn.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
* @author Levin
* @since 2018/9/26 0026
*/
@FeignClient(name = "product-server/products", decode404 = true)
//@FeignClient(name = "products", url = "http://localhost:7073/products")
//@FeignClient(value = "product", serviceId = "product-server", path = "/products", decode404 = true)
public interface ProductClient {

/**
* 根據產品ID查詢產品信息
*
* @param productId ID
* @return 查詢結果
*/
@GetMapping("/{product_id}")
String selectProductById(@PathVariable("product_id") Long productId);
}

OrderController

直接使用 @Autowired 注入進去即可,然后調用就好了,對比較 Ribbon 這里我們看不到 RestTemplate 的代碼了,也無需自己做解碼映射,Spring Cloud Feign 默認都替我們實現好了,我們只需要遵循既定的標准即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.battcn.controller;

import com.battcn.api.ProductClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author Levin
* @since 2018/9/26 0026
*/
@RestController
@RequestMapping("/orders")
public class OrderController {

@Autowired
private ProductClient productClient;


@GetMapping
public String query() {
return this.productClient.selectProductById(10L);
}
}

主函數

通過 @EnableFeignClients 注解開啟對 Feign 的支持,用習慣 Dubbo 的朋友喜歡將 API 打包成獨立的 JAR ,這個時候需要指定 basePackage 屬性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.battcn;

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

/**
* @author Levin
*/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class OrderApplication {

public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM