自己构建微服务(springmvc+内嵌jetty+maven 环境配置)

pom.xml文件

 

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

mymvc

springmvc.jetty

war

0.0.1-SNAPSHOT

springmvc.jetty Maven Webapp

http://maven.apache.org

UTF-8

1.1

4.0.4.RELEASE

9.2.15.v20160210

 

junit

junit

3.8.1

test

 

org.eclipse.jetty

jetty-jsp

${jetty.version}

provided

org.eclipse.jetty

jetty-webapp

${jetty.version}

 

org.springframework

spring-webmvc

${spring.version}

 

org.springframework

spring-jdbc

${spring.version}

 

org.springframework

spring-context

${spring.version}

 

org.springframework

spring-aop

${spring.version}

 

org.springframework

spring-core

${spring.version}

 

org.springframework

spring-test

${spring.version}

 

springmvc.jetty

org.apache.maven.plugins

maven-shade-plugin

2.4.3

package

shade

false

implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

com.test.jettyemb.Lauhcher

*:*

META-INF/*.SF

META-INF/*.DSA

META-INF/*.RSA

 

启动main类

 

package com.test.jettyemb;

 

import org.eclipse.jetty.server.Connector;

import org.eclipse.jetty.server.Server;

import org.eclipse.jetty.server.ServerConnector;

import org.eclipse.jetty.webapp.WebAppContext;

 

public class Launcher {

 

public static final int PORT = 8080;

public static final String CONTEXT = "/";

 

private static final String DEFAULT_WEBAPP_PATH = "src/main/webapp";

 

/**

 * 创建用于开发运行调试的Jetty Server, 以src/main/webapp为Web应用目录.

 */

public static Server createServerInSource(int port, String contextPath) {

Server server = new Server();

// 设置在JVM退出时关闭Jetty的钩子。

server.setStopAtShutdown(true);

 

// 这是http的连接器

ServerConnector connector = new ServerConnector(server);

connector.setPort(port);

// 解决Windows下重复启动Jetty居然不报告端口冲突的问题.

connector.setReuseAddress(false);

server.setConnectors(new Connector[] { connector });

 

WebAppContext webContext = new WebAppContext(DEFAULT_WEBAPP_PATH, contextPath);

// webContext.setContextPath("/");

webContext.setDescriptor("src/main/webapp/WEB-INF/web.xml");

// 设置webapp的位置

webContext.setResourceBase(DEFAULT_WEBAPP_PATH);

webContext.setClassLoader(Thread.currentThread().getContextClassLoader());

server.setHandler(webContext);

return server;

}

 

/**

 * 启动jetty服务

 * 

 * @param port

 * @param context

 */

public void startJetty(int port, String context) {

final Server server = Launcher.createServerInSource(PORT, CONTEXT);

try {

server.stop();

server.start();

server.join();

} catch (Exception e) {

e.printStackTrace();

System.exit(-1);

}

}

 

public static void main(String[] args) {

new Launcher().startJetty(8080, "");

}

}

 

测试

cmd到根目录

打包 mvn package

运行 java -jar name-version.jar

 

 

 

 

参考网址

http://www.geedoo.info/springmvc-jetty-embedded-detailed-use.html

 

另外其他一个提供了一个springmvc+jetty的例子再github上

http://www.cnblogs.com/yjmyzz/p/jetty-embed-demo.html

https://github.com/yjmyzz/jetty-embed-demo

 

 上述例子出现打包目录结构释放加载问题,后面自己修改jetty-embed-demo后可以成功运行

附上例子下载

 

 

你可能感兴趣的:(技术点滴)