此文章发表前,作者没有搞懂dubbox与dubbo区别,请见谅!!!

一、安装zookeeper
安装环境:linux
1.首先去zookeeper官方网站下在tar.gz包
2.解压
tar -zxvf xxx.tar.gz
3.修改配置文件
路径:zookeeper/conf
可能看到sample_zoo.cfg
vi 一下,看看内容作如下修改,一般改下dataDir既可。

# 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.
dataDir=~/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181

这里有一个端口号:2181
4.启动服务:
在zookeeper/bin/下运行:

./zkServer.sh start

5.可以看一下端口是否起来:

netstat -antp 2181

6.停止服务

./zkServer.sh stop

二.搭建Dubbbox
由于我的pom.xml是直接拿过来的,所以这里说一下:
个人看法:
既然使用了Dubbo和zookeeper那么他们的相关jar是必须的了
dubbo的maven

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
</dependency>

zookeeper的客户端maven

<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.3</version>
</dependency>

根据阿里巴巴官方介绍:dubbo是基于Spring的(可能是这个意思吧),所有基本的spring也得有。spring的引用就不贴了。


开始Demo之旅
首先一览我的Demo项目:
这里写图片描述
parent:就是jar的引用
provider:服务提供者
consumer:消费者

dubbox个人分析(小白阶段):
服务提供者(Provider)—->注册中心(Registry)<—-服务消费者(Consumer)
注册中心,配置下下就好了。
那么如何写Provider和Consumer呢??
服务提供者(DemoService)
一如既往:接口&实现
DemoService.java

package com.xbz.learning.dubbox.demo.provider;

public interface DemoService {
    public String sayHello();
}

DemoService.java

package com.xbz.learning.dubbox.demo.provider.impl;

import com.xbz.learning.dubbox.demo.provider.DemoService;

public class DemoServiceImpl implements DemoService{

    public String sayHello() {
        return "hello world";
    }

}

服务端配置

<?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:c="http://www.springframework.org/schema/c"

    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd

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

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

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

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app"  />

    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.35.100:2181" check="false" subscribe="false" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service version="1.0.0" interface="com.xbz.learning.dubbox.demo.provider.DemoService" ref="demoService" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.xbz.learning.dubbox.demo.provider.impl.DemoServiceImpl"></bean>

</beans>

运行服务提供者,实际应该是向注册中心注册服务

public class DemoServiceRegisterTest {
    @Test
    public void startDemoProvider() throws IOException{
        System.out.println("准备启动DemoServiceProvider");
        ClassPathXmlApplicationContext ap=new ClassPathXmlApplicationContext("classpath:applicationContext-provider.xml");
        DemoService demoServiceProvider=(DemoService) ap.getBean("demoService");
        String res=demoServiceProvider.sayHello();
        System.out.println(res);
        System.out.println("启动DemoServiceProvider完成");
        System.in.read();
    }
}

消费者与服务提供者,需要共用接口类



服务消费者
消费者拥有提供者的接口,然后进行如下配置

<?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:c="http://www.springframework.org/schema/c"

    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd

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

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

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

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer-of-helloworld-app"  />

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://192.168.35.100:2181" />

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference version="1.0.0" id="demoService" interface="com.xbz.learning.dubbox.demo.provider.DemoService" />

</beans>

测试服务是否可以运行:

public class DemoConsumerTest {
    @Test
    public void testDemoServiceProvider() throws IOException{
        ClassPathXmlApplicationContext ap=new ClassPathXmlApplicationContext("classpath:applicationContext-consumer.xml");
        DemoService demoServiceProvider=(DemoService) ap.getBean("demoService");
        for(int i=1;i<=10;i++){
            String res=demoServiceProvider.sayHello();
            System.out.println("第"+i+"次调用"+res);
        }

    }
}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