Centos zookeeper集群 dubbo 使用
前言
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等 [ 百度百科 ]
开始
1 .去官网,下载 ZooKeeper
cd /usr/local/
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.2-alpha/zookeeper-3.5.2-alpha.tar.gz
2 .创建文件夹,解压压缩包
mkdir -p /usr/local/zookeeper
tar -zxvf zookeeper-3.5.2-alpha.tar.gz -C /usr/local/zookeeper
3 .配置zookeeper文件
cd /usr/local/zookeeper/zookeeper-3.5.2-alpha/conf/
cp zoo_sample.cfg zoo.cfg
vi /usr/local/zookeeper/zookeeper-3.5.2-alpha/conf/zoo.cfg
配置如下:
dataDir=/usr/local/zookeeper/zookeeper-3.5.2-alpha/data/
server.1=192.168.0.118:2888:3888
server.2=192.168.0.120:2888:3888
server.3=192.168.0.121:2888:3888
注意,这里的server.1(myid),需要在 /usr/local/zookeeper/zookeeper-3.5.2-alpha/data/下面创建文件名为 myid 内容是 1 来标识服务器
4 .配置环境变量,在zookeeper文件后面附加如下配置
vi /etc/profile
配置如下:
# idea - zookeeper-3.5.2 config start - 2017-09-01
export ZOOKEEPER_HOME=/usr/local/zookeeper/zookeeper-3.5.2-alpha/
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH
# idea - zookeeper-3.5.2 config start - 2017-09-01
5 .刷新配置文件,并启动
source /etc/profile
cd /usr/local/zookeeper/zookeeper-3.5.2-alpha/bin/
zkServer.sh start
zkServer.sh status
zkServer.sh start-foreground -- 启动错误信息
6 .如果查看状态报错,请关闭防火墙,命令如下
systemctl stop firewalld
systemctl disable firewalld
8 .成功状态如下
[root@localhost bin]# zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
9 .dubbo中provider.xml配置如下:
<dubbo:application name="hello-world-app"/>
<dubbo:registry protocol="zookeeper" address="192.168.0.118:2181,192.168.0.120:2181,192.168.0.121:2181" />
<dubbo:service interface="com.alvis.service.interfaces.IDemoService" ref="demoService" loadbalance="roundrobin"/>
<bean id="demoService" class="com.alvis.service.implementes.DemoService"/>
pom.xml中配置如下:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
10 .将服务打包成jar文件,发布到三个服务器中,使用命令开始运行服务
java -jar /usr/local/dubbo/dubbo-provider.jar
11 .dubbo consumer多次调用可以看到如下信息,如果某个服务挂了,会自动切换到其他2个节点
Hello world, response form provider: 192.168.0.118:20880
Hello world, response form provider: 192.168.0.120:20880
Hello world, response form provider: 192.168.0.121:20880