Dubbo开发指南

前置描述

  1. 文档中所说的项目目录结构仅针对自启动的服务提供者及消费者

  2. 服务提供者及消费者都需要添加dubbo相关jar依赖

  3. 自启动的项目必须添加插件依赖

  4. 详细资料请移步Dubbo官网Github

项目结构

  1. 约定优于配置
  2. 非默认约定及更详细资料请移步Dubbo官网Github
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
xxx-project
src
main
assembly
conf ## assmbly打包资源目录
dubbo.properties ## 约定默认读取的配置文件,配置文件不在该路径用-Ddubbo.properties.file或-Ddubbo.properties指定
log4j.xml## log4j配置文件
assembly.xml## asembly打包定义
java
resources
META-INF
spring
xxx.xml## 约定读取该路径下所有xml文件,不在该路径用-Ddubbo.spring.config指定
xxx-provider.xml## 暴露服务定义文件。非必须由上面的xxx.xml引用,也可以直接和xxx.xml一个目录下无需引入
yyy-consumer.xml## 依赖服务定义文件。同上
pom.xml

assembly.xml文件

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
<assembly>
<id>assembly</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/dubbo/META-INF/assembly/bin</directory>
<!-- 将dubbo的脚本打包到bin目录 -->
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/assembly/conf</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>

配置文件示例

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
##====================dubbo配置====================##
dubbo.registry.address=zookeeper://192.168.0.1:2181?backup=192.168.0.2:2181,192.168.0.3:2181
## 启用监控
dubbo.monitor.protocol=registry
# 暴露服务协议类型
dubbo.protocol.name=dubbo
# 序列化缺省hessian2,支持fst、kryo、java、json、fastjson、compactedjava、nativejava
dubbo.protocol.serialization=kryo
#服务提供者使用 netty4
dubbo.provider.transporter=netty4
## 服务消费者使用 netty4
#dubbo.reference.client=netty4
# 日志适配器
dubbo.container=log4j,spring
dubbo.shutdown.hook=true

## 具体服务配置
dubbo.application.owner=
# 服务名称
dubbo.application.name=xxx-provider
# 服务版本
xxx.service.version=
# 服务端口
dubbo.protocol.port=20881
# 使用文件缓存注册中心地址列表及服务提供者列表,应用重启时将基于此文件恢复,注意:两个注册中心不能使用同一文件存储
dubbo.registry.file=/tmp/xxx-provider-registry.cache
dubbo.service.loadbalance=roundrobin
dubbo.log4j.file=logs/xxx-provider.log
dubbo.log4j.level=INFO

引入依赖

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
<dependencies>
<!-- dubbo -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.7</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<!-- 当前release版本为2.5.3,私服上2.5.3-fixed是基于2.5.4-SNAPSHOT源码编译,修复一些2.5.3的bug -->
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<!-- 如果使用netty,下面的netty依赖保留其中一个即可 -->
<!-- 排除dubbo中netty 3.2.5.Final依赖 -->
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
<!-- 排除dubbo中netty 3.9.6.Final依赖 -->
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--使用netty4引入如下依赖 -->
<!--
<dependency>
<groupId>net.dubboclub</groupId>
<artifactId>netty4</artifactId>
<version>0.0.6</version>
</dependency>
-->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
<!-- dubbo end -->

<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- log start -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j_version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j_version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j_version}</version>
</dependency>
<!-- log end -->
</dependencies>

增加插件

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
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<outputDirectory>${project.build.directory}/dubbo</outputDirectory>
<includes>META-INF/assembly/**</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

服务提供者

项目结构

至少包含两个模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
xxx-project
xxx-api## 单纯的接口、bean及常量定义
src
main
java
com
yyy
xxx
service
XxxService.java
model
pom.xml
xxx-server## 逻辑实现模块
src
main
java
com
yyy
xxx
service
XxxServiceImpl.java
pom.xml

服务接口

1
2
3
4
package com.yyy.xxx.service;
public interface XxxService {
String sayHello(String name);
}

服务实现

1
2
3
4
5
6
7
8
9
package om.yyy.xxx.service;
import org.springframework.stereotype.Service;

@Service("xxxService")
public class XxxServiceImpl implements XxxService {
public String sayHello(String name){
return "Hello " + name;
}
}

暴露服务

1
2
<!-- version非必须 -->
<dubbo:service interface="com.yyy.xxx.service.XxxService" ref="xxxService" version="${xxx.service.version}"/>

启动服务

1
sh bin/start.sh

服务消费者

项目结构

无特别要求

引入依赖

1
2
3
4
5
<dependency>
<groupId>com.yyy</groupId>
<artifactId>xxx-api</artifactId>
<version>0.0.1</version>
</dependency>

引入依赖服务

1
2
<!-- version非必须 -->
<dubbo:reference id="xxxService" interface="com.yyy.xxx.service.XxxService" version="${xxx.service.version}"/>

调用依赖服务

1
2
3
4
5
6
7
8
9
10
11
12
13
// 仅仅是个调用示例
@Service("xxxAction")
public class XxxAction {
@Resource
private XxxService xxxService;

@PostConstruct
public void start(){
for (int i=0;i<1000;i++){
System.out.println(xxxService.sayHello(Integer.toString(i)));
}
}
}
-------------本文结束感谢您的阅读-------------