OSGI环境搭建-使用JETTY-9.X创建WEB应用-Jetty性能调优

由于项目有组件式开发的需求,网上找了一阵,发现有OSGI和SpringDM都能支持,我选了equinox的实现(新版本注册Servlet已经抛弃HttpService

网上学习的过程中,发现资料极少,而且大部分发帖时间都在08、09年左右,找来找去只有那几个博客,最近一直在折腾,总算略懂一二。

环境配置:

我的开发工具:  eclipse 4.4.2 + JDK 1.8

1  保证OSGI环境能运行:

第一步:下载OSGI环境 http://download.eclipse.org/equinox/drops/R-Neon-201606061100/download.php?dropFile=equinox-SDK-Neon.zip

第二步:解压equinox-SDK-Neon.zip,/equinox-SDK-Neon/plugins下面,找到下面这7个jar(不是必须的,因为一共有200多个,后面很难找):

javax.servlet_3.1.0.v201410161800

org.eclipse.osgi_3.11.0.v20160603-1336

org.eclipse.osgi.services_3.5.100.v20160504-1419

org.apache.felix.gogo.command_0.10.0.v201209301215

org.apache.felix.gogo.runtime_0.10.0.v201209301036

org.apache.felix.gogo.shell_0.10.0.v201212101605

org.eclipse.equinox.console_1.1.200.v20150929-1405

复制到 my-osgi文件夹

第三步:打开eclipse > window > Preferrence > Plug-in-Development  > add (Nothing:xxxx) > next > add 选中之前的 my-osgi文件夹

第四步:打开eclipse > Run > Run Configurations > OSGi Framework > 右键new > 选中Bundles下的所有Target Platform(第三步的7个JAR):

点击run,如果控制台出现 osgi >  则说明OSGI环境没问题了(如果不行,请百度,这个不是本文重点)。

2  配置Jetty:

第一步:下载Jetty Bundles(之前我是下的9.4,运行的时候看见控制台打印不是稳定的版本,所以找的9.3)

地址:http://mirrors.neusoft.edu.cn/eclipse/jetty/updates/jetty-bundles-9.x/9.3.10.v20160621/Jetty-bundles-repository-9.3.10.v20160621.zip

步:解压,\target\site\plugins下面找到如下10个JAR(同上,不是必须的):

org.eclipse.jetty.deploy_9.3.10.v20160621

org.eclipse.jetty.http_9.3.10.v20160621

org.eclipse.jetty.io_9.3.10.v20160621

org.eclipse.jetty.osgi.boot_9.3.10.v20160621

org.eclipse.jetty.security_9.3.10.v20160621

org.eclipse.jetty.server_9.3.10.v20160621

org.eclipse.jetty.servlet_9.3.10.v20160621

org.eclipse.jetty.util_9.3.10.v20160621

org.eclipse.jetty.webapp_9.3.10.v20160621

org.eclipse.jetty.xml_9.3.10.v20160621

复制到 my-osgi文件夹,刷新Target Platform: 打开eclipse > window > Preferrence > Plug-in-Development  > 选中 reload

第三步:设置启动参数,启用Jetty-boot,开启WEB端口:

打开eclipse > Run > Run Configurations > Arguments选项卡 > VM arguments 增加参数:

-Djetty.home=D:your-path\jetty-home -Djetty.http.port=8080

注意 -Djetty.home 是Jetty 配置文件的路径,jetty-home文件夹下必须有个etc目录,etc下是jetty的配置文件(jetty.xml, jetty-deployer.xml, jetty-http.xml等)

第四步:打开eclipse > Run > Run Configurations > OSGi Framework > 全选  > run 测试能否正常运行。


创建工程,测试Servlet:

1 创建plug-in project,编辑MANIFEST.MF, Imported Package > add > 

  javax.servlet;version="3.1.0",
  javax.servlet.http;version="3.1.0",
  org.eclipse.jetty.http;version="9.3.10",
  org.eclipse.jetty.server;version="9.3.10",
  org.eclipse.jetty.server.handler;version="9.3.10",
  org.eclipse.jetty.servlet;version="9.3.10",
  org.osgi.framework;version="1.3.0"

2 创建Servlet

3 注册Servlet 

public void start(BundleContext context) throws Exception {

        //1. We create a Servlet Handler
        ServletHandler handler = new ServletHandler();
//2. We register our Servlet and its URL mapping
        handler.addServletWithMapping(MyServlet.class, "/test");
        //3. We are creating a Servlet Context handler
        ServletContextHandler ch = new ServletContextHandler();
        //4. We are defining the context path
        ch.setContextPath("/web");
        //5. We are attaching our servlet handler
        ch.setServletHandler(handler);
        //6. We are creating an empty Hashtable as the properties
        Hashtable props = new Hashtable();
        // 7. Here we register the ServletContextHandler as the OSGi service
        context.registerService(ContextHandler.class.getName(), ch, props);
        // now you can test the URL: http://ip:port/web/test
}


4 启动,测试 http://ip:port/web/test


5 注册多个servlet, 关键在这句: handler.addServletWithMapping(MyServlet.class, "/test"); 

其他bundle里面通过bundleContext.getServiceReference(ContextHandler.class.getName()) 拿到ServletContextHandler 然后拿到ServletHandler  

最后handler.addServletWithMapping(MyServlet.class, "/test2...3...4...5..."); 

   ServiceReference ref = bundleContext.getServiceReference(ContextHandler.class.getName()) ;

ServletContextHandler sch = (ServletContextHandler) context.getService(ref);

sch.getservletHandler().addServletWithMapping


我的源码下载

http://download.csdn.net/detail/chen42955/9597079

参考文章:

www.eclipse.org/jetty/documentation/9.4.x/framework-jetty-osgi.html

https://examples.javacodegeeks.com/enterprise-java/jetty/jetty-osgi-example/


Jetty 欢迎访问Jetty文档 Wiki. Jetty是一个开源项目,提供了http服务器、http客户端和java servlet容器。 这个wiki提供jetty的入门教程、基础配置、功能特性、优化、安全、JavaEE、监控、常见问题、故障排除帮助等等。它包含教程、使用手册、视频、特征描述、参考资料以及常见问题。 Jetty文档 ---------------- 入门: 下载Download, 安装, 配置, 运行 Jetty入门(视频) 下载和安装Jetty 如何安装一个Jetty包 如何配置Jetty – 主要文档 如何运行Jetty 用JConsole监控Jetty 如何使用Jetty开发 Jetty HelloWorld教程 Jetty和Maven HelloWorld教程 Jetty(6)入门 (www.itjungle.com) Jetty Start.jar 配置Jetty 如何设置上下文(Context Path) 如何知道使用了那些jar包 如何配置SSL 如何使用非root用户监听80端口 如何配置连接器(Connectors) 如何配置虚拟主机(Virtual Hosts) 如何配置会话ID(Session IDs) 如何序列化会话(Session) 如何重定向或移动应用(Context) 如何让一个应用响应一个特定端口 使用JNDI 使用JNDI 在JNDI中配置数据源(DataSource) 内嵌Jetty服务器 内嵌Jetty教程 内嵌Jetty的HelloWorld教程 内嵌Jetty视频 优化Jetty 如何配置垃圾收集 如何配置以支持高负载 在Jetty中部署应用 部署管理器 部署绑定 热部署 Context提供者 如何部署web应用 webApp提供者 如何部署第三方产品 部署展开形式的web应用 使用Jetty进行开发 如何使用Jetty进行开发 如何编写Jetty中的Handlers 使用构建工具 如何在Maven中使用Jetty 如何在Ant中使用Jetty Maven和Ant的更多支持 Jetty Maven插件(Plugin) Jetty Jspc Maven插件(Plugin) Maven web应用工程原型 Ant Jetty插件(Plugin) 使用集成开发环境(IDEs) 在Eclipse中使用Jetty 在IntelliJ中使用Jetty 在Eclipse中工作 在Eclipse中开发Jetty Jetty WTP插件(Plugin) JettyOSGi SDK for Eclipse-PDE EclipseRT Jetty StarterKit SDK OSGi Jetty on OSGi, RFC66 基于Jetty OSGi的产品 OSGi贴士 Equinox中使用Jetty实现HTTP Service Felix中使用Jetty实现HTTP Service PAX中使用Jetty实现HTTP Srevice ProSyst mBedded Server Equinox Edition Spring Dynamic Modules里的Jetty JOnAS5里的Jetty 配置Ajax、Comet和异步Servlets 持续和异步Servlets 100 Continue和102 Processing WebSocket Servlet 异步的REST Stress Testing CometD 使用Servlets和Filters Jetty中绑定的Servlets Quality of Service Filter Cross Origin Filter 配置安全策略(Security Policies) 安全领域(Security Realms) 安全域配置教程 Java Authentication and Authorization Service (JAAS) JAAS配置教程 JASPI 安全模型(Secure Mode) 存储在文件中的安全密码以及编程教程 如何开启或禁止Jetty中的SSL功能 如何在Jetty中安全存储密码 如何安全终止Jetty 如何配置Spnego Application Server Integrations(集成) Apache Geronimo JEE 配置Apache httpd和Jetty教程 配置Apache mod_proxy和Jetty 配置Jetty中的AJP13 在JBoss中配置Jetty Remote Glassfish EJBs from Jetty Jetty and Spring EJB3 (Pitchfork) JBoss EJB3 ObjectWeb EasyBeans
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值