项目码云GIT地址:https://gitee.com/xshuai/dubbo/

开发工具 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,修改相关配置内容

  1. # The number of milliseconds of each tick
  2. tickTime=2000
  3. # The number of ticks that the initial
  4. # synchronization phase can take
  5. initLimit=10
  6. # The number of ticks that can pass between
  7. # sending a request and getting an acknowledgement
  8. syncLimit=5
  9. # the directory where the snapshot is stored.
  10. # do not use /tmp for storage, /tmp here is just
  11. # example sakes.
  12. dataDir=/tmp/zookeeper
  13. # the port at which the clients will connect
  14. #这块是设置zookeeper的端口。默认2181
  15. clientPort=2181
  16. # the maximum number of client connections.
  17. # increase this if you need to handle more clients
  18. #maxClientCnxns=60
  19. #
  20. # Be sure to read the maintenance section of the
  21. # administrator guide before turning on autopurge.
  22. #
  23. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  24. #
  25. # The number of snapshots to retain in dataDir
  26. #autopurge.snapRetainCount=3
  27. # Purge task interval in hours
  28. # Set to "0" to disable auto purge feature
  29. #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配置如下

  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>com</groupId>
  5. <artifactId>mydubbo</artifactId>
  6. 注意这里。打包为POM,分布式系统的第一步
  7. <packaging>pom</packaging>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <name>mydubbo</name>
  10. <url>http://maven.apache.org</url>
  11. <properties>
  12. <spring.version>3.2.4.RELEASE</spring.version>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.alibaba</groupId>
  17. <artifactId>dubbo</artifactId>
  18. <version>2.5.3</version>
  19. <exclusions>
  20. <exclusion>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring</artifactId>
  23. </exclusion>
  24. </exclusions>
  25. </dependency>
  26. <!--dubbo注册中心-->
  27. <dependency>
  28. <groupId>org.apache.zookeeper</groupId>
  29. <artifactId>zookeeper</artifactId>
  30. <version>3.4.6</version>
  31. </dependency>
  32. <!--zookeeper客户端-->
  33. <dependency>
  34. <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>
  35. 创建子项目后自动会加载。刚开始是没有的
  36. <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 项目相关配置

  1. pom配置

    1. <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">
    2. <modelVersion>4.0.0</modelVersion>
    3. <parent>
    4. <artifactId>mydubbo</artifactId>
    5. <groupId>com</groupId>
    6. <version>0.0.1-SNAPSHOT</version>
    7. </parent>
    8. <name>myService</name>
    9. <artifactId>myService</artifactId>
    10. <packaging>jar</packaging>
    11. </project>
  2. 新建一个接口类

    1. package com.xiaoshuai;
    2. public interface HelloService {
    3. public String speakHello(String name);
    4. }
  3. 以上工作完成后,进行maven install

  • myProvider项目相关配置

  1. pom配置

    1. <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">
    2. <modelVersion>4.0.0</modelVersion>
    3. <parent>
    4. <artifactId>mydubbo</artifactId>
    5. <groupId>com</groupId>
    6. <version>0.0.1-SNAPSHOT</version>
    7. </parent>
    8. <artifactId>myProvider</artifactId>
    9. 引用接口服务项目 子项目maven install 为jar
    10. <dependencies>
    11. <dependency>
    12. <groupId>com</groupId>
    13. <artifactId>myService</artifactId>
    14. <version>0.0.1-SNAPSHOT</version>
    15. </dependency>
    16. </dependencies>
    17. </project>
  2. 实现接口服务者

    1. package com.xiaoshuai.impl;
    2. import com.xiaoshuai.HelloService;
    3. public class HelloServiceImpl implements HelloService{
    4. public String speakHello(String name) {
    5. return "你好:"+name+"欢迎查阅小帅丶博客";
    6. }
    7. }
  3. 将实现类接口类暴露给dubbo+zookeeper 在myProvider创建provider.xml 内容如下

    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 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    7. <!-- 提供方应用信息,用于计算依赖关系 -->
    8. <dubbo:application name="hello-provider" />
    9. <!-- 使用multicast广播注册中心暴露服务地址 -->
    10. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    11. <!-- 用dubbo协议在20880端口暴露服务 -->
    12. <dubbo:protocol name="dubbo" port="20880" />
    13. <!-- 声明需要暴露的服务接口 -->
    14. <dubbo:service interface="com.xiaoshuai.HelloService" ref="helloService" />
    15. <!-- 和本地bean一样实现服务 -->
    16. <bean id="helloService" class="com.xiaoshuai.impl.HelloServiceImpl" />
    17. </beans>
  4. 需要创建一个Java类写个方法 去 加载provider.xml 注册到dubbo + zookeeper

    1. package com.xiaoshuai.impl;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. public class ProviderServer {
    4. public static void main(String[] args){
    5. try {
    6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
    7. context.start();
    8. System.in.read();
    9. } catch (Exception e) {
    10. e.printStackTrace();
    11. }
    12. }
    13. }
  5. 可以运行。启动该提供者服务,编写消费者。

  6. 可以看到dubbo 提供者服务已经注册

  • myConsumer 项目相关配置

  1. pom配置

    1. <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">
    2. <modelVersion>4.0.0</modelVersion>
    3. <parent>
    4. <artifactId>mydubbo</artifactId>
    5. <groupId>com</groupId>
    6. <version>0.0.1-SNAPSHOT</version>
    7. </parent>
    8. <artifactId>myConsumer</artifactId>
    9. <dependencies>
    10. <dependency>
    11. <groupId>com</groupId>
    12. <artifactId>myService</artifactId>
    13. <version>0.0.1-SNAPSHOT</version>
    14. </dependency>
    15. </dependencies>
    16. </project>
  2. 消费者进行订阅provider的服务,也就是需要去zookeeper读取加载服务,那就需要创建一个consumer.xml去加载zookeeper

    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" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans
    5. http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    6. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    7. <dubbo:application name="hello-consumer" />
    8. <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    9. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    10. <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    11. <dubbo:reference id="helloService" interface="com.xiaoshuai.HelloService" />
    12. </beans>
  3. 创建一个消费者方法,进行调用ConsumerClient

    1. package com.xiaoshuai;
    2. import org.springframework.context.support.ClassPathXmlApplicationContext;
    3. public class ConsumerClient {
    4. public static void main(String[] args) {
    5. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
    6. HelloService helloService = (HelloService) context.getBean("helloService");
    7. String result = helloService.speakHello("xiaoshuai");
    8. System.out.println(result);
    9. }
    10. }
  4. 运行该方法输出一下内容

  5. 接下来看dubbo中心有什么内容。

  6. 看看消费者里面显示些什么内容

  7. 详细消费者信息如图所示

  • 以上一个简单的分布式的Demo就已经完结

