博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot~openfeign从此和httpClient说再见
阅读量:7141 次
发布时间:2019-06-29

本文共 2800 字,大约阅读时间需要 9 分钟。

在微服务设计里,服务之间的调用是很正常的,通常我们使用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,以后开发服务对服务调用就可以解耦了!

转载于:https://www.cnblogs.com/lori/p/9138678.html

你可能感兴趣的文章
gitlab新建用户、组、分支以及为项目分配相应权限
查看>>
Java RMI之HelloWorld篇
查看>>
Linux C/C++ 内存泄漏检测工具:Valgrind
查看>>
vCenter一些常见问题解决
查看>>
Oracle字段的默认值无效的原因
查看>>
C++函数的重载/覆盖/隐藏
查看>>
php 设置提交信息后自动替换敏感字符加单引号
查看>>
存储容灾的相关限制
查看>>
sql连表查询where后面的条件是否可以加在on后面
查看>>
mysql字符串转数字小计
查看>>
Android Studio第九期 - QQ计步器效果
查看>>
用xftp上传文件到虚拟机提示553错误
查看>>
配置Kdump和Crash心得
查看>>
此证书已在此前安装为一个证书权威机构
查看>>
test
查看>>
稳安快ghost win7 x64 sp1 超级精简版v2.0试用报告
查看>>
python
查看>>
安全笔记
查看>>
创建密码报错ERROR 1372 (HY000)--案例
查看>>
给用户设置密码
查看>>