前言
在微服務(wù)設(shè)計里,服務(wù)之間的調(diào)用是很正常的,通常我們使用httpclient來實現(xiàn)對遠(yuǎn)程資源的調(diào)用,而這種方法需要知識服務(wù)的地址,業(yè)務(wù)接口地址等,而且需要等他開發(fā)完成后你才可以去調(diào)用它,這對于集成開發(fā)來說,不是什么好事 ,產(chǎn)生了a業(yè)務(wù)與b業(yè)務(wù)的強(qiáng)依賴性,那么我們?nèi)绾芜M(jìn)行解耦呢,答案就是openfeign框架,它與是springcloudy里的一部分。
1 添加包引用
1
|
'org.springframework.cloud:spring-cloud-starter-openfeign' , |
注意:如果你沒有引用springcloudy版本人有太多,需要先聲明它
1
2
3
4
5
|
dependencymanagement { imports { mavenbom "org.springframework.cloud:spring-cloud-dependencies:${springcloudversion}" } } |
2 定義profile相關(guān)配置
1
2
3
4
5
6
7
8
9
10
11
12
|
//默認(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ù)和它的返回值,實現(xiàn)不用管,只在單元測試?yán)飉ock就可以
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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)境變量,你在類上通過activeprofile去激活它,在使用它時,有過profile注解來使用上,上面代碼中mockclient對象不能在integtest環(huán)境下使用。
5 添加mock實現(xiàn),它是自動注入的,所以聲明@bean注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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 添加單元測試,注意在單元測試上一定要指定它的環(huán)境變量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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)行測試后,mockclient將會被注入,它將使用mock實現(xiàn)類,因為只有mock實現(xiàn)類的profile是指向integtest環(huán)境的。
有了openfeign,以后開發(fā)服務(wù)對服務(wù)調(diào)用就可以解耦了!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:https://www.cnblogs.com/lori/p/9138678.html