当前位置: 移动技术网 > IT编程>开发语言>Java > spring boot openfeign从此和httpClient说再见详析

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

前言

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网