工具:zookeeper-3.4.5.tar.gz以及jdk-7u55
下载:https://download.youkuaiyun.com/download/ly_linyuan/10377506
1.java-version 首先查看是否已经安装过jdk,若未安装则先安装jdk
2.使用ftp工具或者sftp模式上传zookeeper-3.4.5.tar.gz文件
3.tar zxf zookeeper-3.4.6.tar.gz 解压zookeeper文件,进入zookeeper目录创建data文件:
cd zokeeper-3.4.6
mkdir data
4.进入conf目录,将目录zoo_sample.cfg改名zoo.cfg
cd conf
mv zoo_sample.cfg zoo.cfg
6.编辑zoo.cfg文件,将dataDri=/tmp/zookeeper 改为我们自己的当前目录/root/zookeeper-3.4.6/data
vim zoo.cfg
修改(dataDir=/root/zookeeper-3.4.6/data)
7.开启注册中心,
cd bin 进入bin目录
./zkServer.sh start 开启注册中心
./zkServer.sh status 查看状态
./zkServer.sh stop 关闭注册中心
注意要关闭linux的防火墙
实例:
1.在工程中引入jar包
<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>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
2.在spring的配置文件中添加dubbo的约束,然后使用dubbo:service发布服务。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
**xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"** xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
**http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd**
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="com.ly.service"></context:component-scan>
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="lymall-manager" />
<dubbo:registry protocol="zookeeper"
address="192.168.64.129:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.ly.service.GoodService" ref="goodServiceImpl" />
</beans>
3.引用服务,在springmvc的配置文件中添加服务的引用
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.ly.controller" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 引用dubbo服务 -->
<dubbo:application name="lymall-manager-web"/>
<dubbo:registry protocol="zookeeper" address="192.168.25.154:2181"/>
<dubbo:reference interface="com.ly.service.GoodService" id="goodService" />
</beans>