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/
發佈了1 篇原創文章 · 獲贊 5 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章