1. 程式人生 > >docker1.10.3-jetty8-jersey1.x 構建微服務

docker1.10.3-jetty8-jersey1.x 構建微服務

本專案是將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=8080database.url=database.driver_class=database.user=database.password=hibernate.show_sql=false

5、Dockerfile配置
FROM isuper/java-oracle:jre_7MAINTAINER Liuyg <liuyg@liuyingguang.cn># Expose the API portEXPOSE 8080ADD target targetRUN set -x chmod 775 /target/*.sh# Run the JARCMD 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