spring boot openfeign從此和httpClient說(shuō)再見(jiàn)詳析
前言
在微服務(wù)設(shè)計(jì)里,服務(wù)之間的調(diào)用是很正常的,通常我們使用httpClient來(lái)實(shí)現(xiàn)對(duì)遠(yuǎn)程資源的調(diào)用,而這種方法需要知識(shí)服務(wù)的地址,業(yè)務(wù)接口地址等,而且需要等他開(kāi)發(fā)完成后你才可以去調(diào)用它,這對(duì)于集成開(kāi)發(fā)來(lái)說(shuō),不是什么好事 ,產(chǎn)生了A業(yè)務(wù)與B業(yè)務(wù)的強(qiáng)依賴性,那么我們?nèi)绾芜M(jìn)行解耦呢,答案就是openfeign框架,它與是springcloudy里的一部分。
1 添加包引用
'org.springframework.cloud:spring-cloud-starter-openfeign',
注意:如果你沒(méi)有引用springcloudy版本人有太多,需要先聲明它
dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
2 定義profile相關(guān)配置
//默認(rèn)的一些文件路徑的配置 sourceSets { integTest { java.srcDir file('src/test/java') resources.srcDir file('src/test/resources') } } task integTest(type: Test) { testClassesDirs = sourceSets.test.output.classesDirs classpath = sourceSets.test.runtimeClasspath }
3 定義服務(wù)接口,定義偽方法,就是服務(wù)里的方法,你要知識(shí)方法參數(shù)和它的返回值,實(shí)現(xiàn)不用管,只在單元測(cè)試?yán)颩OCK就可以
package test.lind.javaLindDay.feignClientDemo; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.context.annotation.Profile; import org.springframework.web.bind.annotation.GetMapping; /** * 模擬其他服務(wù). */ @Profile("!integTest") @FeignClient(name = "serviceName") public interface MockClient { @GetMapping(path = "/balanceSheet/{clientCode}") String balanceSheet(String clientCode); }
4 Profile的作用
profile就是環(huán)境變量,你在類上通過(guò)ActiveProfile去激活它,在使用它時(shí),有過(guò)Profile注解來(lái)使用上,上面代碼中MockClient對(duì)象不能在integTest環(huán)境下使用。
5 添加MOCK實(shí)現(xiàn),它是自動(dòng)注入的,所以聲明@Bean注解
package test.lind.javaLindDay; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import test.lind.javaLindDay.feignClientDemo.MockClient; @Configuration @Profile("integTest") public class MockClientTest { @Bean public MockClient mockClient() { MockClient client = mock(MockClient.class); when(client.balanceSheet( anyString())) .thenReturn("OK"); return client; } }
6 添加單元測(cè)試,注意在單元測(cè)試上一定要指定它的環(huán)境變量
package test.lind.javaLindDay; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import test.lind.javaLindDay.feignClientDemo.MockClient; @RunWith(SpringRunner.class) @SpringBootTest //指定profile環(huán)境 @ActiveProfiles("integTest") public class JavaLindDayApplicationTests { @Autowired MockClient mockClient; @Test public void testMockClient() { assertEquals(mockClient.balanceSheet("OK"), "OK"); } }
運(yùn)行測(cè)試后,MockClient將會(huì)被注入,它將使用Mock實(shí)現(xiàn)類,因?yàn)橹挥蠱ock實(shí)現(xiàn)類的Profile是指向integtest環(huán)境的。
有了openfeign,以后開(kāi)發(fā)服務(wù)對(duì)服務(wù)調(diào)用就可以解耦了!
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
java中JsonObject與JsonArray轉(zhuǎn)換方法實(shí)例
在項(xiàng)目日常開(kāi)發(fā)中常常會(huì)遇到JSONArray和JSONObject的轉(zhuǎn)換,很多公司剛?cè)肼毜男∶刃聲?huì)卡在這里,下面這篇文章主要給大家介紹了關(guān)于java中JsonObject與JsonArray轉(zhuǎn)換方法的相關(guān)資料,需要的朋友可以參考下2023-04-04java多線程Synchronized實(shí)現(xiàn)可見(jiàn)性原理解析
這篇文章主要介紹了java多線程Synchronized實(shí)現(xiàn)可見(jiàn)性原理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12關(guān)于Spring配置文件加載方式變化引發(fā)的異常詳解
這篇文章主要給大家介紹了關(guān)于Spring配置文件加載方式變化引發(fā)的異常的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01SpringMVC xml文件路徑在web.xml中的配置方式
這篇文章主要介紹了SpringMVC xml文件路徑在web.xml中的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java如何解決發(fā)送Post請(qǐng)求報(bào)Stream?closed問(wèn)題
這篇文章主要介紹了Java如何解決發(fā)送Post請(qǐng)求報(bào)Stream?closed問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Spring?Data?JPA框架的核心概念與Repository接口詳解
Spring?Data?JPA是Spring基于JPA規(guī)范的基礎(chǔ)上封裝的?套?JPA?應(yīng)?框架,可使開(kāi)發(fā)者?極簡(jiǎn)的代碼即可實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的訪問(wèn)和操作,本篇我們來(lái)了解Spring?Data?JPA框架的核心概念與Repository接口2022-04-04詳解spring security四種實(shí)現(xiàn)方式
這篇文章主要介紹了詳解spring security四種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11