Zookeeper+Dubbo+SpringMVC环境搭建的更多相关文章

  1. 【转载】Maven+druid+MyBatis+Spring+Oracle+Dubbo开发环境搭建

    原地址:http://blog.csdn.net/wp1603710463/article/details/48247817#t16 Maven+druid+MyBatis+spring+Oracle ...

  2. [转]ZooKeeper 集群环境搭建 (本机3个节点)

    ZooKeeper 集群环境搭建 (本机3个节点) 是一个简单的分布式同步数据库(或者是小文件系统) ------------------------------------------------- ...

  3. SpringMVC环境搭建和详解

    1.Spring容器和SpringMVC容器是父子容器 1.1 SpringMVC容器可以调用Spring容器中的所有内容 1.2 图示 2.SpringMVC环境搭建 1.导入jar包 2.在web ...

  4. zookeeper集群环境搭建详细图文教程

    zookeeper集群环境搭建详细图文教程 zhoubang @ 2018-01-02 [文档大纲] 友情介绍 软件环境 注意点 环境安装 1. 新建用于存储安装包以及软件安装的目录 2. 下载安装z ...

  5. springmvc环境搭建及实例

    一. 软件环境 eclipse-jee-mars-R-win32-x86_64 jdk1.7.0_79 apache-tomcat-7.0.52 spring-framework-3.2.0.RELE ...

  6. Linux环境下ZooKeeper集群环境搭建关键步骤

    ZooKeeper版本:zookeeper-3.4.9 ZooKeeper节点:3个节点 以下为Linux环境下ZooKeeper集群环境搭建关键步骤: 前提条件:已完成在Linux环境中安装JDK并 ...

  7. Dubbo简单环境搭建

    Dubbo服务的发展和作用: 首先,看下一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越多之后的常规方案演进历程. 其次,当服务越来越多之后,我们需要做哪些服务治理? 最后,是d ...

  8. Dubbo系列(二)dubbo的环境搭建

    dubbo是一个分布式服务框架,提供一个SOA的解决方案.简单的说,dubbo就像在生产者和消费者中间架起了一座桥梁,使之能透明交互.本文旨在搭建一个可供使用和测试的dubbo环境,使用了spring ...

  9. 【2020-03-21】Dubbo本地环境搭建-实现服务注册和消费

    前言 本周主题:加班工作.本周内忙于CRUD不能自拔,基本每天都是九点半下班,下周上线,明天还要加班推进进度.今天是休息日,于是重拾起了dubbo,打算近期深入了解一下其使用和原理.之所以说是重拾,是 ...

