REST微服务架构之DropWizard

 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代码   收藏代码
  1. apply plugin: 'idea'  
  2. apply plugin: 'maven'  
  3. apply plugin: 'java'  
  4.   
  5. repositories {  
  6.     mavenCentral()  
  7. }  
  8.   
  9. dependencies {  
  10.     compile(  
  11.             'com.yammer.dropwizard:dropwizard-core:0.6.1'  
  12.     )  
  13. }  


然后,定义Configuration 
Java代码   收藏代码
  1. public class HelloWorldConfiguration extends Configuration {  
  2.     @NotEmpty //参数检查  
  3.     @JsonProperty //自动映射配置文件  
  4.     private String template;  
  5.   
  6.     @NotEmpty  
  7.     @JsonProperty  
  8.     private String defaultName;  
  9.   
  10.     public String getTemplate() {  
  11.         return template;  
  12.     }  
  13.   
  14.     public String getDefaultName() {  
  15.         return defaultName;  
  16.     }  
  17. }  

再接着,定义服务想要开放的Resource,(DropWizard中大量使用了Annotation,大大简化了代码开发) 
Java代码   收藏代码
  1. @Path("/helloWorld")  
  2. @Produces(MediaType.APPLICATION_JSON)  
  3. public class HelloWorldResource {  
  4.     private final String template;  
  5.     private final String defaultName;  
  6.     private final AtomicLong counter;  
  7.   
  8.     public HelloWorldResource(String template, String defaultName) {  
  9.         this.template = template;  
  10.         this.defaultName = defaultName;  
  11.         this.counter = new AtomicLong();  
  12.     }  
  13.   
  14.     @GET  
  15.     @Timed  
  16.     public SayingRepresentation sayHello(@QueryParam("name")Optional<String> name){  
  17.         return new SayingRepresentation(counter.incrementAndGet(),String.format(template,name.or(defaultName)));  
  18.     }  
  19. }  

然后,定义该服务返回值的Representation: 
Java代码   收藏代码
  1. public class SayingRepresentation {  
  2.     private long id;  
  3.     private String content;  
  4.   
  5.     public SayingRepresentation(long id, String content) {  
  6.         this.id = id;  
  7.         this.content = content;  
  8.     }  
  9.   
  10.     public long getId() {  
  11.         return id;  
  12.     }  
  13.   
  14.     public String getContent() {  
  15.         return content;  
  16.     }  
  17. }  

然后,为该服务定义一个HeatlthCheck,这个是可选的,但是,有HealthCheck的web服务让人放心很多: 
Java代码   收藏代码
  1. public class TemplateHealthCheck extends HealthCheck {  
  2.     private final String template;  
  3.   
  4.     protected TemplateHealthCheck(String template) {  
  5.         super("template");  
  6.         this.template = template;  
  7.     }  
  8.   
  9.     @Override  
  10.     protected Result check() throws Exception {  
  11.         final String saying = String.format(template,"TEST");  
  12.         if(!saying.contains("TEST")){  
  13.             return Result.unhealthy("template doesn't include a name!");  
  14.         }  
  15.         return Result.healthy();  
  16.     }  
  17. }  

最后,把该服务涉及的配置,资源,HealthCheck统一整合到Service主类中: 
Java代码   收藏代码
  1. public class HelloWorldService extends Service<HelloWorldConfiguration> {  
  2.   
  3.     //服务入口  
  4.     public static void main(String[] args) throws Exception {  
  5.         new HelloWorldService().run(args);  
  6.     }  
  7.   
  8.     @Override  
  9.     public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {  
  10.         //指定配置文件的名字  
  11.         bootstrap.setName("helloWorld");  
  12.     }  
  13.   
  14.     @Override  
  15.     public void run(HelloWorldConfiguration helloWorldConfiguration, Environment environment) throws Exception {  
  16.         final String template = helloWorldConfiguration.getTemplate();  
  17.         final String defaultName = helloWorldConfiguration.getDefaultName();  
  18.         environment.addResource(new HelloWorldResource(template,defaultName));  
  19.         environment.addHealthCheck(new TemplateHealthCheck(template));  
  20.     }  
  21. }  


另外配置文件如下: 
template: Hello, %s! 
defaultName: Stranger 

这就是一个完整的REST风格的Web服务代码,另外,DropWizard的部署也非常简单,只需要使用构建脚本把该服务打包,然后使用如下的命令即可运行服务: 
Java代码   收藏代码
  1. java -jar <jar包> server <config_file>  

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

最后,前面只是关于DropWizard的最基本的应用,DropWizard开发团队还为开发者考虑了很多贴心的功能,比方说,和Hibernate,Liquidbase的集成等等。更多更详细的信息,请移步:http://dropwizard.codahale.com/manual/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值