如何使用openfeign框架进行解耦

发布时间:2021-09-10 14:05:05 作者:小新
来源:亿速云 阅读:125

小编给大家分享一下如何使用openfeign框架进行解耦,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

前言

在微服务设计里,服务之间的调用是很正常的,通常我们使用httpClient来实现对远程资源的调用,而这种方法需要知识服务的地址,业务接口地址等,而且需要等他开发完成后你才可以去调用它,这对于集成开发来说,不是什么好事 ,产生了A业务与B业务的强依赖性,那么我们如何进行解耦呢,答案就是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 定义服务接口,定义伪方法,就是服务里的方法,你要知识方法参数和它的返回值,实现不用管,只在单元测试里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就是环境变量,你在类上通过ActiveProfile去激活它,在使用它时,有过Profile注解来使用上,上面代码中MockClient对象不能在integTest环境下使用。

5 添加MOCK实现,它是自动注入的,所以声明@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 添加单元测试,注意在单元测试上一定要指定它的环境变量

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,以后开发服务对服务调用就可以解耦了!

以上是“如何使用openfeign框架进行解耦”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Android T-MVVM深度解耦开发框架
  2. WCF利用企业库Unity框架的IOC层次解耦

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

openfeign

上一篇:C++中数组作数据成员的示例分析

下一篇:怎么通过重启路由的方法切换IP地址

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》