所需环境
zookeeper 作为dubbo的注册中心,dubbo服务提供方和消费方都需要在 zookeeper 注册中心注册。
可参考:http://blog.csdn.net/jingyangv587/article/details/78901508
注意:启动后,请勿关闭!
开始搭建
1. 服务提供方和消费方都需要的包(这里我新建的 maven 工程为 pom 工程,将共同的项目依赖写到 pom.xml 中)
总的项目结构为
pom.xml 文件内容为
4.0.0
com.test
dubbo-demo
0.0.1-SNAPSHOT
pom
0.3.0 2.5.3 2.8.4 4.3.6.RELEASE 1.7 UTF-8
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- spring 相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
</dependencies>
demo-api
dubbo-consumer
dubbo-provider
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
- 在 demo-api 中定义服务接口(注意服务提供方和消费方都需要依赖这个项目)
package com.test;
public interface DemoService{
String sayHello(String name);
}
1
2
3
4
5
6
- 服务提供方实现
项目结构
实现接口
package com.test;
import org.springframework.stereotype.Service;
import com.test.DemoService;
@Service(“demoService”)
public class DemoServiceImpl implements DemoService{
@Override
public String sayHello(String name) {
// TODO Auto-generated method stub
return name;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
声明暴露服务:
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo_provider" />
<!-- 使用 zookeeper 注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用 dubbo 协议在 20880 端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.test.DemoService" ref="demoService" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
在 springmvc.xml 中扫描 service 注解并将dubbo-provider.xml 中的相关的 dubbo 配置引入进来
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.test" />
<import resource="classpath:dubbo-provider.xml" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
加载 Spring 配置,启动服务:
package com.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“classpath:springmvc.xml”);
context.start();
System.out.println("Dubbo provider start...");
try {
System.in.read(); // 按任意键退出
} catch (IOException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
4. 服务消费者实现
项目结构
在 dubbo-consumer.xml 中声明所所需要消费的服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
在 springmvc.xml 中扫描 service 注解并将 dubbo-consumer.xml 中的相关的 dubbo 配置引入进来
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.test" />
<import resource="classpath:/dubbo-consumer.xml" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
加载 Spring 配置,调用服务:
package com.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { “classpath:springmvc.xml” });
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
System.out.println(demoService.sayHello("哈哈哈"));
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
如果出现以下结果则调用成功
通过 dubbo-admin 管理后台可以看到服务的提供方与消费方
具体搭建可参考博客:http://blog.csdn.net/jingyangv587/article/details/78904369
提供方:
消费者:
完整项目下载地址(含 admin 包):https://gitee.com/jingyang3877/all-examples/blob/master/jingyang3877-dubbo-demo-master.zip
参考博客:https://www.cnblogs.com/Javame/p/3632473.html
想进一步了解 dubbo 请移步我的另外的 dubob 相关的文章:https://blog.csdn.net/jingyangV587/article/details/84983770
作者:敬仰的博客
来源:CSDN
原文:https://blog.csdn.net/jingyangV587/article/details/78901937
版权声明:本文为博主原创文章,转载请附上博文链接!