Docker中maven插件怎么使用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
Docker最近在业内非常火。如果你现在还不知道Docker是什么,你可要小心喽。今后,你会发现自己正在以某种方式使用它。本文假设你已经有了Docker的基础。如果你现在对它还不是很熟悉,我确定你以后还会来读这篇文章。
Docker用于集成测试、复杂分布式系统演示,非常理想。甚至可以用于运行生产环境下的系统。它是一个开源的软件容器。你可以把它想像成一个非常轻的超级快的虚拟机。
得到"Integration testing with Maven and Docker"文章和Docker Java API项目的启发,我写了一个简单的可以管理Docker容器maven插件,Docker Maven Plugin。这个插件将会根据你的配置,在构建时启动容器,构建结束时停止容器并删除,如果本地找不到镜像,Docker会自动去中央仓库下载。
以下与Apache Camel的集成测试是被忽略的,因为测试需要一个Redis实例才可以执行:
package org.apache.camel.component.redis;
import org.apache.camel.impl.JndiRegistry;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
@Ignore
public class RedisProducerIntegrationTest extends RedisTestSupport {
private static final JedisConnectionFactory CONNECTION_FACTORY = new JedisConnectionFactory();
static {
CONNECTION_FACTORY.afterPropertiesSet();
}
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(CONNECTION_FACTORY);
redisTemplate.afterPropertiesSet();
registry.bind("redisTemplate", redisTemplate);
return registry;
}
@Test
public void shouldSetAString() throws Exception {
sendHeaders(
RedisConstants.COMMAND, "SET",
RedisConstants.KEY, "key1",
RedisConstants.VALUE, "value");
assertEquals("value", redisTemplate.opsForValue().get("key1"));
}
@Test
public void shouldGetAString() throws Exception {
redisTemplate.opsForValue().set("key2", "value");
Object result = sendHeaders(RedisConstants.KEY, "key2", RedisConstants.COMMAND, "GET");
assertEquals("value", result);
}
}
我们配置docker-maven-plugin使用一个Redis镜像同时让主机的6379端口映射到容器的6379端口:
<plugin>
<groupId>com.ofbizian</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<images>
<image>
<name>dockerfile/redis</name>
<hostConfig>
<![CDATA[
{
"PortBindings": {
"6379/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "6379"
}
]
}
}
]]>
</hostConfig>
</image>
</images>
</configuration>
<executions>
<execution>
<id>start-docker</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-docker</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/zjzhai/blog/215524