SpringCloud OpenFeign-服務調用 1.OpenFeign介紹 https://github.com/spring-cloud/spring-cloud-openfeign OpenFeign是一個聲明式WebService客戶端,使用OpenFeign讓編寫Web Service ...
SpringCloud OpenFeign-服務調用
1.OpenFeign介紹
- OpenFeign是一個聲明式WebService客戶端,使用OpenFeign讓編寫Web Service客戶端更加簡單
- 它的使用方法是定義一個服務埠然後在上面添加註解
- OpenFeign也支持可插拔式的編碼器和解碼器
- SpringCloud對OpenFeign進行了封裝使其支持SpringMVC標準註解和HttpMessageConverters消息轉換器
- OpenFeign可以與Eureka和Ribbon組合使用以支持負載均衡
2.OpenFeign和Feign的區別
- Feign
- Feign是SpringCloud組件中的一個輕量級RESTful的Http服務客戶端
- Feign內置了Ribbon,用來做客戶端負載均衡,去調用服務註冊中心的服務
- Feign的使用方法是:使用Feign的註解定義介面,調用服務註冊中心的服務
- Feign支持的註解和用法請參考官方文檔:OpenFeign/feign: Feign makes writing java http clients easier (github.com)
- Feign本身不支持SpringMVC註解,它有一套自己的註解
- Feign集成了Ribbon、RestTemplate實現了負載均衡的執行Http調用,只不過對原有的方式(Ribbon+RestTemplate)進行了封裝,開發者不必手動使用RestTemplate調服務,而是定義一個介面,在這個介面中標註一個註解即可完成服務調用,這樣更加符合面向介面編程的宗旨,簡化了開發。
- OpenFeign
- OpenFeign是SpringCloud在Feign的基礎上支持了SpringMVC的註解,如@RequestMapping等
- OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping註解下的介面
- OpenFeign通過動態代理的方式產生實現類,實現類中做負載均衡並調用其他服務
- 一句話:OpenFeign就是在Feign的基礎上做了加強
3.OpenFeign應用實例
需求分析:如下,將原來使用Ribbon+RestTemplate實現:獲取服務+遠程調用+負載均衡,替換為使用OpenFeign來實現
![image-20230410184244233](https://img2023.cnblogs.com/blog/2192446/202304/2192446-20230411161212325-336549888.png)
參考 member-service-consumer-80 創建 member-service-consumer-openfeign-80(步驟參考以前)
(1)創建新模塊-member-service-consumer-openfeign-80
(2)修改 pom.xml:拷貝 member-service-consumer-80 的 pom.xml 依賴,並加入 openfeign-starter
<!-- 引入 openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
(3)創建application.yml
server:
port: 80
spring:
application:
name: e-commerce-consumer-openfeign-80
eureka:
client:
register-with-eureka: true #將自己註冊到EurekaServer
fetch-registry: true
service-url:
#將自己註冊都哪個EurekaServer
defaultZone: http://eureka9001.com:9001/eureka,http://eureka9002.com:9002/eureka
(4)創建主啟動類
package com.li.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author 李
* @version 1.0
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients//啟動OpenFeignClient
public class MemberConsumerOpenfeignApplication {
public static void main(String[] args) {
SpringApplication
.run(MemberConsumerOpenfeignApplication.class,args);
}
}
(5)創建介面,該介面最終是由OpenFeign來實現的(這裡是OpenFeign的核心)
@FeignClient(value = "MEMBER-SERVICE-PROVIDER")
指定遠程調用的地址別名- 註意這裡的
@GetMapping("/member/get/{id}")
指定要調用服務方
的哪個方法,路徑要和服務方的路徑匹配。這是OpenFeign支持的SpringMVC的註解
package com.li.springcloud.service;
import com.li.springcloud.entity.Member;
import com.li.springcloud.utils.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author 李
* @version 1.0
*/
@Component
//MEMBER-SERVICE-PROVIDER 是服務提供方[集群]註冊到EurekaServer的別名
//根據這個key,可以在EurekaServer提供的註冊信息中找到對應value,即真正的服務方地址:http://ip+port
@FeignClient(value = "MEMBER-SERVICE-PROVIDER")
public interface MemberFeignService {
//定義方法-遠程調用的介面
/**
* 1.遠程調用的方式是get
* 2.遠程調用的url http://ip+port/member/get/{id}
* 3.MEMBER-SERVICE-PROVIDER 是服務提供方[集群]註冊到EurekaServer的別名
* 4.OpenFeign會根據負載均衡來決定要掉用服務提供方的哪個節點(預設是輪詢)
* 5.OpenFeign的好處是支持了SpringMVC註解+使用介面解耦
* @param id
* @return
*/
@GetMapping("/member/get/{id}")
public Result<Member> getMemberById(@PathVariable("id") Integer id);
}
(6)創建MemberConsumerFeignController.java
這裡的@GetMapping("/member/consumer/openfeign/get/{id}")
,是消費方給瀏覽器的介面。
package com.li.springcloud.controller;
import com.li.springcloud.service.MemberFeignService;
import com.li.springcloud.utils.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author 李
* @version 1.0
*/
@RestController
public class MemberConsumerFeignController {
//裝配MemberFeignService,
//使用時該介面會對應一個代理對象,通過代理對象可以該介面的方法
@Resource
private MemberFeignService memberFeignService;
@GetMapping("/member/consumer/openfeign/get/{id}")
public Result getMemberById(@PathVariable("id") Integer id) {
return memberFeignService.getMemberById(id);
}
}
(7)啟動EurekaServer,服務消費方,啟動本模塊主程式,在瀏覽器中訪問:http://localhost:80/member/consumer/openfeign/get/5
,可以看到查詢數據成功,並且多次刷新會發現調用的介面是輪詢的。
![image-20230411170725815](https://img2023.cnblogs.com/blog/2192446/202304/2192446-20230411171017309-81962682.png)
註意事項和使用細節
-
OpenFeign使用特點是 微服務調用介面+@FeignClient,使用介面進行解耦
-
介面中的
@FeignClient(value = "MEMBER-SERVICE-PROVIDER")
,這裡的MEMBER-SERVICE-PROVIDER就是Eureka Server的服務提供方註冊的別名,底層會通過這個別名(key)找到真正的地址(value) -
介面中的方法,value是不能亂寫的,要根據服務消費方的url一致,否則無法訪問到服務消費方對應的方法
4.OpenFeign的日誌配置
4.1基本介紹
-
Feign提供了日誌列印功能,可以通過配置來調整日誌級別,從而對Feign介面的調用情況進行監控和輸出
-
日誌級別
- NONE:預設的,不顯示任何日誌
- BASIC:僅記錄請求方法、URL、響應狀態碼和執行時間
- HEADERS:除了BASIC中定義的信息之外,還有請求和響應的頭信息
- FULL:除了HEADERS中定義的信息之外,還有請求和響應的正文及元數據
4.2日誌配置-應用實例
(1)在member-service-consumer-80創建OpenFeignConfig.java
package com.li.springcloud.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 李
* @version 1.0
*/
@Configuration
public class OpenFeignConfig {
@Bean
public Logger.Level loggerLevel() {
return Logger.Level.FULL;
}
}
(2)修改application.yml
logging:
level:
#對MemberFeignService介面調用過程列印信息-Debug
com.li.springcloud.service.MemberFeignService: debug
常見的日誌級別有 5 種,分別是 error、warn、info、debug、trace
error:錯誤日誌,指比較嚴重的錯誤,對正常業務有影響,需要運維配置監控的;
warn:警告日誌,一般的錯誤,對業務影響不大,但是需要開發關註;
info:信息日誌,記錄排查問題的關鍵信息,如調用時間、出參入參等等;
debug:用於開發 DEBUG 的,關鍵邏輯裡面的運行時數據;
trace:最詳細的信息,一般這些信息只記錄到日誌文件中。
(3)重啟模塊,瀏覽器訪問消費模塊,後臺輸出如下:
![image-20230411173306168](https://img2023.cnblogs.com/blog/2192446/202304/2192446-20230411180100583-1073317874.png)
5.OpenFeign超時時間配置
OpenFeign調用服務的預設時長是1秒鐘,也就是如果超過1秒沒連接上或者超過1秒沒響應,那麼會相應的報錯。
![image-20230411181907078](https://img2023.cnblogs.com/blog/2192446/202304/2192446-20230411182451549-551990746.png)
而實際會因為業務的不同出現超出1秒的情況,這時我們需要調整超時時間:
Feign 的負載均衡底層用的就是 Ribbon。在application.yml中添加如下配置,超過8秒沒連接上報連接超時,如果超過8秒沒有響應,報請求超時
#全局配置
ribbon:
# 設置feign客戶端超時時間(OpenFeign預設支持ribbon),單位ms,預設超時時間為1s
ReadTimeout: 8000
#兩端連接所用時間
ConnectionTimeout: 8000