Jersey+jetty 搭建高并RestFull 接口服务

来源:互联网 发布:淘宝微淘刷粉丝封号吗 编辑:程序博客网 时间:2024/06/03 18:34

原文链接 http://www.zhaochao.net/index.php/2015/12/07/5/

选择Jersey+jetty原因

之前做的项目大部分是PC的项目,没有做的完全的前后端分离,后端使用的是MVC框架 像SpringMVC、Sturts2。最近开发移动webapp项目,因为对SpringMVC框架比较熟悉所以就选择了用SpringMVC框架为前端提供接口,后来发现了一些新的RestFull框架如Jersey、RestEeasy等RestFull框架。对比后觉得使用SpringMVC框架为app提供接口有点“大材小用了”,主要原因:
1. webApp所要的数据主要有HTML、CSS、JS、图片、JSON数据,首先HTML直接放在nginx下面,因为nginx处理静态资源比较快。CSS/JS/图片 放入第三放的CDN云空间中,这样可以省去对图片等静态资源管理的工作,并且可以利用CDN加速。唯一需要后端提供的就是JSON数据,所以后端没必要像PC网站一样在项目中存放js/css/html等资料,只需要处理一个http请求,然后返回JSON数据就可以了;
2. SpringMVC 不仅可以处理http请求返回JSON数据,还可以返回视图如JSP/velocity/freemark等,所以使用SpringMVC 用来作接口显得有点“大材小用了”;
3. 既然只需要处理http返回JSON,项目可以不用打成war包,可以使用jetty这样嵌入式的serlvet容器,它比tomcat更轻量,部署更简单,所以准备使用jersey+jetty 代替SpringMVC+tomcat ,做RestFull接口。

项目实战

  • 建立Maven项目,配置Pom文件,配置如下所示:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.zhaochao</groupId>    <artifactId>Jersey_jetty</artifactId>    <version>0.0.1-SNAPSHOT</version>    <dependencies>        <!-- Jersey -->        <dependency>            <groupId>com.sun.jersey</groupId>            <artifactId>jersey-servlet</artifactId>            <version>1.19</version>        </dependency>        <!-- Jetty -->        <dependency>            <groupId>org.eclipse.jetty.aggregate</groupId>            <artifactId>jetty-all-server</artifactId>            <version>8.1.16.v20140903</version>        </dependency>    </dependencies>    <build>        <finalName>jerseyDemo</finalName>        <plugins>            <plugin>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.3</version>                <configuration>                    <source>1.8</source>                    <target>1.8</target>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-shade-plugin</artifactId>                <version>2.4.2</version>                <configuration>                    <createDependencyReducedPom>true</createDependencyReducedPom>                    <filters>                        <filter>                            <artifact>*:*</artifact>                            <excludes>                                <exclude>META-INF/*.SF</exclude>                                <exclude>META-INF/*.DSA</exclude>                                <exclude>META-INF/*.RSA</exclude>                            </excludes>                        </filter>                    </filters>                </configuration>                <executions>                    <execution>                        <phase>package</phase>                        <goals>                            <goal>shade</goal>                        </goals>                        <configuration>                            <transformers>                                <transformer                                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />                                <transformer                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                    <mainClass>com.zhaochao.bin.Main</mainClass>                                </transformer>                            </transformers>                        </configuration>                    </execution>                </executions>            </plugin>            <plugin>                <groupId>org.codehaus.mojo</groupId>                <artifactId>exec-maven-plugin</artifactId>                <version>1.2.1</version>                <executions>                    <execution>                        <goals>                            <goal>java</goal>                        </goals>                    </execution>                </executions>                <configuration>                    <mainClass>com.zhaochao.bin.Main</mainClass>                </configuration>            </plugin>        </plugins>    </build></project>

项目主要依赖jersey和jetty

  • 新建Jersey 处理RestFull接口类
package com.zhaochao.action;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;@Path("hello")public class HelloAction {    @GET    @Path("{name}")    @Produces(MediaType.TEXT_PLAIN)    //访问路径 /hello/zhaochao    public String hello(@PathParam("name") String name) throws Exception {        return "hello wolrd! "+name;    }}

这里是jersey实现的RestFull接口,通过简单的Path注解可以将简单的JAVA类映射成一个接口处理类,@GET表明这个接口只接受HTTP的GET请求,@Path(“{name}”)表明这个name是http路径中这变量,@Produces 表明返回类型,@PathParam(“name”) 会将/hello/name 中的name 注入到hello()参数中的name

  • 建立jetty服务并启动
package com.zhaochao.bin;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.servlet.ServletContextHandler;import org.eclipse.jetty.servlet.ServletHolder;import com.sun.jersey.spi.container.servlet.ServletContainer;public class Main {    public static void main(String[] args) throws Exception {        Server server=new Server(82);        ServletHolder servlet = new ServletHolder(ServletContainer.class);        servlet.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");        servlet.setInitParameter("com.sun.jersey.config.property.packages", "com.zhaochao.action");        ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);        handler.setContextPath("/");        handler.addServlet(servlet, "/*");        server.setHandler(handler);        server.start();        System.out.println("start...in 82");    }}

这段代码就是建立一个jetty服务,并指定要处理jersey资源的包名,然后启动

  • 测试hello请求

Hello请求结果

  • linux测试请求吞吐量

linux启动方式

[root@dev51 jersey]# lsjerseyDemo.jar[root@dev51 jersey]# java -jar jerseyDemo.jar 2015-11-30 09:24:50.748:INFO:oejs.Server:jetty-8.y.z-SNAPSHOT2015-11-30 09:24:50.871:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:82start...in 82

吞吐量

  • 响应时间

响应时间

  • 总结

通过jersey+jetty 的hello项目测试我们可以发现以下几个优点

  • 项目更轻量
  • 项目代码更简洁
  • 项目部署更方便
  • 项目并发量更大

与Mybatis Spring 整合项目

http://blog.csdn.net/whzhaochao/article/details/50152833

2 0
原创粉丝点击