随机推荐

  1. 树链剖分——NOI2015

    8说了上代码 给定一棵树,两种操作 a x:x->root路径上的点权值置1 b x: 把x的子树所有结点权值置0 树上的区间更新操作,显然是要维护dfs 第一个操作暴力显然是t,用树剖把复杂度 ...

  2. 实践笔记_J2EE_Server_Tomcat_tomcat域名绑定_1_单域名绑定

                                                                      Tomcat域名绑定(1)单域名绑定 1. 测试环境说明 名称 版本 ...

  3. ​Django-model

    Model:数据库操作 创建数据库的表: django不能自动创建数据库,但能创建表 在web的models里定义生成表 在project的settings里设置app定义和数据库信息 步骤: 1.创 ...

  4. Django By Example 总结

    从3月1号到3月23号总共24天完成了三个项目,时间很赶,学的很充实,但是憋的有点难受了,从下周开始就有上机实验了,抽不出来很多时间学课外的东西了.任务进度拖延了一个星期的进度,主要是因为我懒.... ...

  5. knn算法详解

    邻近算法,或者说K最近邻(kNN,k-NearestNeighbor)分类算法是数据挖掘分类技术中最简单的方法之一.所谓K最近邻,就是k个最近的邻居的意思,说的是每个样本都可以用它最接近的k个邻居来代 ...

  6. eclipse使用lombok简化java代码

    可以利用注解来代替getter.setter.toString.hashCode.构造方法等,简化代码开发. 具体用法 https://www.cnblogs.com/qnight/p/8997493 ...

  7. Linux 添加新用户账号并赋予root权限

    除了root用户之外,通常需要为每个管理创建各自的用户账号,方便每个管理员登录使用, 步骤如下: 1.  添加新用户账号 useradd  mary.lee 2.  为新用户账号设置密码 passwd ...

  8. 【Vue-Cli3.0】【2】渲染

    哈哈哈,发工资啦,发工资啦,立刻就买了一个matebook D .开启了新的编程工作区了. 进入正题 Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统.这句话来自 ...

  9. Azkaban日志中文乱码问题解决

    Azkaban作为LinkedIn开源的任务流式管理工具,在工作中很大程度上被用到.但是,由于非国人开发,对中文的支持性很不好.大多数情况下,会出现几种乱码现象: - 执行内置脚本生成log乱码 - ...

  10. BZOJ.4160.[NEERC2009]Exclusive Access 2(状压DP Dilworth定理)

    BZOJ DAG中,根据\(Dilworth\)定理,有 \(最长反链=最小链覆盖\),也有 \(最长链=最小反链划分数-1\)(这个是指最短的最长链?并不是很确定=-=),即把所有点划分成最少的集合 ...