一、Linux安装Zookeeper
1.将zookeeper安装包zookeeper-3.4.12.tar.gz上传到linux服务器上并解压到/usr/local/soft文件夹中。
2.进入zookeeper根目录,创建data文件夹
[root@localhost bin]# cd /usr/local/soft/zookeeper/
[root@localhost zookeeper]# mkdir data
3.修改conf下的zoo_sample.cfg文件。
[root@localhost zookeeper]# mv conf/zoo_sample.cfg conf/zoo.cfg
[root@localhost zookeeper]# vim conf/zoo.cfg
修改文件中的dataDir
dataDir=/usr/local/soft/zookeeper/data
4.开启Zookeeper服务
[root@localhost zookeeper]#./zkServer.sh start
关闭为:./zkServer.sh stop
查看状态为:./zkServer.sh status
5.关闭防火墙
firewall防火墙
[root@localhost ~]# service firewalld stop
iptables防火墙
[root@localhost ~]# service iptables stop
永久关闭自己百度下。
二、Maven工程添加依赖
添加如下依赖(pom.xml)
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
服务层与表现层的pom.xml中都需要添加。
注意一定要添加curator依赖,否则会报错。
三、spring容器中(服务层)
<!--dubbo发布服务-->
<dubbo:application name="e3-manager"/>
<!--address为放Zookeeper的服务器的ip加Zookeeper端口号-->
<dubbo:registry protocol="zookeeper" address="192.168.247.20:2181"/>
<!--用dubbo协议在20880端口暴露服务-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--声明需要暴露的服务接口-->
<dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl"/>
四、springmvc容器中(表现层)
<dubbo:application name="e3-manager-web" />
<dubbo:registry protocol="zookeeper" address="192.168.247.20:2181" />
<dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />