日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringCloud Openfeign

發布時間:2023/12/3 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloud Openfeign 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 一、 Openfeign簡介
    • 二、 Openfeign的實現
        • 1 創建 springcloudopenfeign 項目
        • 2 創建 feigncommons 子模塊
        • 3 創建 feignapi 子模塊
        • 4 創建 feignservice 子模塊 (Application server)
        • 5 創建 feignclient 子模塊 (Application client)
    • 三、 Openfeign的請求參數處理
    • 四、 Openfeign的性能優化
        • 1 GZIP簡介
        • 2 HTTP協議中的壓縮傳輸簡介
        • 3 在Openfeign技術中應用GZIP壓縮
    • 五、 配置Openfeign負載均衡請求超時時間

一、 Openfeign簡介

Openfeign是一種聲明式、模板化的HTTP客戶端(僅在Application Client中使用)。聲明式調用是指,就像調用本地方法一樣調用遠程方法,無需感知操作遠程http請求。
Spring Cloud的聲明式調用, 可以做到使用 HTTP請求遠程服務時能就像調用本地方法一樣的體驗,開發者完全感知不到這是遠程方法,更感知不到這是個HTTP請求。Openfeign的應用,讓Spring Cloud微服務調用像Dubbo一樣,Application Client直接通過接口方法遠程調用Application Service,而不需要通過常規的RestTemplate構造請求再解析返回數據。它解決了讓開發者調用遠程接口就跟調用本地方法一樣,無需關注與遠程的交互細節,更無需關注分布式環境開發。

1 使用Feign技術開發時的應用部署結構

二、 Openfeign的實現

1 創建 springcloudopenfeign 項目

1.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><packaging>pom</packaging><modules><module>feigncommons</module><module>feignapi</module><module>feignservice</module><module>feignclient</module></modules><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version></parent><groupId>com.bjsxt</groupId><artifactId>springcloudopenfeign</artifactId><version>1.0-SNAPSHOT</version><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>

2 創建 feigncommons 子模塊


2.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloudopenfeign</artifactId><groupId>com.bjsxt</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>feigncommons</artifactId></project>

2.2 com.bjsxt.feign.entity 創建實體類 User

package com.bjsxt.feign.entity;import java.io.Serializable; import java.util.Objects;public class User implements Serializable {private Integer id;private String name;private String password;public User(){}//省略get set toString equals方法

3 創建 feignapi 子模塊


3.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloudopenfeign</artifactId><groupId>com.bjsxt</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>feignapi</artifactId><dependencies><dependency><groupId>com.bjsxt</groupId><artifactId>feigncommons</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>

3.2 UserServiceAPI 接口

package com.bjsxt.feign.api;import com.bjsxt.feign.entity.User; import org.springframework.web.bind.annotation.*;import java.util.Map;/*** 類型命名隨意* 類型是用于約束微服務特性的。* 在約束請求參數的時候,Openfeign技術要特殊的限制要求:* 1、 簡單類型參數,必須使用@RequestParam注解描述,且需要提供請求參數命名。* 2、 自定義類型參數,必須使用@RequestBody注解描述,且自定義類型參數數量唯一。* 就是參數表中只有唯一的參數,且是自定義類型參數。** Application Service需要提供什么服務。* 服務1 - 保存用戶,方法名、參數表、返回值類型和請求路徑地址、請求方式。* 服務2 - 更新用戶,方法名、參數表、返回值類型和請求路徑地址、請求方式。* Application Client可以訪問什么遠程服務。* 服務1 - 保存用戶,方法名、參數表、返回值類型和請求路徑地址、請求方式。* 服務2 - 更新用戶,方法名、參數表、返回值類型和請求路徑地址、請求方式。*/ public interface UserServiceAPI {@PostMapping("/user/save") // 就是約束了只處理POST請求方式的RequestMapping注解。Map<String, Object> saveUser(@RequestBody User user);@PostMapping("/user/update")Map<String, Object> updateUser(@RequestBody User user); }

4 創建 feignservice 子模塊 (Application server)


4.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloudopenfeign</artifactId><groupId>com.bjsxt</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>feignservice</artifactId><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>com.bjsxt</groupId><artifactId>feignapi</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>

4.2 UserController 控制類

package com.bjsxt.feign.controller;import com.bjsxt.feign.api.UserServiceAPI; import com.bjsxt.feign.entity.User; import org.springframework.web.bind.annotation.RestController;import java.util.Arrays; import java.util.HashMap; import java.util.Map;@RestController public class UserController implements UserServiceAPI {@Overridepublic Map<String, Object> saveUser(User user) {Map<String, Object> result = new HashMap<>();System.out.println("保存用戶數據:" + user);result.put("message", "保存用戶數據成功");return result;}@Overridepublic Map<String, Object> updateUser(User user) {Map<String, Object> result = new HashMap<>();System.out.println("更新用戶數據:" + user);result.put("message", "更新用戶數據成功");return result;}}

4.3 application.yml 配置文件

server:port: 8082spring:application:name: feign-app-serviceeureka:client:service-url:defaultZone: http://localhost:8761/eureka/

4.4 啟動類(集群)

package com.bjsxt.feign;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class FeignServiceApp {public static void main(String[] args) {SpringApplication.run(FeignServiceApp.class, args);} } package com.bjsxt.feign;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class FeignServiceApp1 {public static void main(String[] args) {SpringApplication.run(FeignServiceApp1.class, args);} }

5 創建 feignclient 子模塊 (Application client)


5.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloudopenfeign</artifactId><groupId>com.bjsxt</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>feignclient</artifactId><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>com.bjsxt</groupId><artifactId>feignapi</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies></project>

5.2 UserController 控制類

package com.bjsxt.feign.client.controller;import com.bjsxt.feign.client.service.UserService; import com.bjsxt.feign.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController;import java.util.Map;@RestController public class UserController {@Autowiredprivate UserService userService;@GetMapping("/user/save")public Object save(User user){Map<String, Object> result = this.userService.saveUser(user);System.out.println(result);return result;}@GetMapping("/user/update")public Object update(User user){Map<String, Object> result = this.userService.updateUser(user);System.out.println(result);return result;}}

5.3 UserService 接口

package com.bjsxt.feign.client.service;import com.bjsxt.feign.api.UserServiceAPI; import org.springframework.cloud.openfeign.FeignClient;/*** 本地服務,是用于遠程訪問Application Service的本地服務接口。*/ @FeignClient("feign-app-service") public interface UserService extends UserServiceAPI { }

5.4 application.yml 配置文件

server:port: 8081compression:spring:application:name: feign-app-clienteureka:client:service-url:defaultZone: http://localhost:8761/eureka/feign-app-service: # 配置負載均衡ribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

5.5 啟動類

package com.bjsxt.feign.client;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients;/*** 需要提供一個新的注解* @EnableFeignClients - 開啟OpenFeign客戶端技術。掃描@FeignClient注解。* 默認掃描當前類所在包,及子包中所有的類型。*/ @SpringBootApplication @EnableFeignClients(basePackages = {"com.bjsxt.feign.client.service"}) public class FeignClientApp {public static void main(String[] args) {SpringApplication.run(FeignClientApp.class, args);} }

三、 Openfeign的請求參數處理

在Openfeign處理遠程服務調用時,傳遞參數是通過HTTP協議傳遞的,參數存在的位置是請求頭或請求體中。請求頭傳遞的參數必須依賴@RequestParam注解來處理請求參數,請求體傳遞的參數必須依賴@RequestBody注解來處理請求參數。

/*** 簡單類型的請求參數,如:字符串,8種基本類型,包裝類型,簡單類型數組* 可以通過請求頭和請求體傳遞,可以使用GET|POST請求方式來進行處理* Openfeign強制要求,簡單類型的請求參數,必須使用@RequestParam注解描述。*/ /*** 自定義類型請求參數處理* Openfeign強制要求,處理自定義類型請求參數的時候,必須使用@RequestBody注解描述。* @RequestBody注解強制約束: 一個方法的參數表中,只能有唯一的一個參數使用此注解描述。* Openfeign強制要求,所有的自定義類型請求參數,必須使用POST請求處理傳遞。* Openfeign處理自定義類型參數的時候:只能處理唯一的一個自定義類型參數對象,必須使用POST* 請求方式傳遞處理,對簡單類型數據無約束。*/

四、 Openfeign的性能優化

1 GZIP簡介

gzip介紹:

gzip是一種數據格式,采用用deflate算法壓縮數據;gzip是一種流行的數據壓縮算法,應用十分廣泛,尤其是在Linux平臺。

gzip能力:

當Gzip壓縮到一個純文本數據時,效果是非常明顯的,大約可以減少70%以上的數據大小。

gzip作用:

網絡數據經過壓縮后實際上降低了網絡傳輸的字節數,最明顯的好處就是可以加快網頁加載的速度。網頁加載速度加快的好處不言而喻,除了節省流量,改善用戶的瀏覽體驗外,另一個潛在的好處是Gzip與搜索引擎的抓取工具有著更好的關系。例如 Google就可以通過直接讀取gzip文件來比普通手工抓取更快地檢索網頁。

2 HTTP協議中的壓縮傳輸簡介

第一:客戶端向服務器請求頭中帶有:Accept-Encoding:gzip, deflate 字段,向服務器表示,客戶端支持的壓縮格式(gzip或者deflate),如果不發送該消息頭,服務器是不會壓縮的。
第二:服務端在收到請求之后,如果發現請求頭中含有Accept-Encoding字段,并且支持該類型的壓縮,就對響應報文壓縮之后返回給客戶端,并且攜帶Content-Encoding:gzip消息頭,表示響應報文是根據該格式壓縮過的。
第三:客戶端接收到響應之后,先判斷是否有Content-Encoding消息頭,如果有,按該格式解壓報文。否則按正常報文處理。

3 在Openfeign技術中應用GZIP壓縮

在Spring Cloud微服務體系中,一次請求的完整流程如下:
在整體流程中,如果使用GZIP壓縮來傳輸數據,涉及到兩次請求-應答。而這兩次請求-應答的連接點是Application Client,那么我們需要在Application Client中配置開啟GZIP壓縮,來實現壓縮數據傳輸。

3.1 只配置Openfeign請求-應答中的GZIP壓縮 (即Application Server 和 Application Client之間)

application.yml

server:port: 8081spring:application:name: feign-app-clienteureka:client:service-url:defaultZone: http://localhost:8761/eureka/feign-app-service: # 配置負載均衡ribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRulefeign:compression:request:enabled: true # 開啟請求GZIP壓縮mime-types: # 什么請求類型開啟GZIP壓縮 a,b,c- text/html- text/plain- text/xml- application/jsonmin-request-size: 512 # 請求容量超過這個閥值的時候,開啟壓縮。默認值2048字節response:enabled: true # 開啟響應GZIP壓縮。

3.2 配置全局GZIP壓縮 (即配置 瀏覽器與Application Client、Application Server 和 Application Client之間)

在全局配置文件中配置下述內容,來開啟所有請求-應答中的GZIP壓縮,這里使用的是Spring Boot中的GZIP技術。在Spring Boot中已經集成了GZIP壓縮技術,并對所有的請求-應答實現GZIP數據壓縮。工程中已經依賴了Spring Boot技術,所以在配置文件中可以開啟Spring Boot中的GZIP壓縮技術,對完整流程中所有相關的請求-應答開啟GZIP壓縮。

application.yml

server:port: 8081compression:enabled: true # 開啟全局GZIP壓縮mime-types: # 設置開啟GZIP壓縮的請求頭類型- text/html- text/plain- text/xml- application/json- application/javascript- application/xmlmin-response-size: 512 # 最小的響應容量閥值,超過這個容量,進行壓縮,默認2048字節。spring:application:name: feign-app-clienteureka:client:service-url:defaultZone: http://localhost:8761/eureka/feign-app-service: # 配置負載均衡ribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRulefeign:compression:request:enabled: true # 開啟請求GZIP壓縮mime-types: # 什么請求類型開啟GZIP壓縮 a,b,c- text/html- text/plain- text/xml- application/jsonmin-request-size: 512 # 請求容量超過這個閥值的時候,開啟壓縮。默認值2048字節response:enabled: true # 開啟響應GZIP壓縮。

五、 配置Openfeign負載均衡請求超時時間

Openfeign技術底層是通過Ribbon技術實現的,那么在負載均衡和超時時間配置上,主要對Ribbon的配置。

超時時間配置-----在Application Client應用的配置文件 application.yml 上:

server:port: 8081compression:enabled: true # 開啟全局GZIP壓縮mime-types: # 設置開啟GZIP壓縮的請求頭類型- text/html- text/plain- text/xml- application/json- application/javascript- application/xmlmin-response-size: 512 # 最小的響應容量閥值,超過這個容量,進行壓縮,默認2048字節。spring:application:name: feign-app-clienteureka:client:service-url:defaultZone: http://localhost:8761/eureka/feign-app-service: # 配置負載均衡ribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRulefeign:compression:request:enabled: true # 開啟請求GZIP壓縮mime-types: # 什么請求類型開啟GZIP壓縮 a,b,c- text/html- text/plain- text/xml- application/jsonmin-request-size: 512 # 請求容量超過這個閥值的時候,開啟壓縮。默認值2048字節response:enabled: true # 開啟響應GZIP壓縮。ribbon:ConnectTimeout: 1000 # 連接超時時間ReadTimeout: 3000 # 操作超時時間。必須大于等于ConnectTimeout。

總結

以上是生活随笔為你收集整理的SpringCloud Openfeign的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。