前言
在微服務設計里,服務之間的調用是很正常的,通常我們使用httpclient來實現對遠程資源的調用,而這種方法需要知識服務的地址,業務接口地址等,而且需要等他開發完成后你才可以去調用它,這對于集成開發來說,不是什么好事 ,產生了a業務與b業務的強依賴性,那么我們如何進行解耦呢,答案就是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相關配置
1
2
3
4
5
6
7
8
9
10
11
12
|
//默認的一些文件路徑的配置 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 定義服務接口,定義偽方法,就是服務里的方法,你要知識方法參數和它的返回值,實現不用管,只在單元測試里mock就可以
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; /** * 模擬其他服務. */ @profile ( "!integtest" ) @feignclient (name = "servicename" ) public interface mockclient { @getmapping (path = "/balancesheet/{clientcode}" ) string balancesheet(string clientcode); } |
4 profile的作用
profile就是環境變量,你在類上通過activeprofile去激活它,在使用它時,有過profile注解來使用上,上面代碼中mockclient對象不能在integtest環境下使用。
5 添加mock實現,它是自動注入的,所以聲明@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 添加單元測試,注意在單元測試上一定要指定它的環境變量
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環境 @activeprofiles ( "integtest" ) public class javalinddayapplicationtests { @autowired mockclient mockclient; @test public void testmockclient() { assertequals(mockclient.balancesheet( "ok" ), "ok" ); } } |
運行測試后,mockclient將會被注入,它將使用mock實現類,因為只有mock實現類的profile是指向integtest環境的。
有了openfeign,以后開發服務對服務調用就可以解耦了!
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/lori/p/9138678.html