docker1.10.3-jetty8-jersey1.x 构建微服务
docker 微服务 MicroService    2017-07-19 21:30:08    813   
lightingfire   docker 微服务 MicroService
本项目是将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



 

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


Pre: build 并deploy saiku-query0.1到nexus

Next: build 并deploy saiku-query0.4到nexus


Table of content