微信搜索superit|邀请体验:大数据, 数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

zookeeper+Dubbo环境搭建及简单Demo

dubbo aide_941 24℃

zookeeper+Dubbo环境搭建及简单Demo

 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zgsdzczh/article/details/78664561

一、运行环境

本文运行环境:windows 7  + JDK1.8 + zookeeper-3.3.6 + dubbo-admin-2.5.4 (支持JDK 1.8)+ dubbo 2.5.3

所需工具可至资源页下载  –》点我下载

Demo下载

本文采用Dubbo与Zookeeper、Spring框架的整合。

主要是以下几个步骤:
1. 安装Zookeeper,启动;
2. 创建MAVEN项目,构建Dubbo+Zookeeper+Spring实现的简单Demo;
3. 安装Dubbo-admin,实现监控。

二、安装配置Zookeeper

   解压zookeeper-3.3.6.tar.gz到指定目录下    D:\zookeeper-3.4.8
   修改zoo_sample.cfg 文件名(D:\zookeeper-3.4.8\conf) 为 zoo.cfg
配置文件简单解析

1、tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
2、dataDir:是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
3、dataLogDir:是 Zookeeper 保存日志文件的目录
4、clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
在(D:\zookeeper-3.4.8\)目录下创建/tmp/zookeeper文件夹

启动

进入到bin文件夹下,在上面地址栏中输入cmd进入命令行,输入zkServer.cmd,若如图显示则代表启动成功。

三、创建MAVEN项目

(编译环境使用Intellij JDK版本为1.8)

项目结构:

dubbo-api : 存放公共接口;
dubbo-consumer : 调用远程服务;
dubbo-provider : 提供远程服务。

具体如图所示:

1、新建MAVEN项目 DubboDemo

