docker1.10.3-jetty8-jersey1.x 構建微服務
阿新 • • 發佈:2019-01-31
本專案是將restful專案打包成可執行的war包,在docker中執行
環境介紹:
docker 1.10.3
jetty 8
jersey 1.19
關鍵配置:
1、pom.xml配置
2、啟動類
3、web.xml配置
4、配置檔案conf.properties
5、Dockerfile配置
6、jenkins配置,請參考我的其他文章: jenkins構建Docker 映象(基於Jenkins的Docker映象及Jenkins外掛):http://blog.csdn.net/gsying1474/article/details/51126522
<build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId |
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; } }
|
<?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>
|
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