zookeeper3.4.5集群部署及dubbo2.5.3入门例子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zookeeper3.4.5集群部署及dubbo2.5.3入门例子相关的知识,希望对你有一定的参考价值。

一、基础信息配置

11 虚拟机硬件配置

技术分享

技术分享

技术分享

技术分享

技术分享


 

12虚拟机软件配置(准备三台机器)

121 公共配置

 

[[email protected] ~]# cat /etc/redhat-release 
CentOS release 6.7 (Final)
[[email protected] ~]# uname -r
2.6.32-573.el6.x86_64
[[email protected] ~]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"


 

12 2三台机器各自配置

1221 ZK11机器

 

 

[[email protected] ~]# hostname
zk11
[[email protected] ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=zk11
 
#对外提供IP,这里简称外网IP
[[email protected] ~]# ifconfig eth0|awk -F ‘[ :]+‘ ‘NR==2{print $4}‘
10.0.0.11
 
#对内提供IP,这里简称内网IP
[[email protected] ~]# ifconfig eth1|awk -F ‘[ :]+‘ ‘NR==2{print$4}‘
172.16.1.11


 

1222 ZK12机器

 

 [[email protected]~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=zk12
[[email protected] ~]# ifconfig eth0|awk -F ‘[ :]+‘ ‘NR==2{print $4}‘
10.0.0.12
[[email protected] ~]# ifconfig eth1|awk -F ‘[ :]+‘ ‘NR==2{print$4}‘ 
172.16.1.12


 

1223 ZK13机器

 

[[email protected] ~]# hostname
zk13
[[email protected] ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=zk13
[[email protected] ~]# ifconfig eth0|awk -F ‘[ :]+‘ ‘NR==2{print$4}‘ 
10.0.0.13
[[email protected] ~]# ifconfig eth1|awk -F ‘[ :]+‘ ‘NR==2{print$4}‘ 
172.16.1.13


 

123建立安装部署文件目录

 

#各个安装包上传目录
[[email protected] ~]# mkdir -p /home/llyweb/tools/
 
#各个安装包软链接目录
[[email protected] ~]# mkdir /application/


 

二、Zookeeper3.4.5部署

21三台机器共同配置

211上传JDK以及zookeeper

 

[[email protected] tools]# pwd
/home/llyweb/tools
[[email protected]k11 tools]# ll jdk-7u79-linux-x64.tar.gzzookeeper-3.4.5.tar.gz
-rw-r--r-- 1 root root 153512879 Apr 18 15:22jdk-7u79-linux-x64.tar.gz
-rw-r--r-- 1 root root 16402010 Apr 18 15:19 zookeeper-3.4.5.tar.gz


 

212安装JDK及配置环境变量

 

参考“java的maven工程部署到linux的前半部分,

http://ylcodes01.blog.51cto.com/5607366/1774050”

 

213三台机器共同操作配置

 

[[email protected] tools]# pwd
/home/llyweb/tools
[[email protected] tools]# tar -zxf zookeeper-3.4.5.tar.gz
[[email protected] tools]# ls -ld zookeeper-3.4.5
drwxr-xr-x 11 root root 4096 Apr 18 15:33 zookeeper-3.4.5
[[email protected] tools]# ln -s/home/llyweb/tools/zookeeper-3.4.5/ /application/zookeeper
[[email protected] zookeeper]# mkdir data
[[email protected] zookeeper]# ls -ld/application/zookeeper/data/
drwxr-xr-x 3 root root 4096 Apr 18 17:42/application/zookeeper/data/
[[email protected] conf]# pwd
/application/zookeeper/conf
[[email protected] conf]# cp zoo_sample.cfg zoo.cfg
#编辑zoo.cfg文件
#修改dataDir为
dataDir=/application/zookeeper/data
#最下面加入
server.1=10.0.0.11:2888:3888
server.2=10.0.0.12:2888:3888
server.3=10.0.0.13:2888:3888


 

22 各个机器配置

221 ZK11-10.0.0.11配置

 

[[email protected] zookeeper]# echo "1" >/application/zookeeper/data/myid

 

222 ZK12-10.0.0.12配置

 

[[email protected] tools]# echo "2" >/application/zookeeper/data/myid

 

223 ZK13-10.0.0.13配置

 

[[email protected] ~]# echo "3" >/application/zookeeper/data/myid

 

224注意事项

 

上述三者机器的写入/application/zookeeper/data/myid中的数字1、2、3一定要对应
“2、1、3三台机器共同操作配置”中的
server.1=10.0.0.11:2888:3888
server.2=10.0.0.12:2888:3888
server.3=10.0.0.13:2888:3888


 

23测试部署

231分别启动三台机器

 

[[email protected] zookeeper]# /bin/sh/application/zookeeper/bin/zkServer.sh start
JMX enabled by default
Using config: /application/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
 
[[email protected] tools]# /bin/sh /application/zookeeper/bin/zkServer.shstart
JMX enabled by default
Using config: /application/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
 
[[email protected] ~]# /bin/sh/application/zookeeper/bin/zkServer.sh start
JMX enabled by default
Using config: /application/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED


 

 

232检查三台机器状态

 

#zookeeper推荐奇数型集群配置,下述三者机器的主节点不一定是哪台,在执行下述命令的时候可能会报错,猜测是还没检测好节点,稍等一会即可看到下述成功标识。

 

[[email protected] zookeeper]# /bin/sh/application/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /application/zookeeper/bin/../conf/zoo.cfg
Mode: follower
 
[[email protected] tools]# /bin/sh/application/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /application/zookeeper/bin/../conf/zoo.cfg
Mode: leader
 
[[email protected] ~]# /bin/sh/application/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /application/zookeeper/bin/../conf/zoo.cfg
Mode: follower
 
[[email protected] zookeeper]# ps -ef|grep zookeeper|grep -v grep


root      2625      1  0 19:42 pts/2    00:00:01 /application/jdk/bin/java-Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp/application/zookeeper/bin/../build/classes:/application/zookeeper/bin/../build/lib/*.jar:/application/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/application/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/application/zookeeper/bin/../lib/netty-3.2.2.Final.jar:/application/zookeeper/bin/../lib/log4j-1.2.15.jar:/application/zookeeper/bin/../lib/jline-0.9.94.jar:/application/zookeeper/bin/../zookeeper-3.4.5.jar:/application/zookeeper/bin/../src/java/lib/*.jar:/application/zookeeper/bin/../conf:.:/application/jdk/lib/dt.jar:/application/jdk/lib/tools.jar-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=falseorg.apache.zookeeper.server.quorum.QuorumPeerMain/application/zookeeper/bin/../conf/zoo.cfg

 

[[email protected] tools]# ps -ef|grep zookeeper|grep -v grep

root      2317      1  0 19:42 pts/1    00:00:01 /application/jdk/bin/java-Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp/application/zookeeper/bin/../build/classes:/application/zookeeper/bin/../build/lib/*.jar:/application/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/application/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/application/zookeeper/bin/../lib/netty-3.2.2.Final.jar:/application/zookeeper/bin/../lib/log4j-1.2.15.jar:/application/zookeeper/bin/../lib/jline-0.9.94.jar:/application/zookeeper/bin/../zookeeper-3.4.5.jar:/application/zookeeper/bin/../src/java/lib/*.jar:/application/zookeeper/bin/../conf:.:/application/jdk/lib/dt.jar:/application/jdk/lib/tools.jar-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=falseorg.apache.zookeeper.server.quorum.QuorumPeerMain/application/zookeeper/bin/../conf/zoo.cfg

 

[[email protected] ~]# ps -ef|grep zookeeper|grep -v grep

root      2297      1  0 19:41 pts/1    00:00:01 /application/jdk/bin/java-Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp/application/zookeeper/bin/../build/classes:/application/zookeeper/bin/../build/lib/*.jar:/application/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/application/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/application/zookeeper/bin/../lib/netty-3.2.2.Final.jar:/application/zookeeper/bin/../lib/log4j-1.2.15.jar:/application/zookeeper/bin/../lib/jline-0.9.94.jar:/application/zookeeper/bin/../zookeeper-3.4.5.jar:/application/zookeeper/bin/../src/java/lib/*.jar:/application/zookeeper/bin/../conf:.:/application/jdk/lib/dt.jar:/application/jdk/lib/tools.jar-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=falseorg.apache.zookeeper.server.quorum.QuorumPeerMain/application/zookeeper/bin/../conf/zoo.cfg

 

 

 

233停止三台机器(仅测试)

 

[[email protected] zookeeper]# /bin/sh/application/zookeeper/bin/zkServer.sh stop
JMX enabled by default
Using config: /application/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[[email protected] zookeeper]# ps -ef|grep zookeeper|grep -v grep
[[email protected] zookeeper]# /bin/sh/application/zookeeper/bin/zkServer.sh start
JMX enabled by default
Using config: /application/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[[email protected] zookeeper]# /bin/sh/application/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /application/zookeeper/bin/../conf/zoo.cfg
Error contacting service. It is probably not running. #出现2、3、2提到的情况
[[email protected] zookeeper]# /bin/sh/application/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /application/zookeeper/bin/../conf/zoo.cfg
Mode: follower


 

234命令测试

 

[[email protected] zookeeper]# cd bin/
[[email protected] bin]# /bin/sh zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls     #显示下述是表示不支持此操作
#下述是zookeeper中所有的命令
ZooKeeper -server host:port cmd args
        connecthost:port
        get path[watch]
        ls path[watch]
        set pathdata [version]
        rmr path
        delquota[-n|-b] path
        quit 
       printwatches on|off
        create [-s][-e] path data acl
        stat path[watch]
        close 
        ls2 path[watch]
        history 
        listquotapath
        setAcl pathacl
        getAcl path
        sync path
        redo cmdno
        addauthscheme auth
        delete path[version]
        setquota-n|-b val path
[zk: localhost:2181(CONNECTED) 1] ls /zookeeper
[quota]
[zk: localhost:2181(CONNECTED) 3] ls /zookeeper/quota 
[]
[zk: localhost:2181(CONNECTED) 4] create /test1 test1
Created /test1
[zk: localhost:2181(CONNECTED) 5] get /test1         
test1
cZxid = 0x600000003
ctime = Tue Apr 18 19:50:10 CST 2017
mZxid = 0x600000003
mtime = Tue Apr 18 19:50:10 CST 2017
pZxid = 0x600000003
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
[zk: localhost:2181(CONNECTED) 6] create /test1/test2test2
Created /test1/test2
[zk: localhost:2181(CONNECTED) 7] get /test1/test2
test2
cZxid = 0x600000004
ctime = Tue Apr 18 19:50:27 CST 2017
mZxid = 0x600000004
mtime = Tue Apr 18 19:50:27 CST 2017
pZxid = 0x600000004
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
[zk: localhost:2181(CONNECTED) 8] set /test1/test2 test22
cZxid = 0x600000004
ctime = Tue Apr 18 19:50:27 CST 2017
mZxid = 0x600000005
mtime = Tue Apr 18 19:50:40 CST 2017
pZxid = 0x600000004
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
[zk: localhost:2181(CONNECTED) 9] get /test1/test2
test22
cZxid = 0x600000004
ctime = Tue Apr 18 19:50:27 CST 2017
mZxid = 0x600000005
mtime = Tue Apr 18 19:50:40 CST 2017
pZxid = 0x600000004
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
[zk: localhost:2181(CONNECTED) 10] create/test1/test2/test3 test3
Created /test1/test2/test3
[zk: localhost:2181(CONNECTED) 11] delete /test1
Node not empty: /test1
[zk: localhost:2181(CONNECTED) 12] delete/test1/test2/test3      
[zk: localhost:2181(CONNECTED) 13] ls /test1/test2
[]
[zk: localhost:2181(CONNECTED) 14] ls /test1      
[test2]
 
#zk12机器操作
[zk: localhost:2181(CONNECTED) 0] get /test1
test1
cZxid = 0x600000003
ctime = Tue Apr 18 19:50:10 CST 2017
mZxid = 0x600000003
mtime = Tue Apr 18 19:50:10 CST 2017
pZxid = 0x600000004
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 1
 
#zk13机器操作
[zk: localhost:2181(CONNECTED) 0] get /test1/test2
test22
cZxid = 0x600000004
ctime = Tue Apr 18 19:50:27 CST 2017
mZxid = 0x600000005
mtime = Tue Apr 18 19:50:40 CST 2017
pZxid = 0x600000008
cversion = 2
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
 
#zk11机器操作
[zk: localhost:2181(CONNECTED) 15] rmr /test1
[zk: localhost:2181(CONNECTED) 16] get /test1
Node does not exist: /test1


 

 

 

三、DUBBO2.5.3部署

 

31上传Tomcatdubbo-admin.war

32安装部署dubbo

321部署Tomcat

参考“java的maven工程部署到linux的前半部分,

http://ylcodes01.blog.51cto.com/5607366/1774050”

 

312只在ZK11机器部署dubbo

3、1、2、1dubbo-admin.war说明

 

dubbo-admin.war可在下载dubbo源码后,通过maven编译生成对应版本的dubbo-admin.war,也可搜索出已打包好的dubbo-admin.war。

 

3122部署dubbo-admin.war

 

[[email protected] tools]# rm -rf/application/tomcat/webapps/ROOT/*
[[email protected] tools]# unzip dubbo-admin.war -d/application/tomcat/webapps/ROOT/
[[email protected] WEB-INF]# pwd
/application/tomcat/webapps/ROOT/WEB-INF
[[email protected] WEB-INF]# cp dubbo.propertiesdubbo.properties.ori
[[email protected] WEB-INF]# cat dubbo.properties
dubbo.registry.address=zookeeper://10.0.0.11:2181?backup=10.0.0.12:2181,10.0.0.13:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest


 

3123启动Tomcat并访问dubbo

 

#在启动此Tomcat之前记得在/etc/hosts的最后加入127.0.0.1 zk11
[[email protected] application]# /bin/sh/application/tomcat/bin/startup.sh >/dev/null 2>&1
[[email protected] application]# ps -ef|grep java|grep -v grep


root      2708      1  0 19:47 pts/2    00:00:04 /application/jdk/bin/java-Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp/application/zookeeper/bin/../build/classes:/application/zookeeper/bin/../build/lib/*.jar:/application/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/application/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/application/zookeeper/bin/../lib/netty-3.2.2.Final.jar:/application/zookeeper/bin/../lib/log4j-1.2.15.jar:/application/zookeeper/bin/../lib/jline-0.9.94.jar:/application/zookeeper/bin/../zookeeper-3.4.5.jar:/application/zookeeper/bin/../src/java/lib/*.jar:/application/zookeeper/bin/../conf:.:/application/jdk/lib/dt.jar:/application/jdk/lib/tools.jar-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=falseorg.apache.zookeeper.server.quorum.QuorumPeerMain/application/zookeeper/bin/../conf/zoo.cfg

root      2934      1 58 20:07 pts/0    00:00:49 /application/jdk/bin/java-Djava.util.logging.config.file=/application/tomcat/conf/logging.properties-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager-Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/application/tomcat/endorsed-classpath/application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar-Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat-Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrapstart

 

 

技术分享



 

四、Java整合DUBBO

41服务端test1和客户端test2目录结构

 技术分享


 

42服务端test1代码配置

421Java代码部分

 

package com.unj.dubbotest.provider;  
  
import java.util.List; 
  
public interface DemoService {  
  
    StringsayHello(String name);  
  
    public ListgetUsers();  
  
}


 

 

 
package com.unj.dubbotest.provider;  
  
import java.util.ArrayList;  
import java.util.LinkedList;  
import java.util.List; 
  
  
public class DemoServiceImpl implements DemoService{  
      
     public StringsayHello(String name) {  
            return"Hello " + name;  
     }  
     publicList<User> getUsers() {  
        List<User> list = new ArrayList<User>();  
         User u1 =new User();  
        u1.setName("jack");  
        u1.setAge(20);  
        u1.setSex("男");  
           
         User u2 =new User();  
        u2.setName("tom");  
        u2.setAge(21);  
        u2.setSex("女");  
           
         User u3 =new User();  
        u3.setName("rose");  
        u3.setAge(19);  
        u3.setSex("女");  
           
        list.add(u1);  
        list.add(u2);  
        list.add(u3);  
         returnlist;  
     }  
}


 

 

package com.unj.dubbotest.provider;
 
import java.io.Serializable;
/**
 * 一定要实现序列化接口Serializable
 * @author pc
 *
 */
public class User implements Serializable{
 
    private staticfinal long serialVersionUID = 1L;
    
    private Stringname;
    private Integerage;
    private Stringsex;
    public StringgetName() {
        return name;
    }
    public voidsetName(String name) {
        this.name =name;
    }
    public StringgetSex() {
        return sex;
    }
    public voidsetSex(String sex) {
        this.sex =sex;
    }
    public IntegergetAge() {
        return age;
    }
    public voidsetAge(Integer age) {
        this.age =age;
    }
    
    
}

 

 

package com.unj.dubbotest.provider;
 
importorg.springframework.context.support.ClassPathXmlApplicationContext;  
 
public class Provider { 
   
    public staticvoid main(String[] args) throws Exception { 
       ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});  
       context.start();  
   
       System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟  
    }  
   
}


 

 

422XML代码部分

 

applicationContext.xml

 

 

<?xml version="1.0"encoding="UTF-8"?>  
<beansxmlns="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  
       ">  
   
    <!-- 具体的实现bean -->  
    <beanid="demoService"class="com.unj.dubbotest.provider.DemoServiceImpl" />  
      
    <!-- 提供方应用信息,用于计算依赖关系 -->  
   <dubbo:application name="xixi_provider"  />  
   
    <!-- 使用multicast广播注册中心暴露服务地址   
   <dubbo:registry address="multicast://224.5.6.7:1234"/>-->  
    
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
   <dubbo:registry address="zookeeper://10.0.0.11:2181"/>   
    
    <!-- 用dubbo协议在20880端口暴露服务 -->  
   <dubbo:protocol name="dubbo" port="20880"/>  
   
    <!-- 声明需要暴露的服务接口 -->  
   <dubbo:serviceinterface="com.unj.dubbotest.provider.DemoService"ref="demoService" />  
      
</beans>


 

 

pom.xml

 

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 
 <groupId>test1</groupId>
 <artifactId>test1</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 
 <name>test1</name>
 <url>http://maven.apache.org</url>
 
 <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
 <dependencies>  
   <dependency>  
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId>  
     <version>3.8.1</version> 
     <scope>test</scope>  
   </dependency>  
  
     <dependency>  
         <groupId>commons-logging</groupId>  
         <artifactId>commons-logging</artifactId>  
         <version>1.1.1</version> 
     </dependency>  
     <dependency>  
         <groupId>com.alibaba</groupId>  
         <artifactId>dubbo</artifactId>  
          <version>2.5.3</version>  
     </dependency>  
     <dependency>  
         <groupId>org.javassist</groupId>  
         <artifactId>javassist</artifactId>  
         <version>3.18.1-GA</version> 
     </dependency>  
     <dependency>  
          <groupId>log4j</groupId>  
         <artifactId>log4j</artifactId>  
         <version>1.2.15</version> 
         <exclusions>  
             <exclusion>  
                 <groupId>com.sun.jdmk</groupId>  
                 <artifactId>jmxtools</artifactId>  
             </exclusion>  
             <exclusion>  
                 <groupId>com.sun.jmx</groupId>  
                 <artifactId>jmxri</artifactId>  
             </exclusion>  
             <exclusion>  
                 <artifactId>jms</artifactId> 
                 <groupId>javax.jms</groupId> 
             </exclusion>  
             <exclusion>  
                 <artifactId>mail</artifactId>  
                 <groupId>javax.mail</groupId>  
             </exclusion>  
         </exclusions>  
     </dependency>  
     <dependency>  
         <groupId>org.springframework</groupId>  
         <artifactId>spring</artifactId>  
         <version>2.5.6.SEC03</version>  
     </dependency>  
     <dependency>  
         <groupId>org.slf4j</groupId> 
         <artifactId>slf4j-api</artifactId>  
         <version>1.7.6</version> 
     </dependency>  
     <dependency>  
         <groupId>org.slf4j</groupId> 
         <artifactId>slf4j-log4j12</artifactId>  
         <version>1.6.1</version> 
     </dependency>  
     <dependency>  
         <groupId>org.apache.zookeeper</groupId>  
         <artifactId>zookeeper</artifactId>  
         <version>3.4.5</version> 
         <type>pom</type>  
      </dependency>  
     <dependency>  
         <groupId>com.101tec</groupId>  
         <artifactId>zkclient</artifactId>  
         <version>0.4</version> 
     </dependency>  
  
 </dependencies>
</project>


 

 

43客户端test2代码部分

 

431Java代码部分

 

package com.unj.dubbotest.provider;  
  
import java.util.List; 
  
public interface DemoService {  
  
    StringsayHello(String name);  
  
    public ListgetUsers();  
  
}

 

 

 

package com.unj.dubbotest.provider;
 
import java.util.List; 
 
importorg.springframework.context.support.ClassPathXmlApplicationContext;  
  
 
import com.unj.dubbotest.provider.DemoService;  
  
public class Consumer { 
  
    public staticvoid main(String[] args) throws Exception { 
       ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext(  
                newString[] {"applicationContext.xml"}); 
       context.start();  
  
        DemoServicedemoService = (DemoService) context.getBean("demoService"); //  
        Stringhello = demoService.sayHello("tom"); //   
       System.out.println(hello); //   
  
        //   
       List<User> list = demoService.getUsers();  
        if (list !=null && list.size() > 0) {  
            for(int i = 0; i < list.size(); i++) {  
                System.out.println(list.get(i));  
            }  
        }
        //System.out.println(demoService.hehe());  
       System.in.read();  
    }  
  
}


 

432XML代码部分

 

applicationContext.xml

 

 

<?xml version="1.0"encoding="UTF-8"?>  
<beansxmlns="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:applicationname="hehe_consumer" />  
  
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->  
   <dubbo:registry address="zookeeper://10.0.0.11:2181"/>  
  
    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->  
   <dubbo:reference id="demoService"  
       interface="com.unj.dubbotest.provider.DemoService" />  
  
</beans>


 

 

pom.xml

 

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 
 <groupId>test2</groupId>
 <artifactId>test2</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 
 <name>test2</name>
 <url>http://maven.apache.org</url>
 
 <properties>  
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
   </properties>  
  
   <dependencies>  
       <dependency>  
           <groupId>junit</groupId> 
           <artifactId>junit</artifactId>  
           <version>3.8.1</version> 
           <scope>test</scope>  
       </dependency>  
       <dependency>  
           <groupId>commons-logging</groupId>  
           <artifactId>commons-logging</artifactId>  
           <version>1.1.1</version> 
       </dependency>  
       <dependency>  
           <groupId>com.alibaba</groupId>  
           <artifactId>dubbo</artifactId>  
           <version>2.5.3</version> 
       </dependency>  
       <dependency>  
           <groupId>org.javassist</groupId>  
           <artifactId>javassist</artifactId>  
           <version>3.18.1-GA</version> 
       </dependency>  
       <dependency>  
           <groupId>log4j</groupId> 
            <artifactId>log4j</artifactId>  
           <version>1.2.15</version> 
           <exclusions>  
               <exclusion>  
                   <groupId>com.sun.jdmk</groupId>  
                   <artifactId>jmxtools</artifactId>  
                </exclusion>  
               <exclusion>  
                   <groupId>com.sun.jmx</groupId>  
                   <artifactId>jmxri</artifactId>  
               </exclusion>  
               <exclusion>  
                   <artifactId>jms</artifactId> 
                   <groupId>javax.jms</groupId> 
               </exclusion>  
               <exclusion>  
                   <artifactId>mail</artifactId>  
                   <groupId>javax.mail</groupId>  
               </exclusion>  
           </exclusions>  
       </dependency>  
       <dependency>  
           <groupId>org.springframework</groupId>  
           <artifactId>spring</artifactId>  
           <version>2.5.6.SEC03</version>  
       </dependency>  
        <dependency>  
           <groupId>org.slf4j</groupId> 
           <artifactId>slf4j-api</artifactId>  
           <version>1.7.6</version> 
       </dependency>  
       <dependency>  
           <groupId>org.slf4j</groupId> 
           <artifactId>slf4j-log4j12</artifactId>  
           <version>1.6.1</version> 
       </dependency>  
       <dependency>  
           <groupId>com.github.adyliu</groupId>  
           <artifactId>zkclient</artifactId>  
           <version>2.0</version> 
       </dependency>  
       <dependency>  
           <groupId>org.apache.zookeeper</groupId>  
           <artifactId>zookeeper</artifactId>  
           <version>3.4.5</version> 
           <type>pom</type>  
       </dependency>  
        <dependency>  
           <groupId>test1</groupId> 
           <artifactId>test1</artifactId>  
           <version>0.0.1-SNAPSHOT</version>  
       </dependency>  
   </dependencies>
</project>


 

 

44运行服务

 

#记得一定先要clean install服务端Test1,再clean install客户端Test1

#先运行Test1服务端Provider,再运行Test2客户端Consumer

#结果为

Hello tom

[email protected]

[email protected]

[email protected]

 

 



本文出自 “你可以选择不平凡” 博客,请务必保留此出处http://ylcodes01.blog.51cto.com/5607366/1917527

以上是关于zookeeper3.4.5集群部署及dubbo2.5.3入门例子的主要内容,如果未能解决你的问题,请参考以下文章

zookeeper安装教程(zookeeper3.4.5为例)

zookeeper3.4.5+Hbase1.2.6安装教程

Hadoop学习笔记-011-CentOS_6.5_64_HA高可用-Zookeeper3.4.5安装Kafka+消息监控KafkaOffsetMonitor

Hadoop学习笔记-010-CentOS_6.5_64_HA高可用-Hadoop2.6+Zookeeper3.4.5安装Hbase1.0.0

2.Dubbo2.5.3注册中心和监控中心部署

Hadoop学习笔记-009-CentOS_6.5_64_HA高可用-Hadoop2.6+Zookeeper3.4.5安装Hive1.1.0