spring boot openfeign從此和httpClient說再見詳析
前言
在微服務(wù)設(shè)計里,服務(wù)之間的調(diào)用是很正常的,通常我們使用httpClient來實現(xiàn)對遠程資源的調(diào)用,而這種方法需要知識服務(wù)的地址,業(yè)務(wù)接口地址等,而且需要等他開發(fā)完成后你才可以去調(diào)用它,這對于集成開發(fā)來說,不是什么好事 ,產(chǎn)生了A業(yè)務(wù)與B業(yè)務(wù)的強依賴性,那么我們?nèi)绾芜M行解耦呢,答案就是openfeign框架,它與是springcloudy里的一部分。
1 添加包引用
'org.springframework.cloud:spring-cloud-starter-openfeign',
注意:如果你沒有引用springcloudy版本人有太多,需要先聲明它
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
2 定義profile相關(guā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就可以
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注解
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)境變量
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");
}
}
運行測試后,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í)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
java中JsonObject與JsonArray轉(zhuǎn)換方法實例
在項目日常開發(fā)中常常會遇到JSONArray和JSONObject的轉(zhuǎn)換,很多公司剛?cè)肼毜男∶刃聲ㄔ谶@里,下面這篇文章主要給大家介紹了關(guān)于java中JsonObject與JsonArray轉(zhuǎn)換方法的相關(guān)資料,需要的朋友可以參考下2023-04-04
java多線程Synchronized實現(xiàn)可見性原理解析
這篇文章主要介紹了java多線程Synchronized實現(xiàn)可見性原理,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
關(guān)于Spring配置文件加載方式變化引發(fā)的異常詳解
這篇文章主要給大家介紹了關(guān)于Spring配置文件加載方式變化引發(fā)的異常的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-01-01
SpringMVC xml文件路徑在web.xml中的配置方式
這篇文章主要介紹了SpringMVC xml文件路徑在web.xml中的配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java如何解決發(fā)送Post請求報Stream?closed問題
這篇文章主要介紹了Java如何解決發(fā)送Post請求報Stream?closed問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Spring?Data?JPA框架的核心概念與Repository接口詳解
Spring?Data?JPA是Spring基于JPA規(guī)范的基礎(chǔ)上封裝的?套?JPA?應(yīng)?框架,可使開發(fā)者?極簡的代碼即可實現(xiàn)對數(shù)據(jù)庫的訪問和操作,本篇我們來了解Spring?Data?JPA框架的核心概念與Repository接口2022-04-04

