peace唠叨

Dubbo的helloworld工程

本文简单介绍下Dubbo的helloworld工程,包括provider和Consumer的代码编写,以及怎样运行工程。

全部代码下载:Github链接:github链接,点击惊喜;写文章不易,欢迎大家采我的文章,以及给出有用的评论,当然大家也可以关注一下我的github;多谢;

1.Dubbo介绍:

DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案
详细介绍见:http://dubbo.io/User+Guide-zh.htm

2.搭建Helloworld工程:

2.1.搭建注册中心:

Dubbo目前支持4种注册中心,(multicast zookeeper redis simple),但是建议使用zookeeper
zookeeper 的安装见: ZooKeeper环境搭建

2.2.提供者的建立:provider

1.创建maven工程:
00
2.配置pom.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
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.wpeace</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>3.2.8.RELEASE</spring.version>
</properties>
<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>
</dependencies>
</project>

3.建立服务接口并实现

1
2
3
4
5
6
7
8
9
10
11
服务接口:
public interface HelloService {
public String getUserName(int id);
}
接口实现:
public class HelloServiceImpl implements HelloService{
public String getUserName(int id) {
System.out.println("id:"+id);
return "hello peace";
}
}

4.建立spring配置文件:在/src/main/resources建立application.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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 ">

<!-- 注册实际的bean -->
<bean id="helloService" class="cn.wpeace.service.impl.HelloServiceImpl" />
<!-- 提供方应用信息 -->
<dubbo:application name="provider" />
<!-- zookeeper注册中心地址 -->
<dubbo:registry address="zookeeper://192.168.1.127:2181" />
<!-- 该服务使用的端口 -->
<dubbo:protocol name="dubbo" port="29014" />
<!-- 声明需要注册的服务接口 -->
<dubbo:service interface="cn.wpeace.service.HelloService" ref="helloService" />
</beans>

5.建立启动主类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class ServerStart {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext(new String[]{"application.xml"});
context.start();
System.out.println("服务提供者注册成功");
try {
System.in.read();//阻塞程序,防止退出
} catch (Exception e) {
e.printStackTrace();
}

}
}

2.2.消费者的建立:consumer

1.创建maven工程:
00
2.配置pom.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
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.wpeace</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>3.2.8.RELEASE</spring.version>
</properties>
<dependencies>
<!-- 添加provider的jar包 -->
<dependency>
<groupId>cn.wpeace</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 添加dubbo依赖 -->
<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>
<!-- 添加zk客户端依赖 -->
<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>
</dependencies>
</project>

3.建立spring配置文件:在/src/main/resources建立application.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<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="consumer" />
<!-- zookeeper订阅中心地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.1.127:2181" />
<!--消费哪个服务接口 -->
<dubbo:reference id="helloService" interface="cn.wpeace.service.HelloService" />
</beans>

4.建立消费主类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class ConsumerStart {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"application.xml"});
context.start();
HelloService helloService = (HelloService)context.getBean("helloService");//从工厂bean中获得服务。
System.out.println(helloService.getUserName(22));//调用服务
try {
System.in.read();//阻塞程序,防止退出
} catch (Exception e) {
e.printStackTrace();
}
}
}

3.运行工程和测试

3.1启动步骤

1.启动注册中心zk
2.运行dubbo-admin(该步可选),步骤可参考:dubbo-admian的打包和平台搭建
3.启动provider
4.启动consumer

3.2测试结果:

provider:
02
consumer:
02
dubbo-admin:
04
05
05
05

本文来自伊豚(blog.wpeace.cn)

Peace wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!