SpringBoot使用Feign調(diào)用其他服務(wù)接口
使用SpringCloud的Feign組件能夠?yàn)榉?wù)間的調(diào)用節(jié)省編碼時(shí)間并提高開發(fā)效率,當(dāng)服務(wù)本身不復(fù)雜時(shí)可以單獨(dú)將該組件拿出使用。
引入依賴
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.0.4.RELEASE</version> </dependency>
引入SpringBoot打包的Feign依賴,需要注意的是Feign的版本與SpringBoot版本的對(duì)應(yīng)關(guān)系,老版本的Feign并不叫openfeign。由于我是用的SpringBoot版本是2.0x,所以openfeign使用了2.0x版本,若使用諸如2.1x或其他高版本的openfeign,在項(xiàng)目啟動(dòng)時(shí)會(huì)報(bào)“抽象方法錯(cuò)誤”這類的異常。
編寫接口作為服務(wù)調(diào)用入口
import com.bhz.demo.client.domain.req.ProductReceiveReq; import com.bhz.demo.client.domain.resp.MemberPointBaseResp; import com.bhz.demo.client.domain.resp.UserPointResp; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * @Author guomz * @create 2021/3/15 14:50 */ @FeignClient(url = "www.123.com", name = "demoClient") public interface DemoClient { @RequestMapping(value = "/demo/user/{uuid}/{name}", method = RequestMethod.GET) DemoBaseResp<DemoUserResp> getUser(@PathVariable("uuid") String uuid, @PathVariable("name") String name); @RequestMapping(value = "/demo/buy", method = RequestMethod.POST) DemoBaseResp buyProduct(DemoBuyReq req); }
Feign的服務(wù)調(diào)用編寫類似mybatis的dao接口,接口上方需要標(biāo)注@FeignClient注解,該注解有url、name、value三個(gè)重要參數(shù)。其中name與value等效,必須填寫一個(gè)。在微服務(wù)環(huán)境下name或value填寫用于被注冊(cè)中心發(fā)現(xiàn)的服務(wù)名,例如調(diào)用的用戶服務(wù)叫userService則此處填寫userService,此使url可以不填寫,因?yàn)橐呀?jīng)指定了調(diào)用方。url則是直接指定服務(wù)的全路徑,若同時(shí)填寫url與name,則以u(píng)rl為準(zhǔn),name便被當(dāng)作當(dāng)前客戶端的名稱。
上面的示例并不屬于復(fù)雜的微服務(wù)環(huán)境,所以采用直接指定url來調(diào)用其他服務(wù)。
方法定義上與controller基本一致,需要注意post方法不能傳遞多個(gè)參數(shù),需要用map或?qū)ο筮M(jìn)行封裝。
調(diào)用服務(wù)
@Service @Slf4j public class DemoService { @Autowired private DemoClient demoClient; public void getUser(Long id){ demoClient.getUser("123", "abc"); } }
在需要調(diào)用其他服務(wù)的模塊中引入之前定義的接口即可。
關(guān)于調(diào)用https接口
調(diào)用https接口時(shí)會(huì)進(jìn)行證書校驗(yàn),若沒有證書則會(huì)拋出No subject alternative names present異常,可以使用以下代碼來繞過證書校驗(yàn):
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-ribbon --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> <version>2.0.4.RELEASE</version> </dependency>
首先需要引入Ribbon依賴,在繞過證書的代碼中存在一些需要被注入的類屬于Ribbon。Ribbon的引入同樣需要注意版本問題。
import feign.Client; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory; import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.net.ssl.*; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; /**feign client配置 * @Author guomz * @create 2021/3/16 9:52 */ @Configuration public class FeignConfiguration { /** * 調(diào)用https接口時(shí)繞過ssl證書驗(yàn)證 * @param cachingFactory * @param clientFactory * @return * @throws NoSuchAlgorithmException * @throws KeyManagementException */ @Bean @ConditionalOnMissingBean public Client feignClient(@Qualifier("cachingLBClientFactory") CachingSpringLoadBalancerFactory cachingFactory, SpringClientFactory clientFactory) throws NoSuchAlgorithmException, KeyManagementException { SSLContext ctx = SSLContext.getInstance("TLSv1.2"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }; ctx.init(null, new TrustManager[]{tm}, null); return new LoadBalancerFeignClient(new Client.Default(ctx.getSocketFactory(), new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession sslSession) { return true; } }), cachingFactory, clientFactory); } }
之后是Feign的配置類,用來繞過https證書校驗(yàn)。
到此這篇關(guān)于SpringBoot使用Feign調(diào)用其他服務(wù)接口的文章就介紹到這了,更多相關(guān)SpringBoot Feign調(diào)用服務(wù)接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java ScheduledExecutorService定時(shí)任務(wù)案例講解
這篇文章主要介紹了Java ScheduledExecutorService定時(shí)任務(wù)案例講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08SpringBoot關(guān)閉druid的頁(yè)面和添加密碼驗(yàn)證方式
這篇文章主要介紹了SpringBoot關(guān)閉druid的頁(yè)面和添加密碼驗(yàn)證方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05本地編譯打包項(xiàng)目部署到服務(wù)器并且啟動(dòng)方式
這篇文章主要介紹了本地編譯打包項(xiàng)目部署到服務(wù)器并且啟動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02使用JWT作為Spring?Security?OAuth2的token存儲(chǔ)問題
這篇文章主要介紹了使用JWT作為Spring?Security?OAuth2的token存儲(chǔ),大家經(jīng)常使用的方法有兩種一種是使用JWT作為Token傳遞,一種是使用Redis存儲(chǔ)Token,資源服務(wù)器本地訪問Redis校驗(yàn)Token,需要的朋友可以參考下2021-12-12Java中BitMap(位圖)hutool版、IntMap、LongMap示例詳解
這篇文章主要給大家介紹了關(guān)于Java中BitMap(位圖)hutool版、IntMap、LongMap的相關(guān)資料,通過位運(yùn)算高效存儲(chǔ)和檢索整數(shù),相比于傳統(tǒng)數(shù)組,它們?cè)趦?nèi)存占用和性能上都有顯著優(yōu)勢(shì),需要的朋友可以參考下2024-12-12SpringBoot中的@EnableConfigurationProperties注解詳細(xì)解析
這篇文章主要介紹了SpringBoot中的@EnableConfigurationProperties注解詳細(xì)解析,如果一個(gè)配置類只配置@ConfigurationProperties注解,而沒有使用@Component或者實(shí)現(xiàn)了@Component的其他注解,那么在IOC容器中是獲取不到properties 配置文件轉(zhuǎn)化的bean,需要的朋友可以參考下2024-01-01hibernate-validator如何使用校驗(yàn)框架
高效、合理的使用hibernate-validator校驗(yàn)框架可以提高程序的可讀性,以及減少不必要的代碼邏輯,本文主要介紹了hibernate-validator如何使用校驗(yàn)框架,感興趣的可以了解一下2022-04-04