Dubbo+Zookeeper+Maven+Idea 入门搭建笔记

环境准备:idea+Maven+git+tomcat : 服务端和消费端demo项目可以到我的GitHub上下载:https://github.com/panyingting/study 也可以到我的CSDN上传的资源中下载,
本文是直接粘贴我的有道云笔记下来的,格式不好看别介意, 其实相关资料已经很多了,我这只是总结我个人的,以及给出我的资源,thx:

一、Zookeeper集群安装(同机器)

1. 下载Zookeeper , 解压后,复制三分,分别起名8001、8002、8003,放到一个目录中(我的放在user\当前用户\ava\zk-cluster中)
2. 创建三个data目录和三个log目录,分别存放三个集群的data和log.比如我是在新建的zkData下创建了8001/data 8002/data 和 8003/data.以及8001/log、8002/log、8003/log ;
3. 在上面创建的data目录下,新建myid文件,文件名就是myid,没有后缀,然后8001下的文件内容为1,8002下的myid内容为2,8003下的myid内容为3.
4. 修改zk-cluster中8001、8002、8003 下conf目录中的配置文件zoo.cfg ,下面是我8001下的zoo.cfg ,其中和8002 8003不同的地方我加上注释
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=D:/Users/tibg5/AppData/zkData/8001/data 	 	# 这是我们放myid文件的目录,8002,8003需要修改成自己对应的目录
dataLogDir=D:/Users/tibg5/AppData/zkData/8001/logs 		#这是log目录,8002,8003需要改成自己对应的目录
# the port at which the clients will connect
clientPort=3001									# 这是客户端链接的端口号,8002和8003 需要修改,分别修改成3002和3003 
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

# Cluster Zookeeper Server Address 下面配置不需要修改 要注意的就是,下面server.number (number是1、2、3)分别对应myid中的内容,zookeeper也是通过server后面的数字以及dataDir下的myid内容来判断zookeeper集群的关系的(哪个server对应哪个地址),然后后面两个端口号,一个是跟服务器发送链接的端口,另一个是接受服务器链接的端口
server.1=127.0.0.1:8001:9001
server.2=127.0.0.1:8002:9002
server.3=127.0.0.1:8003:9003

# CSDN   真的是有点烦,这代码格式弄得..
如果报错,请参照控制台打印信息或者log目录下日志信息进行解决。

二、Dubbo Admin端搭建

mvn 编译的时候还是遇到很多问题的,jdk版本问题等等,我上传的资源中有编译好的war包,可以直接使用(但还是得改zookeeper的dubbo.properties 路径,下面介绍怎么改),我的war是jdk8编译的,但目前我在jdk7上用没什么问题,jdk8没试(Linux jdk8编译,到win jdk7上用的)
1、下载源码,cmd进入dubbo-admin目录 , 运行 mv install -Dmaven.test.skip=true 进行编译, 如果编译成功,会在target目录下生成war文件,若失败,根据网上给的答案解决即可;
2. 把上一步编译生成的 war 放入tomcat home/webapps 下,运行tomcat后会自动解压出dubbo-admin-2.5.4-SNAPSHOT目录来,这时一般是启动tomcat 是失败的,因为 dubbo 默认的zookeeper 注册中心和我们配置的 zookeeper 不同,这时需要 netstat -aon | find 8080  然后 taskkill /pid toncat进程号 /f 进行杀掉tomcat进程,然后修改dubbo-admin-2.5.4-SNAPSHOT\WEB-INF\dubbo.properties ,我的dubbo.properties内容为:
dubbo.registry.address=zookeeper://127.0.0.1:3002?backup=127.0.0.1:3001,127.0.0.1:3003
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

3. 再次启动tomcat,访问 http://127.0.0.1:8080/dubbo-admin-2.5.4-SNAPSHOT ,输入用户名密码,进入管理端界面

三、 Dubbo + Spring+Zookeeper 开发


1、首先在idea 中新建两个maven 项目(一个provider端,一个consumer端)

