预计阅读时间: 17 分钟
ZooKeeper集群由一组Server节点组成,这一组Server节点中存在一个角色为Leader的节点,其他节点都为Follower。当客户端Client连接到ZooKeeper集群,并且执行写请求时,这些请求会被发送到Leader节点上,然后Leader节点上数据变更会同步到集群中其他的Follower节点。
ZooKeeper采用一种称为Leader election的选举算法(也有称做:分布式选举算法-Paxos)的。在整个集群运行过程中,只有一个Leader,其他的都是Follower,如果ZooKeeper集群在运行过程中Leader出了问题,系统会采用该算法重新选出一个Leader,ZooKeeper用于三台以上的服务器集群之中,只要还有超过半数的服务器在线,ZooKeeper就能够正常提供服务,过半,意味着实际能够有效参与选举的节点数量是奇书个数,否者不能有效的过半。
Zookeeper逻辑图如下,
Zookeeper下载地址: http://下载Zookeeper-3.4.6.tar.gz 地址http://www.apache.org/dist/zookeeper/
设置Maven依赖
<!--dubbo服务注册中心--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <!--zookeeper apache--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <!--zkclient--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>
xml文件目录
ClientService接口
package com.journaldev.spring.service; /** * Created by pengguangyu on 2016/3/17. */ public interface ClientService { public String sayHello(String name); public String like(String name); }
ClientServiceImpl
package com.journaldev.spring.service.provider; import com.journaldev.spring.service.ClientService; /** * Created by pengguangyu on 2016/3/17. */ public class ClientServiceImpl implements ClientService { @Override public String sayHello(String name) { return "Hello " + name; } @Override public String like(String name){ return "doubi like " + name; } }
dubbo-provider.xml 暴露ClientService接口
<?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"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="hello-world-app" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <!--<dubbo:registry address="multicast://224.5.6.8:1234" />--> <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register="" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.journaldev.spring.service.ClientService" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.journaldev.spring.service.provider.ClientServiceImpl" /> </beans>
dubbo-consumer.xml 调用远程接口
<?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"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="consumer-of-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <!--<dubbo:registry address="multicast://224.5.6.8:1234" />--> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.journaldev.spring.service.ClientService" /> </beans>
Provider.java
import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by pengguangyu on 2016/3/17. */ public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-extend-config/dubbo-provider.xml"}); context.start(); System.in.read(); // 按任意键退出 } }
Consumer.java
import com.journaldev.spring.service.ClientService; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by pengguangyu on 2016/3/17. */ public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-extend-config/dubbo-consumer.xml"}); context.start(); ClientService demoService = (ClientService)context.getBean("demoService"); // 获取远程服务代理 String hello = demoService.sayHello("world"); // 执行远程方法 System.out.println( hello ); // 显示调用结果 System.out.println( demoService.like("nimei") ); // 显示调用结果 } }
运行Provider.java和Consumer.java之前,先运行Zookeeper的服务端和客户端。
运行结果: