极力推荐:一个Docker的maven插件

什么是Docker

Docker最近在业内非常火。如果你现在还不知道Docker是什么,你可要小心喽。今后,你会发现自己正在以某种方式使用它。本文假设你已经有了Docker的基础。如果你现在对它还不是很熟悉,我确定你以后还会来读这篇文章。

Docker用于集成测试、复杂分布式系统演示,非常理想。甚至可以用于运行生产环境下的系统。它是一个开源的软件容器。你可以把它想像成一个非常轻的超级快的虚拟机。

例子

得到“Integration testing with Maven and Docker”文章和Docker Java API项目的启发,我写了一个简单的可以管理Docker容器maven插件,Docker Maven Plugin。这个插件将会根据你的配置,在构建时启动容器,构建结束时停止容器并删除,如果本地找不到镜像,Docker会自动去中央仓库下载。

以下与Apache Camel的集成测试是被忽略的,因为测试需要一个Redis实例才可以执行:

 
 
  1. package org.apache.camel.component.redis;
  2.     import org.apache.camel.impl.JndiRegistry;
  3.     import org.junit.Ignore;
  4.     import org.junit.Test;
  5.     import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  6.     import org.springframework.data.redis.core.RedisTemplate;
  7.     @Ignore
  8.     public class RedisProducerIntegrationTest extends RedisTestSupport {
  9.         private static final JedisConnectionFactory CONNECTION_FACTORY = new JedisConnectionFactory();
  10.         static {
  11.             CONNECTION_FACTORY.afterPropertiesSet();
  12.         }
  13.         @Override
  14.         protected JndiRegistry createRegistry() throws Exception {
  15.             JndiRegistry registry = super.createRegistry();
  16.             redisTemplate = new RedisTemplate();
  17.             redisTemplate.setConnectionFactory(CONNECTION_FACTORY);
  18.             redisTemplate.afterPropertiesSet();
  19.             registry.bind("redisTemplate", redisTemplate);
  20.             return registry;
  21.         }
  22.         @Test
  23.         public void shouldSetAString() throws Exception {
  24.             sendHeaders(
  25.                     RedisConstants.COMMAND, "SET",
  26.                     RedisConstants.KEY, "key1",
  27.                     RedisConstants.VALUE, "value");
  28.             assertEquals("value", redisTemplate.opsForValue().get("key1"));
  29.         }
  30.         @Test
  31.         public void shouldGetAString() throws Exception {
  32.             redisTemplate.opsForValue().set("key2", "value");
  33.             Object result = sendHeaders(RedisConstants.KEY, "key2", RedisConstants.COMMAND, "GET");
  34.             assertEquals("value", result);
  35.         }
  36.     }

我们配置docker-maven-plugin使用一个Redis镜像同时让主机的6379端口映射到容器的6379端口:

 
 
  1.         com.ofbizian
  2.         docker-maven-plugin
  3.         1.0.0
  4.         
  5.             
  6.                 
  7.                     dockerfile/redis
  8.                     
  9.                         
  10.                             {
  11.                                 "PortBindings": {
  12.                                     "6379/tcp": [
  13.                                         {
  14.                                             "HostIp": "0.0.0.0",
  15.                                             "HostPort": "6379"
  16.                                         }
  17.                                     ]
  18.                                 }
  19.                             }
  20.                     ]]>
  21.                     
  22.                 
  23.             
  24.         
  25.         
  26.             
  27.                 start-docker
  28.                 pre-integration-test
  29.                 
  30.                     start
  31.                 
  32.             
  33.             
  34.                 stop-docker
  35.                 post-integration-test
  36.                 
  37.                     stop
  38.                 
  39.             
  40.         
  41.     

插件在编译期启动一个Docker容器,然后在集成测试结束期关闭容器。

这是一个非常简单的例子,但是这个插件支持更多的场景,如多镜像不同配置;将启动/关闭容器动作定义在不同的maven构建期间。Enjoy.

分享标题:极力推荐:一个Docker的maven插件
URL地址:http://www.hantingmc.com/qtweb/news27/373127.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联