新建完成后,一般是修改maven配置信息,把本地仓库配置成你maven的本地仓库。

2、修改pom.xml 文件,让maven自动注入依赖jar,

下面是我的pom.xml。这里我想多说一点,就是开始从网上找的pom.xml 然后使用时,好多jar下载不下来,都是显示连接超时;最后通过修改代理,还是有一些jar下载不下来,最后,我搜了 一些maven仓库,找那些比较热门的版本,试了最后才成功下载了:
<?xml version="1.0"encoding="UTF-8"?>
<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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.reinty</groupId>
    <artifactId>dubbo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>4.0.0.RELEASE</org.springframework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.6</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.mydubbo</groupId>
            <artifactId>provider</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>


3、 Provider端新建服务(service),以及实现类:

public interfaceMyOrderService {
    publicString getName(String arg);
    publicInteger getSize(String param);
}
@Service("myOrderServiceImpl")
public classMyOrderServiceImplimplementsMyOrderService{
    publicString getName(String arg) {
        return"Name of "+arg;
    }

    publicInteger getSize(String param) {
        returnInteger.parseInt(param);
    }
}

4、服务端配置applicationContext.xml

<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
      xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.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-3.1.xsd ">

    <context:annotation-config/>
    <context:component-scanbase-package="com.mydubbo.orderService"/>

    <!-- 提供方应用信息,用于计算依赖关系 -->
   <dubbo:applicationname="test_provider"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
   <dubbo:registryaddress="zookeeper://127.0.0.1:3002?backup=127.0.0.1:3001,127.0.0.1:3003"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
   <dubbo:protocolname="dubbo"port="20880"/>

    <!-- 声明需要暴露的服务接口 -->
   <dubbo:serviceinterface="com.mydubbo.orderService.MyOrderService"ref="myOrderServiceImpl"/>
</beans>


5、打包服务端(provider端),maven中:clean+package+install

6、Consumer 端在pom.xml中引用provider端打包好的jar包(参见provider端的pom.xml来引用),

7、consumer端配置applicationContext.xml

<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
      xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.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-3.1.xsd ">

    <context:annotation-config/>
    <context:component-scanbase-package="com.mydubbo.service"/>
    <!-- 提供方应用信息,用于计算依赖关系 -->
   <dubbo:applicationname="test_provider"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
   <dubbo:registryaddress="zookeeper://127.0.0.1:3002?backup=127.0.0.1:3001,127.0.0.1:3003"/>
    <dubbo:referenceid="myOrderService"interface="com.mydubbo.orderService.MyOrderService"/>
</beans>


8、consumer端通过依赖注入来调用服务


@Service("orderService")
public classOrderServiceImplimplementsOrderService {

    @Autowired
   MyOrderService myOrderService;

    publicString getName(String arg) {
        returnmyOrderService.getName(arg);
    }

    publicInteger getSize(String param) {
        returnmyOrderService.getSize(param);
    }

    public intgetOrderId(String idStr) {
        returnInteger.parseInt(idStr);
    }
}

9、在管理端写一个启动服务的程序,然后通过运行main方法运行服务端:

public classLuncherProvider {

    public static voidmain(String[] args) {
        try{
            ClassPathXmlApplicationContext context =newClassPathXmlApplicationContext("classpath:applicationContext.xml");

        }catch(Exception e){
            e.printStackTrace();;
        }
        synchronized(LuncherProvider.class){
            while(true){
                try{
                    LuncherProvider.class.wait();
                }catch(Exception e){e.printStackTrace();}
            }
        }


    }
}


10、consumer端测试


public classMyConsumer {

    public static voidmain(String[] args) {
        ClassPathXmlApplicationContext xmlapp =newClassPathXmlApplicationContext("classpath:applicationContext.xml");

        OrderService tOrderService = xmlapp.getBean("orderService",OrderService.class);

        System.out.println(""+tOrderService.getName("test"));

        System.out.println(tOrderService.getSize("23"));

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值