欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot openfeign從此和httpClient說再見詳析

 更新時間:2018年06月12日 09:57:03   作者:張占嶺  
這篇文章主要給大家介紹了關于spring boot openfeign從此和httpClient說再見的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧

前言

在微服務設計里,服務之間的調(diào)用是很正常的,通常我們使用httpClient來實現(xiàn)對遠程資源的調(diào)用,而這種方法需要知識服務的地址,業(yè)務接口地址等,而且需要等他開發(fā)完成后你才可以去調(diào)用它,這對于集成開發(fā)來說,不是什么好事 ,產(chǎn)生了A業(yè)務與B業(yè)務的強依賴性,那么我們?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相關配置

//默認的一些文件路徑的配置
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 定義服務接口,定義偽方法,就是服務里的方法,你要知識方法參數(shù)和它的返回值,實現(xiàn)不用管,只在單元測試里MOCK就可以

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就是環(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ā)服務對服務調(diào)用就可以解耦了!

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • java中JsonObject與JsonArray轉換方法實例

    java中JsonObject與JsonArray轉換方法實例

    在項目日常開發(fā)中常常會遇到JSONArray和JSONObject的轉換,很多公司剛入職的小萌新會卡在這里,下面這篇文章主要給大家介紹了關于java中JsonObject與JsonArray轉換方法的相關資料,需要的朋友可以參考下
    2023-04-04
  • 詳解MyBatis中column屬性的總結

    詳解MyBatis中column屬性的總結

    在MyBatis的映射中有column這么一個屬性,我一直以為它映射的是數(shù)據(jù)庫表中的列名,但經(jīng)過學習發(fā)現(xiàn)他似乎映射的是SQL語句中的列名,或者說是查詢結果所得到的表的列名,這篇文章主要介紹了MyBatis中column屬性的總結,需要的朋友可以參考下
    2022-09-09
  • Java 8 引入lambda表達式的原因解析

    Java 8 引入lambda表達式的原因解析

    這篇文章主要介紹了Java 8 引入lambda表達式的原因解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • java多線程Synchronized實現(xiàn)可見性原理解析

    java多線程Synchronized實現(xiàn)可見性原理解析

    這篇文章主要介紹了java多線程Synchronized實現(xiàn)可見性原理,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • 關于Spring配置文件加載方式變化引發(fā)的異常詳解

    關于Spring配置文件加載方式變化引發(fā)的異常詳解

    這篇文章主要給大家介紹了關于Spring配置文件加載方式變化引發(fā)的異常的相關資料,文中通過實例代碼介紹的非常詳細,對大家學習或者使用Spring具有一定的參考學習價值,需要的朋友可以參考下
    2022-01-01
  • Java中字符串的一些常見方法分享

    Java中字符串的一些常見方法分享

    這篇文章主要介紹了Java中字符串的一些常見方法,需要的朋友可以參考下
    2014-02-02
  • SpringMVC xml文件路徑在web.xml中的配置方式

    SpringMVC xml文件路徑在web.xml中的配置方式

    這篇文章主要介紹了SpringMVC xml文件路徑在web.xml中的配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java如何解決發(fā)送Post請求報Stream?closed問題

    Java如何解決發(fā)送Post請求報Stream?closed問題

    這篇文章主要介紹了Java如何解決發(fā)送Post請求報Stream?closed問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Spring?Data?JPA框架的核心概念與Repository接口詳解

    Spring?Data?JPA框架的核心概念與Repository接口詳解

    Spring?Data?JPA是Spring基于JPA規(guī)范的基礎上封裝的?套?JPA?應?框架,可使開發(fā)者?極簡的代碼即可實現(xiàn)對數(shù)據(jù)庫的訪問和操作,本篇我們來了解Spring?Data?JPA框架的核心概念與Repository接口
    2022-04-04
  • 詳解spring security四種實現(xiàn)方式

    詳解spring security四種實現(xiàn)方式

    這篇文章主要介紹了詳解spring security四種實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11

最新評論