五、zookeeper(3)安装并配置为dubbo的注册中心示例

    xiaoxiao2022-12-05  40

    挺久之前自己装过的一个例子, 安装部分忘了参考的谁的博客, 此处就不贴原链接了, 有兴趣的新手小伙伴可以参考一下, 有问题欢迎共同讨论, 此处先记录下来, 以后有时间再回来修改。

    1.zookeeper 安装注册中心

    根据 dubbo 发布服务和引用服务:发布和引用服务中心在 Zookeeper 里面。

    Dubbo 发布服务到 Zookeeper 注册中心,用户获取服务从 Zookeeper 注册中心获取。

     

    安装步骤:

    1) 导入 Zookeeper 到/usr/local/Hadoop

    a) 使用 rz 命令导入 zookeeper-3.4.5.tar.gz

    2) 解压

    a) tar -zxvf zookeeper-3.4.5.tar.gz

    3) 修改 Zookeeper 名称

    a) mv zookeeper-3.4.5 zookeeper //修改为 Zookeeper

    4) 修改 Zookeeper 配置文件

    a) 修改配置文件名称

    [root@localhost conf]# ll

    total 12

    -rw-r--r--. 1 mine games 535 Oct 1 2012 configuration.xsl

    -rw-r--r--. 1 mine games 2161 Oct 1 2012 log4j.properties

    -rw-r--r--. 1 mine games 808 Oct 1 2012 zoo_sample.cfg

    [root@localhost conf]# mv zoo_sample.cfg zoo.cfg

    b) 加载数据目录

    i. dataDir=/usr/local/hadoop/zookeeper/data

    ii. 配置位置:zoo.cfg

    c) 加载日志目录

    i. dataLogDir=/usr/local/hadoop/zookeeper/log

    ii. 配置位置:zoo.cfg

    d) 在 Zookeeper 目录下创建 data 和 log

    e) Zookeeper 端口 clientPort=2181

     

    5) 启动 Zookeeper 服务

    a) 进入 bin 目录:./zkServer.sh start

    b) 查询 Zookeeper 启动状态:./zkServer.sh status

    6) 登录

    a) 进入 bin 目录:./zkCli.sh

    b) 查询节点信息:ls /

     

    2.dubbo

    2.1解压一个tomcat,进入ROOT目录

    2.2下载dubbo-admin-2.4.1.war包,在Linux的tomcat部署

    (1)先把dubbo-admin-2.5.4.war移动到ROOT中进行解压:

            jar -xvf dubbo-admin-2.4.1.war

        (2)然后到webapps/ROOT/WEB-INF下,有一个dubbo.properties文件,里面指向Zookeeper ,使用的是Zookeeper 的注册中心,如图所示:

     

     (3)然后启动tomcat服务,用户名和密码:root,并访问服务,显示登陆页面,说明dubbo-admin部署成功,如图所示:

     

    3.SpringMVC与Dubbo的整合,这边使用的Maven的管理项目

    3.1jar包

      <dependency>  

                <groupId>com.alibaba</groupId>  

                <artifactId>dubbo</artifactId>  

                <version>2.5.3</version>  

            </dependency>  

              

             <dependency>  

                <groupId>org.apache.zookeeper</groupId>  

                <artifactId>zookeeper</artifactId>  

                <version>3.4.6</version>  

            </dependency>  

        

          <dependency>  

             <groupId>com.github.sgroschupf</groupId>  

             <artifactId>zkclient</artifactId>  

             <version>0.1</version>  

          </dependency>  

    3.2spring配置文件--暴露服务端

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    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.0.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://code.alibabatech.com/schema/dubbo

    http://code.alibabatech.com/schema/dubbo/dubbo.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://www.springframework.org/schema/mvc

    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

    http://www.springframework.org/schema/util

    http://www.springframework.org/schema/util/spring-util-4.0.xsd">

     

    <!-- 发布服务:把接口service发布Zookeeper注册中心 -->

    <!-- 提供方应用信息,用于计算依赖关系 -->

    <dubbo:application name="dubbo_provider" />

    <!-- 使用multicast广播注册中心暴露服务地址 -->

    <!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> -->

    <!-- 使用dubbo通过Zookeeper协议注册服务 -->

    <dubbo:registry protocol="zookeeper" address="192.168.66.66:2181" />

    <!-- 用dubbo协议在20880端口暴露服务 -->

    <dubbo:protocol name="dubbo" port="20880"/>

     

    <!-- 声明需要暴露的服务接口 -->

    <!-- 创建需要发布对象 -->

    <bean id="userServiceImpl" class="cn.jionglu.manager.service.impl.UserServiceImpl"></bean>

    <!-- 发布服务 -->

    <dubbo:service interface="cn.jionglu.manager.service.UserService"

    ref="userServiceImpl" />

    </beans>

    3.3spring配置文件--客户端

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    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.0.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://code.alibabatech.com/schema/dubbo

    http://code.alibabatech.com/schema/dubbo/dubbo.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://www.springframework.org/schema/mvc

    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

    http://www.springframework.org/schema/util

    http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 引用服务 -->

    <dubbo:application name="jionglu-manager-web" />

    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->

    <!-- 使用dubbo从Zookeeper注册中心获取服务 -->

    <dubbo:registry protocol="zookeeper" address="192.168.66.66:2181" />

    <!-- 引用服务 -->

    <dubbo:reference interface="cn.jionglu.manager.service.UserService"

    id="userService" timeout="1000000"/>

    </beans>

     

     

    最新回复(0)