DropWizard
http://www.dropwizard.io/0.9.2/docs/getting-started.html
Jetty for HTTP
Jersey for REST
官方说法 capable of doing 30,000-50,000 requests per second
定义依赖
<dependencies> <dependency> <groupId>io.dropwizard</groupId> <artifactId>dropwizard-core</artifactId> <version>${dropwizard.version}</version> </dependency> </dependencies>
实现
最核心的类Resource,定义路径和方法
import com.google.common.base.Optional; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; /** * Created by xuanzhui on 2016/3/21. */ @Path("/hello") @Produces(MediaType.APPLICATION_JSON) public class HelloWorldResource { @GET public String sayHello(@QueryParam("name") Optional<String> name) { return "Hello " + name.or("unknown"); } }
定义启动的类Application
import io.dropwizard.Application; import io.dropwizard.Configuration; import io.dropwizard.setup.Environment; /** * Created by xuanzhui on 2016/3/21. */ public class HelloWorldApplication extends Application<Configuration> { public static void main(String[] args) throws Exception { new HelloWorldApplication().run(new String[] { "server" }); } @Override public void run(Configuration configuration, Environment environment) throws Exception { environment.jersey().register(new HelloWorldResource()); } }
直接运行Application,即可通过 localhost:8080/hello 访问
打包
打包出一个jar,使其包含全部的依赖,最核心的是pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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>cn.xz</groupId> <artifactId>DropWizardSample</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <dropwizard.version>0.9.2</dropwizard.version> <jdk.version>1.8</jdk.version> </properties> <dependencies> <dependency> <groupId>io.dropwizard</groupId> <artifactId>dropwizard-core</artifactId> <version>${dropwizard.version}</version> </dependency> </dependencies> <build> <finalName>DropWizardSample</finalName> <plugins> <!-- Set a compiler level --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> <!-- Maven Shade Plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</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> <!-- Run shade goal on package phase --> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <!-- important for RS --> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> <!-- add Main-Class to manifest file --> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>cn.xz.HelloWorldApplication</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
通过maven package生成fat/uber jar
运行java -jar DropWizardSample.jar,即可通过 localhost:8080/hello 访问