對開發者來說,使用DropWizard有如下好處:
1、和Maven集成良好,也就是說和Gradle集成也很良好;
2、開發迅速,部署簡單;
3、代碼結構好,可讀性高;
4、自動爲服務提供OM框架;
5、讓開發者自然的把一個應用拆分爲一個個的小服務
DropWizard結構的Web服務組成
1、Configuration:用於設置該服務的配置,比方說在服務開放在哪個端口,數據庫配置是怎樣的等等。
2、Service:該服務的主入口,定義該服務使用哪個配置文件,開放哪些Resource,該服務需要哪些HealthCheck等等。
3、Resource:定義一個資源,包括如何獲取該資源,對該資源做Get/Post/Delete/Query時,對應的各種業務邏輯。
4、Representation:定義了一個服務返回值對象,當服務返回該對象時,會自動的把該對象按屬性值生成一個Json格式的字符串返回給服務調用者。
5、HealthCheck:在DropWizard爲每個服務提供的OM框架中用到,通過它可以隨時檢測當前服務是否可用。
DropWizard之Hello World
怎樣開發一個DropWizard的Web服務呢?首先,在你的項目中引入DropWizard依賴
- apply plugin: 'idea'
- apply plugin: 'maven'
- apply plugin: 'java'
- repositories {
- mavenCentral()
- }
- dependencies {
- compile(
- 'com.yammer.dropwizard:dropwizard-core:0.6.1'
- )
- }
然後,定義Configuration
- public class HelloWorldConfiguration extends Configuration {
- @NotEmpty //參數檢查
- @JsonProperty //自動映射配置文件
- private String template;
- @NotEmpty
- @JsonProperty
- private String defaultName;
- public String getTemplate() {
- return template;
- }
- public String getDefaultName() {
- return defaultName;
- }
- }
再接着,定義服務想要開放的Resource,(DropWizard中大量使用了Annotation,大大簡化了代碼開發)
- @Path("/helloWorld")
- @Produces(MediaType.APPLICATION_JSON)
- public class HelloWorldResource {
- private final String template;
- private final String defaultName;
- private final AtomicLong counter;
- public HelloWorldResource(String template, String defaultName) {
- this.template = template;
- this.defaultName = defaultName;
- this.counter = new AtomicLong();
- }
- @GET
- @Timed
- public SayingRepresentation sayHello(@QueryParam("name")Optional<String> name){
- return new SayingRepresentation(counter.incrementAndGet(),String.format(template,name.or(defaultName)));
- }
- }
然後,定義該服務返回值的Representation:
- public class SayingRepresentation {
- private long id;
- private String content;
- public SayingRepresentation(long id, String content) {
- this.id = id;
- this.content = content;
- }
- public long getId() {
- return id;
- }
- public String getContent() {
- return content;
- }
- }
然後,爲該服務定義一個HeatlthCheck,這個是可選的,但是,有HealthCheck的web服務讓人放心很多:
- public class TemplateHealthCheck extends HealthCheck {
- private final String template;
- protected TemplateHealthCheck(String template) {
- super("template");
- this.template = template;
- }
- @Override
- protected Result check() throws Exception {
- final String saying = String.format(template,"TEST");
- if(!saying.contains("TEST")){
- return Result.unhealthy("template doesn't include a name!");
- }
- return Result.healthy();
- }
- }
最後,把該服務涉及的配置,資源,HealthCheck統一整合到Service主類中:
- public class HelloWorldService extends Service<HelloWorldConfiguration> {
- //服務入口
- public static void main(String[] args) throws Exception {
- new HelloWorldService().run(args);
- }
- @Override
- public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
- //指定配置文件的名字
- bootstrap.setName("helloWorld");
- }
- @Override
- public void run(HelloWorldConfiguration helloWorldConfiguration, Environment environment) throws Exception {
- final String template = helloWorldConfiguration.getTemplate();
- final String defaultName = helloWorldConfiguration.getDefaultName();
- environment.addResource(new HelloWorldResource(template,defaultName));
- environment.addHealthCheck(new TemplateHealthCheck(template));
- }
- }
另外配置文件如下:
template: Hello, %s!
defaultName: Stranger
這就是一個完整的REST風格的Web服務代碼,另外,DropWizard的部署也非常簡單,只需要使用構建腳本把該服務打包,然後使用如下的命令即可運行服務:
- java -jar <jar包> server <config_file>
注意:1、在打包的時候一定要把依賴庫也打進去
2、配置文件的名字一定要和Service類中設置的一樣
最後,前面只是關於DropWizard的最基本的應用,DropWizard開發團隊還爲開發者考慮了很多貼心的功能,比方說,和Hibernate,Liquidbase的集成等等。更多更詳細的信息,請移步:http://dropwizard.codahale.com/manual/