Java使用OpenFeign管理多個第三方服務(wù)調(diào)用
最近開發(fā)了一個統(tǒng)一調(diào)度類的項目,需要依賴多個第三方服務(wù),這些服務(wù)都提供了HTTP接口供我調(diào)用。
組件架構(gòu)
服務(wù)多、接口多,如何進行第三方服務(wù)管理和調(diào)用就成了問題。
常用的服務(wù)間調(diào)用往往采用zk、Eureka等注冊中心進行服務(wù)管理(SpringBoot常使用SpringCloud)。OpenFeign也是SpringCloud的解決方案之一。我們單獨使用OpenFeign, 無需對原有第三方服務(wù)進行改動,本服務(wù)開發(fā)時的引入也很輕量。
下面給出我的用法。
應(yīng)用maven依賴引入maven依賴:
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-core</artifactId> <version>10.2.3</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-gson</artifactId> <version>10.2.3</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.8.0</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.8.0</version> </dependency>
其中,form相關(guān)引入是為了解決ContentType為application/x-www-form-urlencoded和multipart/form-data的編碼問題。
配置和服務(wù)聲明第三方服務(wù)的地址通過配置來注入。
服務(wù)地址配置ThirdpartServiceConfig.java
@Data@Component@ConfigurationProperties(prefix = 'thirdpart-service')public class ThirdpartServiceConfig { private String serviceA; private String serviceB; private String serviceC;}
服務(wù)配置(超時時間配置等也可以寫在這里) application.yaml
thirdpart-service: serviceA: http://****:***/ serviceB: http://****:***/ serviceC: http://****:***/第三方服務(wù)配置
因為聲明方法一致,所以省略了多個第三方聲明。 ThirdPartClientConfig.java
@Configurationpublic class ThirdParttClientConfig { @Resource private ThirdpartServiceConfig thirdpartServiceConfig; @Bean public ServiceAClient serviceAClient() {return Feign.builder() .encoder(new FormEncoder(new GsonEncoder())) .decoder(new GsonDecoder()) .target(ServiceAClient.class, thirdpartServiceConfig.getServiceA()); }}接口聲明和使用
完成了服務(wù)的聲明和服務(wù)的配置之后,就可以進行服務(wù)接口的聲明了。具體聲明方法可以參看OpenFeign文檔:# 翻譯: Spring Cloud Feign使用文檔
下面給出使用示例:
GET請求(feign可直接將返回的結(jié)果反序列化為本服務(wù)中定義的POJO)
@RequestLine('GET testGet?a={a}&b={b}')ServiceResp testGet(@Param('a') String a,@Param('b')String b);
GET 下載使用feign.Response接收請求結(jié)果
@RequestLine('GET export?exportId={exportId}')Response exportFromServiceA(@Param('exportId')String exportId);
@Resourceprivate ServiceAClient serviceAClient ;// 導(dǎo)出方法public void export(exportId) { Response serviceResponse = serviceserviceAClient.exportFromServiceA(exportId); Response.Body body = serviceResponse.body(); try(InputStream inputStream = body.asInputStream();// 處理獲取到的inputStream } catch (IOException e) { log.error('導(dǎo)出發(fā)生異常',e);}
POST application/json'
@RequestLine('POST /save') @Headers('Cofntent-Type: application/json') ServiceResp saveEntity(EntityPOJO entityPOJO);
POST form
@RequestLine('POST uqa/repo/qa/batch') @Headers('Content-Type:multipart/form-data') ServiceResp uploadFile(@Param('id')String id, @Param('batch_file') File file); 注意:除了file類型,其他參數(shù)會被序列化為String,所以若第三方接口參數(shù)的值為POJO(或Map),可能會出錯。 對于POJO參數(shù),若第三方參數(shù)名含有Java中不合法的屬性字符(如 ”-“,”#“,”.“等),可使用注解進行序列化時的轉(zhuǎn)化。由于聲明Feign Client時使用的encoder是Gson,所以使用如下注解:
@SerializedName(value='aaa-bbb') private String aaaBbb;
如果使用的是其他序列化工具,改為對應(yīng)的注解即可。
小結(jié)使用聲明式的第三方和接口寫法,基本覆蓋了請求第三方接口的需求,也易于拓展和管理。
我計劃在后續(xù)添加統(tǒng)一的鑒權(quán)、日志打印和異常捕獲處理功能,使依賴組件引入的風險更為可控。OpenFeign幫我們實現(xiàn)了服務(wù)聲明、接口聲明、HTTP請求發(fā)送和結(jié)果處理等邏輯,在項目需要調(diào)用多個第三方服務(wù)時可以使用。
到此這篇關(guān)于Java使用OpenFeign管理多個第三方服務(wù)調(diào)用的文章就介紹到這了,更多相關(guān)Java 第三方服務(wù)調(diào)用內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. PHP基礎(chǔ)之流程控制3——while/do-while2. Python TestSuite生成測試報告過程解析3. python3實現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)4. python爬蟲實戰(zhàn)之制作屬于自己的一個IP代理模塊5. 簡述JAVA同步、異步、阻塞和非阻塞之間的區(qū)別6. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題7. springboot的yml配置文件通過db2的方式整合mysql的教程8. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法9. moment轉(zhuǎn)化時間戳出現(xiàn)Invalid Date的問題及解決10. 利用單元測試對PHP代碼進行檢查
