最近有项目客户要求使用dubbo进行服务关联和分布式部署,能基于dubbo发布分布式的rest服务和基于wsdl的webservice服务。看了下dubbo这个项目,其开发基本处于停滞状态,比较活跃的项目只有当当网维护的dubbox项目。本项目就是基于dubbox进行的。前端
用pom来组织Jar包依赖,dubbox我已经打包成Jar。能够看到项目分为四个模块。api提供rest和webservice的接口暴露,common是一些实体和工具类,server是数据库操做的模块,这里咱们使用spring-data-jpa进行操做。web是前端应用,它经过api模块暴露的接口来调用远程dubbo服务,包括rest和webservice的。git
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
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
在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调用接口的配置是在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里就能够调用接口方法了。
代码下载地址:csdn下载
github: github.com/gongxufan/d…