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 數(shù)組獲取最大和最小值的實(shí)例實(shí)現(xiàn)
這篇文章主要介紹了Java 數(shù)組獲取最大和最小值的實(shí)例實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09springboot Quartz動(dòng)態(tài)修改cron表達(dá)式的方法
這篇文章主要介紹了springboot Quartz動(dòng)態(tài)修改cron表達(dá)式的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09JAVA實(shí)現(xiàn)往字符串中某位置加入一個(gè)字符串
這篇文章主要介紹了JAVA實(shí)現(xiàn)往字符串中某位置加入一個(gè)字符串,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08解決MyBatisPlus的updateBatchById()批量修改失效問題
這篇文章主要介紹了解決MyBatisPlus的updateBatchById()批量修改失效問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08springmvc+shiro自定義過濾器的實(shí)現(xiàn)代碼
這篇文章主要介紹了springmvc+shiro自定義過濾器的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10springboot?serviceImpl初始化注入對(duì)象實(shí)現(xiàn)方式
這篇文章主要介紹了springboot?serviceImpl初始化注入對(duì)象實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05