docker1.10.3-jetty8-jersey1.x 构建微服务

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

环境介绍:

docker    1.10.3
jetty     8
jersey    1.19

项目代码:

https://git.coding.net/firewarm/docker-jetty-jersey1.x.git

关键配置:

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>
<echomessage="*** Moving launcher.class..." />
<movetodir="${project.build.directory}/${project.artifactId}/">
<filesetdir="${project.build.directory}/classes/">
<includename="Launcher.class" />
</fileset>
</move>
<echomessage="*** Moving launcher.class done." />
<echomessage="*** Removing *.SF, *.RSA in META-INF ..." />
<delete>
<fileset
dir="${project.build.directory}/${project.artifactId}/META-INF/"
includes="*.SF,*.RSA" />
</delete>
<echomessage="*** Removing *.SF, *.RSA in META-INF done." />
<echomessage="*** Copying logback-test.xml..." />
<copytodir="${project.build.directory}/${project.artifactId}/">
<filesetdir="${project.build.directory}/../src/main/resources/">
<includename="logback-test.xml" />
</fileset>
</copy>
<echomessage="*** Copying logback-test.xml done." />
<echomessage="*** Copying conf.properties ..." />
<copytodir="${project.build.directory}/${project.artifactId}/../">
<filesetdir="${project.build.directory}/../src/main/resources/">
<includename="conf.properties" />
</fileset>
</copy>
<echomessage="*** Copying conf.properties done." />
<echomessage="*** Copying run.sh ..." />
<copytodir="${project.build.directory}/${project.artifactId}/../">
<filesetdir="${project.build.directory}/../src/main/resources/">
<includename="run.sh" />
</fileset>
</copy>
<echomessage="*** 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;
publicfinalclassLauncher{
publicstaticvoidmain(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();
       }
privatestatic 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-appversion="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 -xchmod775 /target/*.sh
# Run the JAR
CMD java -DCONFIGPATH=./target/conf.properties -jar /target/docker-jetty-jersey1.x.war 

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

by 刘迎光@萤火虫工作室
OpenBI交流群:495266201
MicroService 微服务交流群:217722918
mail: liuyg#liuyingguang.cn
博主首页(==防止爬虫==):http://blog.liuyingguang.cn
OpenBI问答社区:http://www.openbi.tk

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容