spring boot openfeign从此和httpClient说再见详析

2025-05-29 0 70

前言

在微服务设计里,服务之间的调用是很正常的,通常我们使用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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 spring boot openfeign从此和httpClient说再见详析 https://www.kuaiidc.com/111480.html

相关文章

发表评论
暂无评论