zoukankan      html  css  js  c++  java
  • [dubbo实战] dubbo+zookeeper伪集群搭建

     zookeeper作为注册中心,服务器和客户端都要访问,如果有大量的并发,肯定会有等待。所以可以通过zookeeper集群解决。

    一、为什么需要zookeeper呢?

       大部分分布式应用需要一个主控、协调器或者控制器来管理物理分布的子进程。目前,大多数都要开发私有的协调程序,缺乏一个通用机制,协调程序的反复编写浪费,且难以形成通用、伸缩性好的协调器,zookeeper提供通用的分布式锁服务,用以协调分布式应用。所以说zookeeper是分布式应用的协作服务。

    二、zookeeper的工作原理

       核心原理是原子广播,这个机制保证了各个server之间的同步,实现这个机制的协议叫做Zab协议,它有两种模式:恢复和广播模式。

       当服务启动或者在领导者崩溃后,Zab就进入了恢复模式,当领导者被选举出来,恢复模式就结束了。

        一旦zookeeper内部同步好了后,就可以开始广播信息了,这时候当一个server加入zookeeper服务中,它会在恢复模式下启动,并且同步状态,同步结束后它也参与广播消息。

    三、集群搭建准备

    1.我用了三个zookeeper服务,分别是zookeeper-1,zookeeper-2,zookeeper-3

    2.在每个zookeeper下添加data和log文件

    3.配置zoo.cfg文件

     

    [html] view plain copy
     
     print?
    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. dataDir=E:/zookeeper/zookeeper-1/data  
    11. # the directory where the log  
    12. dataLogDir=E:/zookeeper/zookeeper-1/log   
    13. # the port at which the clients will connect  
    14. clientPort=2181  
    15. #Clusters  
    16. server.1=192.168.24.140(127.0.0.1):2888:3888  
    17. server.2=192.168.24.140:2889:3889  
    18. server.3=192.168.24.140:2890:3890   

     每个zookeeper的端口配置都应该不同,分别是2181,2182,2183

     

    4.在data文件添加myid文件(touch myid),并且里面的内容分别为:1,2,3

    5.在dubbo项目中,provider和consumer的xml文件中修改zookeeper的地址,如下:

     

    [html] view plain copy
     
     print?
    1. <!-- 本机 伪集群 测试 -->       
    2.     <dubbo:registry  protocol="zookeeper"  address="192.168.24.140:2181,192.168.24.140:2182,192.168.24.140:2183"  />  


    四、测试

     

        先分别启动zookeeper,然后依次启动服务和客户端。启动服务的时候会看到连接到哪个zookeeper上了。为了测试集群的特性,比如客户端启动后zookeeper-1提供服务,我关掉它,那么程序就继续发起连接,接着可能zookeeper-2提供服务。

       因为现在有三台服务器,所以要求至少有2台正常启动才能运行正常。那么zookeeper-2也关闭,那程序不会连接zookeeper-3,一直报错,提示no further connection。

       上面说了停掉某个服务,zookeeper内部会选举出下一个leader来,它内部存在投票选举机制。这里不多说了。就像MongoDB集群,会根据心跳机制选出主服务器。

       接下来的测试,我继续打开zookeeper-1或zookeeper-2,能正常提供服务。把zookeeper-3关闭,如果zookeeper-1和zookeeper-2重新启动成功后,也是能提供服务的。内部在恢复模式下同步状态。

     

     zookeeper作为注册中心,服务器和客户端都要访问,如果有大量的并发,肯定会有等待。所以可以通过zookeeper集群解决。

    一、为什么需要zookeeper呢?

       大部分分布式应用需要一个主控、协调器或者控制器来管理物理分布的子进程。目前,大多数都要开发私有的协调程序,缺乏一个通用机制,协调程序的反复编写浪费,且难以形成通用、伸缩性好的协调器,zookeeper提供通用的分布式锁服务,用以协调分布式应用。所以说zookeeper是分布式应用的协作服务。

    二、zookeeper的工作原理

       核心原理是原子广播,这个机制保证了各个server之间的同步,实现这个机制的协议叫做Zab协议,它有两种模式:恢复和广播模式。

       当服务启动或者在领导者崩溃后,Zab就进入了恢复模式,当领导者被选举出来,恢复模式就结束了。

        一旦zookeeper内部同步好了后,就可以开始广播信息了,这时候当一个server加入zookeeper服务中,它会在恢复模式下启动,并且同步状态,同步结束后它也参与广播消息。

    三、集群搭建准备

    1.我用了三个zookeeper服务,分别是zookeeper-1,zookeeper-2,zookeeper-3

    2.在每个zookeeper下添加data和log文件

    3.配置zoo.cfg文件

     

    [html] view plain copy
     
     print?
    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. dataDir=E:/zookeeper/zookeeper-1/data  
    11. # the directory where the log  
    12. dataLogDir=E:/zookeeper/zookeeper-1/log   
    13. # the port at which the clients will connect  
    14. clientPort=2181  
    15. #Clusters  
    16. server.1=192.168.24.140:2888:3888  
    17. server.2=192.168.24.140:2889:3889  
    18. server.3=192.168.24.140:2890:3890   

     每个zookeeper的端口配置都应该不同,分别是2181,2182,2183

     

    4.在data文件添加myid文件,并且里面的内容分别为:1,2,3

    5.在dubbo项目中,provider和consumer的xml文件中修改zookeeper的地址,如下:

     

    [html] view plain copy
     
     print?
    1. <!-- 本机 伪集群 测试 -->       
    2.     <dubbo:registry  protocol="zookeeper"  address="192.168.24.140:2181,192.168.24.140:2182,192.168.24.140:2183"  />  


    四、测试

     

        先分别启动zookeeper,然后依次启动服务和客户端。启动服务的时候会看到连接到哪个zookeeper上了。为了测试集群的特性,比如客户端启动后zookeeper-1提供服务,我关掉它,那么程序就继续发起连接,接着可能zookeeper-2提供服务。

       因为现在有三台服务器,所以要求至少有2台正常启动才能运行正常。那么zookeeper-2也关闭,那程序不会连接zookeeper-3,一直报错,提示no further connection。

       上面说了停掉某个服务,zookeeper内部会选举出下一个leader来,它内部存在投票选举机制。这里不多说了。就像MongoDB集群,会根据心跳机制选出主服务器。

       接下来的测试,我继续打开zookeeper-1或zookeeper-2,能正常提供服务。把zookeeper-3关闭,如果zookeeper-1和zookeeper-2重新启动成功后,也是能提供服务的。内部在恢复模式下同步状态。

     

  • 相关阅读:
    〖Linux〗Kubuntu设置打开应用时就只在打开时的工作区显示
    〖Linux〗Kubuntu, the application 'Google Chrome' has requested to open the wallet 'kdewallet'解决方法
    unity, dll is not allowed to be included or could not be found
    android check box 自定义图片
    unity, ios skin crash
    unity, Collider2D.bounds的一个坑
    unity, ContentSizeFitter立即生效
    类里的通用成员函数应声明为static
    unity, Gizmos.DrawMesh一个坑
    直线切割凹多边形
  • 原文地址:https://www.cnblogs.com/linjiaxin/p/7261251.html
Copyright © 2011-2022 走看看