成都网站建设设计

将想法与焦点和您一起共享

如何使用openfeign框架进行解耦

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

我们提供的服务有:做网站、网站建设、微信公众号开发、网站优化、网站认证、遂溪ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的遂溪网站制作公司

前言

在微服务设计里,服务之间的调用是很正常的,通常我们使用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框架进行解耦”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


分享名称:如何使用openfeign框架进行解耦
当前路径:http://chengdu.cdxwcx.cn/article/gcddds.html