您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)怎么在SpringBoot中利用Feign調(diào)用其他服務(wù)接口,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
引入依賴
<!-- 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版本的對應(yīng)關(guān)系,老版本的Feign并不叫openfeign。由于我是用的SpringBoot版本是2.0x,所以openfeign使用了2.0x版本,若使用諸如2.1x或其他高版本的openfeign,在項目啟動時會報“抽象方法錯誤”這類的異常。
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接口,接口上方需要標注@FeignClient注解,該注解有url、name、value三個重要參數(shù)。其中name與value等效,必須填寫一個。在微服務(wù)環(huán)境下name或value填寫用于被注冊中心發(fā)現(xiàn)的服務(wù)名,例如調(diào)用的用戶服務(wù)叫userService則此處填寫userService,此使url可以不填寫,因為已經(jīng)指定了調(diào)用方。url則是直接指定服務(wù)的全路徑,若同時填寫url與name,則以url為準,name便被當作當前客戶端的名稱。
上面的示例并不屬于復雜的微服務(wù)環(huán)境,所以采用直接指定url來調(diào)用其他服務(wù)。
方法定義上與controller基本一致,需要注意post方法不能傳遞多個參數(shù),需要用map或?qū)ο筮M行封裝。
@Service @Slf4j public class DemoService { @Autowired private DemoClient demoClient; public void getUser(Long id){ demoClient.getUser("123", "abc"); } }
在需要調(diào)用其他服務(wù)的模塊中引入之前定義的接口即可。
調(diào)用https接口時會進行證書校驗,若沒有證書則會拋出No subject alternative names present異常,可以使用以下代碼來繞過證書校驗:
<!-- 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接口時繞過ssl證書驗證 * @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); } }
以上就是怎么在SpringBoot中利用Feign調(diào)用其他服務(wù)接口,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。