Dubbo与Zookeeper、SpringMVC整合和使用(我的入门demo)
|
开发工具 |
MyEclipse 10.7 |
|
JDK |
1.7 |
|
容器 |
Tomcat 8(运行dubbo) |
|
zookeeper版本 |
zookeeper-3.4.6 |
|
dubbo |
dubbo-admin-2.5.3 |
dubbo-admin-2.5.3下载地址:http://pan.baidu.com/s/1bozCMzP
zookeeper下载地址:http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.6/
Dubbo+Zookeeper 的下载安装配置启动
1.dubbo-admin-2.5.3下载完成后。放在webapps文件夹下面。先把默认的tomcat的ROOT项目备份后移除。将dubbo-admin-2.5.3.war改成ROOT.war 备用
2.zookeeper下载后解压安装即可。windows安装完成后如下图
进入到conf里面,会看到zoo_sample.cfg文件。复制一个修改为zoo.cfg,修改相关配置内容
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
#这块是设置zookeeper的端口。默认2181
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
到bin文件夹下面。启动zookeeper,windows 点击zkServer.cmd即可
3.可以启动tomcat了。这样直接访问 就能看到dubbo的页面。要不然dubbo启动会报错。第一次入门的话建议就用默认的端口配置即可。默认用户 root 密码 root
登录成功以后看到如下页面
以上就是dubbo+zookeeper的配置和启动。下面开始Java代码的编写
- 整合Dubbo+Zookeeper+SpringMVC
1.创建myDubbo项目为主项目,结构图如下 myC P S后续才创建,刚开始是没有的。
POM.XML配置如下
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>mydubbo</artifactId>
注意这里。打包为POM,分布式系统的第一步
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mydubbo</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.2.4.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>
<!--dubbo注册中心-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!--zookeeper客户端-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>mydubbo</finalName>
</build>
创建子项目后自动会加载。刚开始是没有的
<modules>
<module>myService</module>
<module>myProvider</module>
<module>myConsumer</module>
</modules>
</project>
2.创建服务提供商子项目,MyEclipse操作如下图示意 对mydubbo项目鼠标右键new或者maven插件直接创建maven module项目,填写子项目名称。有的如果不选种Create a simple project 自定义创建会报错。选择则不会。根据实际情况来确定是否选择
3.创建3个子项目分别为 myService(服务商(接口),模块提供方) myProvider(供应者,给dubbo zookeeper注册暴露接口) myConsumer
- myService 项目相关配置
- pom配置
- <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>
- <parent>
- <artifactId>mydubbo</artifactId>
- <groupId>com</groupId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <name>myService</name>
- 10. <artifactId>myService</artifactId>
- 11. <packaging>jar</packaging>
</project>
- 新建一个接口类
13. package com.xiaoshuai;
- 14.
15. public interface HelloService {
- 16. public String speakHello(String name);
}
- 以上工作完成后,进行maven install
- myProvider项目相关配置
- pom配置
- <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>
- <parent>
- <artifactId>mydubbo</artifactId>
- <groupId>com</groupId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <artifactId>myProvider</artifactId>
10. 引用接口服务项目 子项目maven install 为jar
- 11. <dependencies>
- 12. <dependency>
- 13. <groupId> myService </groupId>
- 14. <artifactId>myService</artifactId>
- 15. <version>0.0.1-SNAPSHOT</version>
- 16. </dependency>
- 17. </dependencies>
</project>
18. 实现接口服务者
19. package com.xiaoshuai.impl;
- 20.
- 21.
22. import com.xiaoshuai.HelloService;
23. public class HelloServiceImpl implements HelloService{
- 24.
- 25. public String speakHello(String name) {
- 26. return "你好:"+name+"欢迎查阅小帅丶博客";
- 27. }
- 28.
29. }
- 将实现类接口类暴露给dubbo+zookeeper 在myProvider创建provider.xml 内容如下
31. <?xml version="1.0" encoding="UTF-8"?>
32. <beans xmlns="http://www.springframework.org/schema/beans"
- 33. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 34. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- 35. xsi:schemaLocation="http://www.springframework.org/schema/beans
- 36. http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- 37.
- 38. <!-- 提供方应用信息,用于计算依赖关系 -->
- 39. <dubbo:application name="hello-provider" />
- 40.
- 41. <!-- 使用multicast广播注册中心暴露服务地址 -->
- 42. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
- 43.
- 44. <!-- 用dubbo协议在20880端口暴露服务 -->
- 45. <dubbo:protocol name="dubbo" port="20880" />
- 46.
- 47. <!-- 声明需要暴露的服务接口 -->
- 48. <dubbo:service interface="com.xiaoshuai.HelloService" ref="helloService" />
- 49.
- 50. <!-- 和本地bean一样实现服务 -->
- 51. <bean id="helloService" class="com.xiaoshuai.impl.HelloServiceImpl" />
- 52.
53. </beans>
- 需要创建一个Java类写个方法 去 加载provider.xml 注册到dubbo + zookeeper
55. package com.xiaoshuai.impl;
56. import org.springframework.context.support.ClassPathXmlApplicationContext;
- 57.
58. public class ProviderServer {
- 59. public static void main(String[] args){
- 60. try {
- 61. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
- 62. context.start();
- 63. System.in.read();
- 64. } catch (Exception e) {
- 65. e.printStackTrace();
- 66. }
- 67. }
68. }
- 可以运行。启动该提供者服务,编写消费者。
- 可以看到dubbo 提供者服务已经注册
- myConsumer 项目相关配置
- pom配置
- <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>
- <parent>
- <artifactId>mydubbo</artifactId>
- <groupId>com</groupId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <artifactId>myConsumer</artifactId>
- 10. <dependencies>
- 11. <dependency>
- 12. <groupId>com</groupId>
- 13. <artifactId>myService</artifactId>
- 14. <version>0.0.1-SNAPSHOT</version>
- 15. </dependency>
- 16. </dependencies>
</project>
- 消费者进行订阅provider的服务,也就是需要去zookeeper读取加载服务,那就需要创建一个consumer.xml去加载zookeeper
18. <?xml version="1.0" encoding="UTF-8"?>
19. <beans xmlns="http://www.springframework.org/schema/beans"
- 20. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- 21. xsi:schemaLocation="http://www.springframework.org/schema/beans
- 22. http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- 23. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
- 24. <dubbo:application name="hello-consumer" />
- 25. <!-- 使用multicast广播注册中心暴露发现服务地址 -->
- 26. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
- 27. <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
- 28. <dubbo:reference id="helloService" interface="com.xiaoshuai.HelloService" />
29. </beans>
- 创建一个消费者方法,进行调用ConsumerClient
31. package com.xiaoshuai;
- 32.
33. import org.springframework.context.support.ClassPathXmlApplicationContext;
- 34.
35. public class ConsumerClient {
- 36. public static void main(String[] args) {
- 37. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
- 38. HelloService helloService = (HelloService) context.getBean("helloService");
- 39. String result = helloService.speakHello("xiaoshuai");
- 40. System.out.println(result);
- 41. }
42. }
- 采取debug的方式运行该方法输出以下内容
- 接下来看dubbo中心有什么内容。
- 看看消费者里面显示些什么内容
- 详细消费者信息如图所示
- 以上一个简单的分布式的Demo就已经完结
浙公网安备 33010602011771号