导入所需要的jar包依赖。

  1. <project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  2. xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”>
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>DubboDemo</groupId>
  5. <artifactId>DubboDemo</artifactId>
  6. <packaging>pom</packaging>
  7. <version>1.0-SNAPSHOT</version>
  8. <modules>
  9. <module>dubbo-api</module>
  10. <module>dubbo-consumer</module>
  11. <module>dubbo-provider</module>
  12. </modules>
  13. <name>DubboDemo Maven Webapp</name>
  14. <url>http://maven.apache.org</url>
  15. <properties>
  16. <maven.compiler.source>1.8</maven.compiler.source>
  17. <maven.compiler.target>1.8</maven.compiler.target>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <!– spring版本号 –>
  21. <spring.version>4.2.5.RELEASE</spring.version>
  22. <!– mybatis版本号 –>
  23. <mybatis.version>3.2.8</mybatis.version>
  24. <!– mysql驱动版本号 –>
  25. <mysql-driver.version>5.1.29</mysql-driver.version>
  26. <!– log4j日志包版本号 –>
  27. <slf4j.version>1.7.18</slf4j.version>
  28. <log4j.version>1.2.17</log4j.version>
  29. </properties>
  30. <dependencies>
  31. <dependency>
  32. <groupId>junit</groupId>
  33. <artifactId>junit</artifactId>
  34. <version>3.8.1</version>
  35. <scope>test</scope>
  36. </dependency>
  37. <!– 添加jstl依赖 –>
  38. <dependency>
  39. <groupId>jstl</groupId>
  40. <artifactId>jstl</artifactId>
  41. <version>1.2</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>javax</groupId>
  45. <artifactId>javaee-api</artifactId>
  46. <version>7.0</version>
  47. </dependency>
  48. <!– 添加junit4依赖 –>
  49. <dependency>
  50. <groupId>junit</groupId>
  51. <artifactId>junit</artifactId>
  52. <version>4.11</version>
  53. <!– 指定范围,在测试时才会加载 –>
  54. <scope>test</scope>
  55. </dependency>
  56. <!– 添加spring核心依赖 –>
  57. <dependency>
  58. <groupId>org.springframework</groupId>
  59. <artifactId>spring-core</artifactId>
  60. <version>${spring.version}</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.springframework</groupId>
  64. <artifactId>spring-web</artifactId>
  65. <version>${spring.version}</version>
  66. </dependency>
  67. <dependency>
  68. <groupId>org.springframework</groupId>
  69. <artifactId>spring-oxm</artifactId>
  70. <version>${spring.version}</version>
  71. </dependency>
  72. <dependency>
  73. <groupId>org.springframework</groupId>
  74. <artifactId>spring-tx</artifactId>
  75. <version>${spring.version}</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>org.springframework</groupId>
  79. <artifactId>spring-jdbc</artifactId>
  80. <version>${spring.version}</version>
  81. </dependency>
  82. <dependency>
  83. <groupId>org.springframework</groupId>
  84. <artifactId>spring-webmvc</artifactId>
  85. <version>${spring.version}</version>
  86. </dependency>
  87. <dependency>
  88. <groupId>org.springframework</groupId>
  89. <artifactId>spring-context</artifactId>
  90. <version>${spring.version}</version>
  91. </dependency>
  92. <dependency>
  93. <groupId>org.springframework</groupId>
  94. <artifactId>spring-context-support</artifactId>
  95. <version>${spring.version}</version>
  96. </dependency>
  97. <dependency>
  98. <groupId>org.springframework</groupId>
  99. <artifactId>spring-aop</artifactId>
  100. <version>${spring.version}</version>
  101. </dependency>
  102. <dependency>
  103. <groupId>org.springframework</groupId>
  104. <artifactId>spring-test</artifactId>
  105. <version>${spring.version}</version>
  106. </dependency>
  107. <!– 添加mybatis依赖 –>
  108. <dependency>
  109. <groupId>org.mybatis</groupId>
  110. <artifactId>mybatis</artifactId>
  111. <version>${mybatis.version}</version>
  112. </dependency>
  113. <!– 添加mybatis/spring整合包依赖 –>
  114. <dependency>
  115. <groupId>org.mybatis</groupId>
  116. <artifactId>mybatis-spring</artifactId>
  117. <version>1.2.2</version>
  118. </dependency>
  119. <!– 添加mysql驱动依赖 –>
  120. <dependency>
  121. <groupId>mysql</groupId>
  122. <artifactId>mysql-connector-java</artifactId>
  123. <version>${mysql-driver.version}</version>
  124. </dependency>
  125. <!– 添加数据库连接池依赖 –>
  126. <dependency>
  127. <groupId>commons-dbcp</groupId>
  128. <artifactId>commons-dbcp</artifactId>
  129. <version>1.2.2</version>
  130. </dependency>
  131. <!– 添加fastjson –>
  132. <dependency>
  133. <groupId>com.alibaba</groupId>
  134. <artifactId>fastjson</artifactId>
  135. <version>1.2.22</version>
  136. </dependency>
  137. <!– 添加日志相关jar包 –>
  138. <dependency>
  139. <groupId>log4j</groupId>
  140. <artifactId>log4j</artifactId>
  141. <version>${log4j.version}</version>
  142. </dependency>
  143. <dependency>
  144. <groupId>org.slf4j</groupId>
  145. <artifactId>slf4j-api</artifactId>
  146. <version>${slf4j.version}</version>
  147. </dependency>
  148. <dependency>
  149. <groupId>org.slf4j</groupId>
  150. <artifactId>slf4j-log4j12</artifactId>
  151. <version>${slf4j.version}</version>
  152. </dependency>
  153. <!– log end –>
  154. <!– 映入JSON –>
  155. <dependency>
  156. <groupId>org.codehaus.jackson</groupId>
  157. <artifactId>jackson-mapper-asl</artifactId>
  158. <version>1.9.13</version>
  159. </dependency>
  160. <!– https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core –>
  161. <dependency>
  162. <groupId>com.fasterxml.jackson.core</groupId>
  163. <artifactId>jackson-core</artifactId>
  164. <version>2.8.0</version>
  165. </dependency>
  166. <!– https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind –>
  167. <dependency>
  168. <groupId>com.fasterxml.jackson.core</groupId>
  169. <artifactId>jackson-databind</artifactId>
  170. <version>2.8.0</version>
  171. </dependency>
  172. <dependency>
  173. <groupId>commons-fileupload</groupId>
  174. <artifactId>commons-fileupload</artifactId>
  175. <version>1.3.1</version>
  176. </dependency>
  177. <dependency>
  178. <groupId>commons-io</groupId>
  179. <artifactId>commons-io</artifactId>
  180. <version>2.4</version>
  181. </dependency>
  182. <dependency>
  183. <groupId>commons-codec</groupId>
  184. <artifactId>commons-codec</artifactId>
  185. <version>1.9</version>
  186. </dependency>
  187. <dependency>
  188. <groupId>org.quartz-scheduler</groupId>
  189. <artifactId>quartz</artifactId>
  190. <version>2.2.1</version>
  191. </dependency>
  192. <dependency>
  193. <groupId>org.apache.shiro</groupId>
  194. <artifactId>shiro-core</artifactId>
  195. <version>1.3.2</version>
  196. </dependency>
  197. <dependency>
  198. <groupId>org.apache.shiro</groupId>
  199. <artifactId>shiro-web</artifactId>
  200. <version>1.3.2</version>
  201. </dependency>
  202. <dependency>
  203. <groupId>org.apache.shiro</groupId>
  204. <artifactId>shiro-spring</artifactId>
  205. <version>1.3.2</version>
  206. </dependency>
  207. <dependency>
  208. <groupId>org.apache.shiro</groupId>
  209. <artifactId>shiro-ehcache</artifactId>
  210. <version>1.3.2</version>
  211. </dependency>
  212. <dependency>
  213. <groupId>org.apache.zookeeper</groupId>
  214. <artifactId>zookeeper</artifactId>
  215. <version>3.4.9</version>
  216. </dependency>
  217. <!– dubbo –>
  218. <dependency>
  219. <groupId>com.alibaba</groupId>
  220. <artifactId>dubbo</artifactId>
  221. <version>2.5.3</version>
  222. <exclusions>
  223. <exclusion>
  224. <groupId>org.springframework</groupId>
  225. <artifactId>spring</artifactId>
  226. </exclusion>
  227. </exclusions>
  228. </dependency>
  229. <dependency>
  230. <groupId>com.101tec</groupId>
  231. <artifactId>zkclient</artifactId>
  232. <version>0.10</version>
  233. </dependency>
  234. <dependency>
  235. <groupId>DubboDemo</groupId>
  236. <artifactId>dubbo-api</artifactId>
  237. <version>1.0-SNAPSHOT</version>
  238. </dependency>
  239. </dependencies>
  240. <build>
  241. <finalName>DubboDemo</finalName>
  242. </build>
  243. </project>

