選擇Jersey+jetty原因
之前做的項目大部分是PC的項目,沒有做的完全的前后端分離,后端使用的是MCV框架 像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請求
- linux測試請求吞吐量
linux啟動方式
[root@dev51 jersey]# ls
jerseyDemo.jar
[root@dev51 jersey]# java -jar jerseyDemo.jar
2015-11-30 09:24:50.748:INFO:oejs.Server:jetty-8.y.z-SNAPSHOT
2015-11-30 09:24:50.871:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:82
start...in 82
- 響應時間
- 總結
通過jersey+jetty 的hello項目測試我們可以發現以下幾個優點
- 項目更輕量
- 項目代碼更簡潔
- 項目部署更方便
- 項目並發量更大