本项目是将restful项目打包成可执行的war包,在docker中执行

环境介绍:

docker    1.10.3
jetty    8
jersey    1.19
关键配置:
1、pom.xml配置
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<systemProperties>
</systemProperties>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>jetty-classpath</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>org.eclipse.jetty,org.mortbay.jetty,javax.servlet,org.glassfish.web</includeGroupIds>
<excludeArtifactIds>servlet-api-3.0,jsp-api,jsp-impl,jstl</excludeArtifactIds>
<outputDirectory>
${project.build.directory}/${project.artifactId}
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>main-class-placement</id>
<phase>prepare-package</phase>
<configuration>
<tasks>
<echo message="*** Moving launcher.class..." />
<move todir="${project.build.directory}/${project.artifactId}/">
<fileset dir="${project.build.directory}/classes/">
<include name="Launcher.class" />
</fileset>
</move>
<echo message="*** Moving launcher.class done." />
<echo message="*** Removing *.SF, *.RSA in META-INF ..." />
<delete>
<fileset
dir="${project.build.directory}/${project.artifactId}/META-INF/"
includes="*.SF,*.RSA" />
</delete>
<echo message="*** Removing *.SF, *.RSA in META-INF done." />
<echo message="*** Copying logback-test.xml..." />
<copy todir="${project.build.directory}/${project.artifactId}/">
<fileset dir="${project.build.directory}/../src/main/resources/">
<include name="logback-test.xml" />
</fileset>
</copy>
<echo message="*** Copying logback-test.xml done." />
<echo message="*** Copying conf.properties ..." />
<copy todir="${project.build.directory}/${project.artifactId}/../">
<fileset dir="${project.build.directory}/../src/main/resources/">
<include name="conf.properties" />
</fileset>
</copy>
<echo message="*** Copying conf.properties done." />
<echo message="*** Copying run.sh ..." />
<copy todir="${project.build.directory}/${project.artifactId}/../">
<fileset dir="${project.build.directory}/../src/main/resources/">
<include name="run.sh" />
</fileset>
</copy>
<echo message="*** Copying run.sh done." />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifest>
<mainClass>Launcher</mainClass>
</manifest>
</archive>
<warSourceExcludes>docs/**</warSourceExcludes>
<packagingExcludes>docs/**</packagingExcludes>
</configuration>
</plugin>
<!-- <plugin> <groupId>org.codehaus.enunciate</groupId> <artifactId>maven-enunciate-plugin</artifactId>
<version>1.26.2</version> <executions> <execution> <goals> <goal>assemble</goal>
</goals> <configuration> <configFile>src/main/resources/enunciate.xml</configFile>
</configuration> </execution> </executions> </plugin> -->
</plugins>
</build>

2、启动类

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.Properties;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public final class Launcher {
public static void main(String[] args) throws Exception {
ProtectionDomain domain = Launcher.class.getProtectionDomain();
URL location = domain.getCodeSource().getLocation();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(location.toExternalForm());
//as enumerated from http://jira.codehaus.org/browse/JETTY-1256
String[] configurations = new String[]{
"org.eclipse.jetty.webapp.WebInfConfiguration"
,"org.eclipse.jetty.webapp.WebXmlConfiguration"
,"org.eclipse.jetty.webapp.MetaInfConfiguration"
,"org.eclipse.jetty.webapp.FragmentConfiguration"
,"org.eclipse.jetty.plus.webapp.EnvConfiguration"
//,"org.eclipse.jetty.plus.webapp.Configuration"
,"org.eclipse.jetty.annotations.AnnotationConfiguration"
,"org.eclipse.jetty.webapp.JettyWebXmlConfiguration"
//,"org.eclipse.jetty.annotations.ContainerInitializerConfiguration"
};
webapp.setAttribute("org.eclipse.jetty.webapp.configuration", configurations);
webapp.setConfigurationClasses(configurations);
int port = 8080;
try{
//NOTE: default port in CONFIGPATH file is 8383
port = Integer.parseInt( load(new File(System.getProperty("CONFIGPATH"))).getProperty("jetty.port"));
}catch(Exception e){
e.printStackTrace();
System.out.println("ERROR: Invalid jetty.port value in configuration file.");
}
Server server = new Server(port);
server.setHandler(webapp);
server.start();
server.join();
}
private static Properties load(File propsFile) throws IOException {
Properties props = new Properties();
FileInputStream fis = null;
try{
fis = new FileInputStream(propsFile);
props.load(fis);
}finally{
try{
fis.close();
}catch(Exception e){
}
}
return props;
}
}

3、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>Rest_Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>cn.firewarm.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Rest_Servlet</servlet-name>
<!-- Redirect any calls to our jersey servlet -->
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
<!-- 添加解决跨域的代码配置,基于pom中有cors-filter的依赖 ,begin -->
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, PUT, DELETE,OPTIONS</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified,Authorization</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>Set-Cookie</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 添加解决跨域的代码配置,基于pom中有cors-filter的依赖 ,end -->
<display-name>Archetype Created Web Application</display-name>
</web-app>

4、配置文件conf.properties

jetty.port=8080
database.url=
database.driver_class=
database.user=
database.password=
hibernate.show_sql=false

5、Dockerfile配置
FROM isuper/java-oracle:jre_7
MAINTAINER Liuyg <liuyg@liuyingguang.cn>
# Expose the API port
EXPOSE 8080
ADD target target
RUN set -x chmod 775 /target/*.sh
# Run the JAR
CMD java -DCONFIGPATH=./target/conf.properties -jar /target/docker-jetty-jersey1.x.war

6、jenkins配置,请参考我的其他文章:

jenkins构建Docker 镜像(基于Jenkins的Docker镜像及Jenkins插件):http://blog.csdn.net/gsying1474/article/details/51126522

docker1.10.3-jetty8-jersey1.x 构建微服务相关推荐

  1. Spring Cloud构建微服务架构:服务容错保护(Hystrix断路器)

    断路器 断路器模式源于Martin Fowler的Circuit Breaker一文."断路器"本身是一种开关装置,用于在电路上保护线路过载,当线路中有电器发生短路时," ...

  2. Spring Cloud构建微服务架构(七)消息总线(续:Kafka)

    Spring Cloud Bus除了支持RabbitMQ的自动化配置之外,还支持现在被广泛应用的Kafka.在本文中,我们将搭建一个Kafka的本地环境,并通过它来尝试使用Spring Cloud B ...

  3. Spring Cloud构建微服务架构(五)服务网关

    通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: alt 我们使用Spring Cloud Netflix中的Eureka实 ...

  4. 基于事件驱动架构构建微服务第1部分:应用程序特定的业务规则

    原文链接:https://logcorner.com/building-microservices-through-event-driven-architecture-part1-applicatio ...

  5. 使用Spring Boot和Kubernetes构建微服务架构

    "我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证. 在本教程 ...

  6. Spring Cloud构建微服务架构:服务容错保护(Hystrix依赖隔离)【Dalston版】

    前言 在上一篇<Spring Cloud构建微服务架构:服务容错保护(Hystrix服务降级)>中,我们已经体验了如何使用@HystrixCommand来为一个依赖资源定义服务降级逻辑.实 ...

  7. Spring Cloud构建微服务架构:服务容错保护(Hystrix断路器)【Dalston版】

    前言 在前两篇<Spring Cloud构建微服务架构:服务容错保护(Hystrix服务降级)>和<Spring Cloud构建微服务架构:服务容错保护(Hystrix依赖隔离)&g ...

  8. 基于Spring Cloud及K8S构建微服务应用

    摘要 广发证券蔡波斯先生通过三个大方向来为我们分享基于Spring Cloud及K8S构建微服务应用. 内容来源:2017年6月10日,广发证券蔡波斯在"Spring Cloud中国社区技术 ...

  9. 构建微服务(Building Microservices)-PDF 文档

    闲时翻译了几篇基于Spring Cloud.Netflix OSS 构建微服务的英文文章,为方便分享交流,整理为PDF文档. PDF 文档目录: 目录 一.微服务操作模型... 3 1.     前提 ...

  10. Spring构建微服务

    Spring构建微服务 此文为译文,原文地址 介绍 本文通过一个使用Spring.Spring Boot和Spring Cloud的小例子来说明如何构建微服务系统. 我们可以通过数个微服务组合成一个大 ...

最新文章

  1. pinctrl框架【转】
  2. ireport怎么套打_阴阳师:当前版本道馆突破怎么打?九套阵容让你成为道馆小能手...
  3. Dubbo 3.0 预览版解读,6到飞起~
  4. sharepoint2010网站根据权限隐藏ribbon
  5. Python中曲率与弯曲的转换_1000R曲率更具沉浸感!三星T55曲面显示器评测
  6. 蓝桥杯第六届国赛JAVA真题----切开字符串
  7. 英语中正式和休闲的打招呼_6
  8. 为什么无法建立过程性能模型?
  9. opencv成员函数data,step,at的使用
  10. 最难游戏2计算机5关,最囧游戏2第5关通关攻略
  11. Java学习博客第一篇
  12. 微信小程序实例教程(一)
  13. 50年的追踪拍摄:社会阶级能被逾越吗?
  14. 26个字母大写及小写分别对应的ASCII码值
  15. 基于Hi3516DV300的嵌入式入门演练(下)
  16. 短信的独特优势以及如何选择国际短信平台?
  17. svn入门----如何使用svn
  18. linux 文件打包下载到 Windows
  19. Excel COUNT COUNTA区别
  20. Hang Detect 问题

热门文章

  1. 《计算机操作系统》课程学习(1)——第1章 操作系统引论
  2. shell三剑客-awk总结-基础
  3. 字典树,01字典树,可持续化01字典树(总结+例题)
  4. vue可以编辑发布多长的视频_vue可以拍多长的视频_vue怎么注册
  5. android生命周期测试
  6. Dialog常用对话框
  7. Google创始人布林将飞往太空
  8. 贝叶斯优化python包_《用贝叶斯优化进行超参数调优》
  9. 2017计算机考试程序改错步骤,计算机二级C++练习题:程序改错题
  10. SpringMVC源码分析系列