2、在此项目下新建MAVEN项目:dubbo-api
创建接口DemoService

  1. package com.zh.dubbo.demo;
  2. import java.util.List;
  3. public interface DemoService {
  4. List<String> getPermissions(Long id);
  5. }

(若不能创建java文件,则在java文件夹点击右键,选择Mark Directory As —–Source Root即可
3、创建dubbo-provider的MAVEN项目实现公共接口

  1. package com.zh.dubbo.demo.impl;
  2. import com.zh.dubbo.demo.DemoService;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. public class DemoServiceImpl implements DemoService {
  6. public List<String> getPermissions(Long id) {
  7. List<String> demo = new ArrayList<String>();
  8. demo.add(String.format(“Permission_%d”, id – 1));
  9. demo.add(String.format(“Permission_%d”, id));
  10. demo.add(String.format(“Permission_%d”, id + 1));
  11. return demo;
  12. }
  13. }

添加Spring配置声明文件 Provider.xml  此文件放在target/classes文件夹下

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3. xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4. xmlns:dubbo=“http://code.alibabatech.com/schema/dubbo”
  5. xsi:schemaLocation=“http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd”>
  9. <!–定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识–>
  10. <dubbo:application name=“demotest-provider” owner=“programmer” organization=“dubbox”/>
  11. <!–使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper–>
  12. <dubbo:registry address=“zookeeper://localhost:2181”/>
  13. <!– 用dubbo协议在20880端口暴露服务 –>
  14. <dubbo:protocol name=“dubbo” port=“20880” />
  15. <!–使用 dubbo 协议实现定义好的 api.PermissionService 接口–>
  16. <dubbo:service interface=“com.zh.dubbo.demo.DemoService” ref=“demoService” protocol=“dubbo” />
  17. <!–具体实现该接口的 bean–>
  18. <bean id=“demoService” class=“com.zh.dubbo.demo.impl.DemoServiceImpl”/>
  19. </beans>

添加启动远程服务类Provider.class:

  1. package com.zh.dubbo.demo.impl;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import java.io.IOException;
  4. public class Provider {
  5. public static void main(String[] args) throws IOException {
  6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“provider.xml”);
  7. System.out.println(context.getDisplayName() + “: here”);
  8. context.start();
  9. System.out.println(“服务已经启动…”);
  10. System.in.read();
  11. }
  12. }

4、创建dubbo-consumer的MAVEN项目添加Spring配置声明文件 consumer.xml  此文件放在target/classes文件夹下

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3. xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4. xmlns:dubbo=“http://code.alibabatech.com/schema/dubbo”
  5. xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd”>
  7. <dubbo:application name=“demotest-consumer” owner=“programmer” organization=“dubbox”/>
  8. <!–向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送–>
  9. <dubbo:registry address=“zookeeper://localhost:2181”/>
  10. <!–使用 dubbo 协议调用定义好的 api.PermissionService 接口–>
  11. <dubbo:reference id=“permissionService” interface=“com.zh.dubbo.demo.DemoService”/>
  12. </beans>

创建Consumer类, 启动Consumer,调用远程服务

  1. package com.zh.dubbo.consumer;
  2. import com.zh.dubbo.demo.DemoService;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Consumer {
  5. public static void main(String[] args) {
  6. //测试常规服务
  7. ClassPathXmlApplicationContext context =
  8. new ClassPathXmlApplicationContext(“consumer.xml”);
  9. context.start();
  10. System.out.println(“consumer start”);
  11. DemoService demoService = context.getBean(DemoService.class);
  12. System.out.println(“consumer”);
  13. System.out.println(demoService.getPermissions(1L));
  14. }
  15. }

5、运行项目,先确保provider已被运行后再启动consumer模块
四、Dubbo管理控制台配置

1、把dubbo-admin.war放到tomcat的webapps目录下

2、启动tomcat

3、在webapps文件夹下找到dubbo-admin文件夹,进入WEB-INF文件夹,打开dubbo.properties,可以看到账户密码分别为root guest

4、在启动zookeeper后,然后再启动tomcat,访问地址:http://localhost:8080/dubbo-admin/

输入帐号密码后即可登录到后台

–结束–

转载请注明:SuperIT » zookeeper+Dubbo环境搭建及简单Demo

喜欢 (0)or分享 (0)