基於springmvc+spring-data-jpa+dubbo開發web應用

最近有項目客戶要求使用dubbo進行服務關聯和分佈式部署,能基於dubbo發佈分佈式的rest服務和基於wsdl的webservice服務。看了下dubbo這個項目,其開發基本處於停滯狀態,比較活躍的項目只有噹噹網維護的dubbox項目。本項目就是基於dubbox進行的。前端

項目模塊以下圖所示
imgjava

用pom來組織Jar包依賴,dubbox我已經打包成Jar。能夠看到項目分爲四個模塊。api提供rest和webservice的接口暴露,common是一些實體和工具類,server是數據庫操做的模塊,這裏咱們使用spring-data-jpa進行操做。web是前端應用,它經過api模塊暴露的接口來調用遠程dubbo服務,包括rest和webservice的。git

api部分的實現

一、暴露rest接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
複製代碼
package cn.com.egova.easyshare.api.human;  
  
import cn.com.egova.easyshare.common.dto.ResultDto;  
import cn.com.egova.easyshare.common.entity.Human;  
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;  
  
import javax.ws.rs.Consumes;  
import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.Produces;  
import javax.ws.rs.core.MediaType;  
  
/** 
 * 人員相關接口 
 * Created by gongxufan on 2016/3/8.
 */  
@Path("humans")  
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})  
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})  
public interface HumanActionApi<T> {  
  
    /** 
     * 獲取人員信息 
     * 
     * @param id 
     * @return 
     */  
    @GET  
    @Path("view/{id : \\d+}")  
    ResultDto<Human> getHuman(@PathParam("id") Integer id);  
}
複製代碼

這裏使用rest的註解暴露接口github

二、暴露webservice

1
2
3
複製代碼
public interface WSDLServiceApi {  
    List<NewsDto> fetchNews(@XmlJavaTypeAdapter(StringObjectMapAdapter.class) Map params);  
}
複製代碼

webservice的寫法很簡單,只是須要注意要在接口返回的實體類上上加上xml的註解
web

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
複製代碼
package cn.com.egova.easyshare.common.dto;  
  
import javax.xml.bind.annotation.XmlAccessType;  
import javax.xml.bind.annotation.XmlAccessorType;  
import javax.xml.bind.annotation.XmlElement;  
import javax.xml.bind.annotation.XmlRootElement;  
import java.util.Date;  
  
/** 
 * Created by gongxufan on 2016/3/30.
 */  
@XmlRootElement  
@XmlAccessorType(XmlAccessType.FIELD)  
public class NewsDto {  
  @XmlElement  
  private Integer newsID;  
  @XmlElement  
  private String newsName;  
  @XmlElement  
  private String newsDescr;  
  @XmlElement  
  private String newsURL;  
  @XmlElement  
  private Integer newsTypeID;  
  @XmlElement  
  private Integer unitID;  
  @XmlElement  
  private Date createDate;  
  @XmlElement  
  private Date updateDate;  
  @XmlElement  
  private Integer displayOrder;  
  @XmlElement  
  private Integer openFlag = 0;  
  @XmlElement  
  private String op;  
  
  public Integer getNewsID() {  
    return newsID;  
  }  
  
  public void setNewsID(Integer newsID) {  
    this.newsID = newsID;  
  }  
  
  public String getNewsName() {  
    return newsName;  
  }  
  
  public void setNewsName(String newsName) {  
    this.newsName = newsName;  
  }  
  
  public String getNewsDescr() {  
    return newsDescr;  
  }  
  
  public void setNewsDescr(String newsDescr) {  
    this.newsDescr = newsDescr;  
  }  
  
  public String getNewsURL() {  
    return newsURL;  
  }  
  
  public void setNewsURL(String newsURL) {  
    this.newsURL = newsURL;  
  }  
  
  public Integer getNewsTypeID() {  
    return newsTypeID;  
  }  
  
  public void setNewsTypeID(Integer newsTypeID) {  
    this.newsTypeID = newsTypeID;  
  }  
  
  public Integer getUnitID() {  
    return unitID;  
  }  
  
  public void setUnitID(Integer unitID) {  
    this.unitID = unitID;  
  }  
  
