Java使用OpenFeign管理多個(gè)第三方服務(wù)調(diào)用
最近開(kāi)發(fā)了一個(gè)統(tǒng)一調(diào)度類(lèi)的項(xiàng)目,需要依賴(lài)多個(gè)第三方服務(wù),這些服務(wù)都提供了HTTP接口供我調(diào)用。
組件架構(gòu)
服務(wù)多、接口多,如何進(jìn)行第三方服務(wù)管理和調(diào)用就成了問(wèn)題。
常用的服務(wù)間調(diào)用往往采用zk、Eureka等注冊(cè)中心進(jìn)行服務(wù)管理(SpringBoot常使用SpringCloud)。OpenFeign也是SpringCloud的解決方案之一。我們單獨(dú)使用OpenFeign, 無(wú)需對(duì)原有第三方服務(wù)進(jìn)行改動(dòng),本服務(wù)開(kāi)發(fā)時(shí)的引入也很輕量。
下面給出我的用法。
應(yīng)用maven依賴(lài)引入maven依賴(lài):
<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èn)題。
配置和服務(wù)聲明第三方服務(wù)的地址通過(guò)配置來(lái)注入。
服務(wù)地址配置ThirdpartServiceConfig.java
@Data@Component@ConfigurationProperties(prefix = 'thirdpart-service')public class ThirdpartServiceConfig { private String serviceA; private String serviceB; private String serviceC;}
服務(wù)配置(超時(shí)時(shí)間配置等也可以寫(xiě)在這里) application.yaml
thirdpart-service: serviceA: http://****:***/ serviceB: http://****:***/ serviceC: http://****:***/第三方服務(wù)配置
因?yàn)槁暶鞣椒ㄒ恢拢允÷粤硕鄠€(gè)第三方聲明。 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ù)的配置之后,就可以進(jìn)行服務(wù)接口的聲明了。具體聲明方法可以參看OpenFeign文檔:# 翻譯: Spring Cloud Feign使用文檔
下面給出使用示例:
GET請(qǐng)求(feign可直接將返回的結(jié)果反序列化為本服務(wù)中定義的POJO)
@RequestLine('GET testGet?a={a}&b=')ServiceResp testGet(@Param('a') String a,@Param('b')String b);
GET 下載使用feign.Response接收請(qǐng)求結(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類(lèi)型,其他參數(shù)會(huì)被序列化為String,所以若第三方接口參數(shù)的值為POJO(或Map),可能會(huì)出錯(cuò)。 對(duì)于POJO參數(shù),若第三方參數(shù)名含有Java中不合法的屬性字符(如 ”-“,”#“,”.“等),可使用注解進(jìn)行序列化時(shí)的轉(zhuǎn)化。由于聲明Feign Client時(shí)使用的encoder是Gson,所以使用如下注解:
@SerializedName(value='aaa-bbb') private String aaaBbb;
如果使用的是其他序列化工具,改為對(duì)應(yīng)的注解即可。
小結(jié)使用聲明式的第三方和接口寫(xiě)法,基本覆蓋了請(qǐng)求第三方接口的需求,也易于拓展和管理。
我計(jì)劃在后續(xù)添加統(tǒng)一的鑒權(quán)、日志打印和異常捕獲處理功能,使依賴(lài)組件引入的風(fēng)險(xiǎn)更為可控。OpenFeign幫我們實(shí)現(xiàn)了服務(wù)聲明、接口聲明、HTTP請(qǐng)求發(fā)送和結(jié)果處理等邏輯,在項(xiàng)目需要調(diào)用多個(gè)第三方服務(wù)時(shí)可以使用。
到此這篇關(guān)于Java使用OpenFeign管理多個(gè)第三方服務(wù)調(diào)用的文章就介紹到這了,更多相關(guān)Java 第三方服務(wù)調(diào)用內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Intellij IDEA 2020.3 配置教程詳解2. idea給項(xiàng)目打war包的方法步驟3. IntelliJ IDEA刪除類(lèi)的方法步驟4. Python importlib模塊重載使用方法詳解5. ASP基礎(chǔ)入門(mén)第八篇(ASP內(nèi)建對(duì)象Application和Session)6. 兩行Javascript代碼生成UUID的方法7. IntelliJ IDEA設(shè)置編碼格式的方法8. 解決python中import文件夾下面py文件報(bào)錯(cuò)問(wèn)題9. 使用 kind 和 Docker 啟動(dòng)本地的 Kubernetes環(huán)境10. XML入門(mén)精解之結(jié)構(gòu)與語(yǔ)法
