前言
在微服务设计里,服务之间的调用是很正常的,通常我们使用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,以后开发服务对服务调用就可以解耦了!
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。
原文链接:https://www.cnblogs.com/lori/p/9138678.html
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 83
-
2025-05-25 30
-
2025-05-27 24
-
2025-05-25 80
-
2025-05-25 58
热门评论