  public Date getCreateDate() {  
    return createDate;  
  }  
  
  public void setCreateDate(Date createDate) {  
    this.createDate = createDate;  
  }  
  
  public Date getUpdateDate() {  
    return updateDate;  
  }  
  
  public void setUpdateDate(Date updateDate) {  
    this.updateDate = updateDate;  
  }  
  
  public Integer getDisplayOrder() {  
    return displayOrder;  
  }  
  
  public void setDisplayOrder(Integer displayOrder) {  
    this.displayOrder = displayOrder;  
  }  
  
  public Integer getOpenFlag() {  
    return openFlag;  
  }  
  
  public void setOpenFlag(Integer openFlag) {  
    this.openFlag = openFlag;  
  }  
  
  public String getOp() {  
    return op;  
  }  
  
  public void setOp(String op) {  
    this.op = op;  
  }  
}
複製代碼

api模塊單獨分出來能夠直接提供給其餘模塊進行調用spring

接口實現部分

使用dubbo服務

在app-config.xml中咱們須要定義dubbo服務相關的信息,具體網上不少資料能夠查看。
數據庫

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
複製代碼
<?xml version="1.0" encoding="UTF-8"?>  
<!--  
 - Copyright 1999-2011 Alibaba Group.  
 -    
 - Licensed under the Apache License, Version 2.0 (the "License");  
 - you may not use this file except in compliance with the License.  
 - You may obtain a copy of the License at  
 -    
 -      http://www.apache.org/licenses/LICENSE-2.0  
 -    
 - Unless required by applicable law or agreed to in writing, software  
 - distributed under the License is distributed on an "AS IS" BASIS,  
 - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 - See the License for the specific language governing permissions and  
 - limitations under the License.  
-->  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  
    <dubbo:application name="service-provider" owner="egova" organization="goutu"/>  
  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>  
  
    <!--使Dubbo在Spring容器初始化完後,再暴露服務-->  
    <dubbo:provider delay="-1"/>  
    <!--uncomment this if you want to test dubbo's monitor--> <!--<dubbo:monitor protocol="registry"/>--> <!-- use tomcat server --> <dubbo:protocol name="rest" port="8888" threads="500" contextpath="services" server="tomcat" accepts="1000" extension="cn.com.egova.easyshare.api.extension.CustomExceptionMapper"/> <dubbo:protocol name="webservice" port="8892" server="tomcat" contextpath="services"/> </beans> 複製代碼

這裏咱們定義了rest和webservice協議,rest協議在8888下而webservice在8892下訪問。須要注意的是,咱們把這兩個服務都跑在tomcat下。這樣咱們的接口實現部分能夠打成war包部署在多個tomcat下,從而實現了接口服務的分佈式部署。express

這裏只是做爲演示,因此app-db.xml中的數據源定義都已經註釋掉了。若是你想採用spring-data-jpa進行數據操做的話能夠把註釋去掉,而後去實現本身的Repository 。apache

web應用部分

這個就不用提了,我提供了一個測試頁面測試接口。須要注意的是,web調用接口的配置是在dubbo-consumer.xml中api

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
    <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方同樣 -->  
    <dubbo:application name="dubbo-client" owner="egova-client" organization="goutu-client" />  
    <!-- zookeeper註冊中心 -->  
    <dubbo:registry  protocol="zookeeper" address="127.0.0.1:2181" check="false"/>  
    <!-- 監控中心配置 -->  
    <!--<dubbo:monitor protocol="registry"/>-->  

    <!--關閉全部服務的啓動可用性檢查-->  
    <dubbo:consumer check="false" />  

     <!-- dubbo遠程服務-->  
    <dubbo:reference id="humanAction"   interface="cn.com.egova.easyshare.api.human.HumanActionApi"/>  
    <dubbo:reference id="wSDLService"   interface="cn.com.egova.easyshare.webservice.WSDLServiceApi" />  
</beans>
複製代碼

在這裏配置好zookeeper和api端的service,而後在contoller裏就能夠調用接口方法了。

end

代碼下載地址:csdn下載
github: github.com/gongxufan/d…

相關文章
相關標籤/搜索