您的位置:首页 > 运维架构 > 网站架构

REST微服务架构之DropWizard

2015-07-20 10:17 1096 查看
 DropWizard是由Yammer开发团队贡献的一个后台服务开发框架,其集成了Java生态系统中各个问题域中最优秀的组件,帮助开发者快速的打造一个Rest风格的后台服务。 
    
对开发者来说,使用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依赖

Gradle代码  


apply plugin: 'idea'  

apply plugin: 'maven'  

apply plugin: 'java'  

  

repositories {  

    mavenCentral()  

}  

  

dependencies {  

    compile(  

            'com.yammer.dropwizard:dropwizard-core:0.6.1'  

    )  

}  

然后,定义Configuration 

Java代码  


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,大大简化了代码开发) 

Java代码  


@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: 

Java代码  


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服务让人放心很多: 

Java代码  


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主类中: 

Java代码  


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代码  


java -jar <jar包> server <config_file>  

注意:1、在打包的时候一定要把依赖库也打进去 
          2、配置文件的名字一定要和Service类中设置的一样 

最后,前面只是关于DropWizard的最基本的应用,DropWizard开发团队还为开发者考虑了很多贴心的功能,比方说,和Hibernate,Liquidbase的集成等等。更多更详细的信息,请移步:http://dropwizard.codahale.com/manual/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息