Dubbo的helloworld maven版

在写dubbo的helloworld前,先安装zookeeper注册中心,阿里文档推荐用这个软件。我用的操作系统是ubuntu14.0.4 ,zookeeper版本zookeeper-3.4.8,安装过程按照阿里提供的文档一步步来的:
http://dubbo.io/Administrator+Guide.htm#AdministratorGuide-ZookeeperRegistryInstallation
总结安装zookeeper的过程就是:
1/下载zookeeper,解压任意目录
2/将 conf/zoo_sample.cfg文件复制重命名 conf/zoo.cfg(内容不用改,因为默认的属性值都有)
3/启动zookeeper 命令: ./bin/zkServer.sh start
4/验证是否安装成功
telnet 127.0.0.1 2181
dump


以上是zookeeper的安装过程
如果你不想看dubbo的helloworld详细步骤,你也可以直接把我写的两个demo下载下来直接运行,项目源码下载地址:https://pan.baidu.com/s/1bVkJ86 密码 appw

以下是项目目录截图和详细代码
Dubbo的helloworld maven版_第1张图片
先写服务端,新建maven项目,
HelloWorldService接口

public interface HelloWorldService {

    String sayHi(String content);
}

实现类:

public class HelloWorldServiceImpl implements HelloWorldService {

    public String sayHi(String content) {
        return "hi "+content;
    }

}

pom.xml配置


    <dependency>
        <groupId>org.apache.zookeepergroupId>
        <artifactId>zookeeperartifactId>
        <version>3.4.5version>
        
        <exclusions>
               <exclusion>
                   <groupId>log4jgroupId>
                   <artifactId>log4jartifactId>
               exclusion>
           exclusions>
    dependency>

    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>dubboartifactId>
        <version>2.4.9version>
    dependency>

    <dependency>
        <groupId>com.github.sgroschupfgroupId>
        <artifactId>zkclientartifactId>
        <version>0.1version>
    dependency>

spring.xml配置:

        
    <dubbo:application name="providerDemo" />
    <dubbo:registry address="zookeeper://10.57.194.128:2181" />

        
    <dubbo:service interface="com.evan.customer.HelloWorldService" ref="dubboApi"/>
    
    <bean id="dubboApi" class="com.evan.dubbo.impl.HelloWorldServiceImpl" />

PrivoderTest启动测试类:

public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring.xml" });
        System.in.read(); // 防止系统退出
    }

客户端调用项目,新建customer的maven项目
1/新建HelloWorldService接口

public interface HelloWorldService {

    String sayHi(String content);
}

2/pom.xml配置


    <dependency>
        <groupId>org.apache.zookeepergroupId>
        <artifactId>zookeeperartifactId>
        <version>3.4.5version>
        <exclusions>
               <exclusion>
                   <groupId>log4jgroupId>
                   <artifactId>log4jartifactId>
               exclusion>
           exclusions>
    dependency>

    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>dubboartifactId>
        <version>2.4.9version>
    dependency>

            <dependency>
            <groupId>com.github.sgroschupfgroupId>
            <artifactId>zkclientartifactId>
            <version>0.1version>
        dependency>

spring.xml配置:

    
    <dubbo:application name="providerDemo" />
    <dubbo:registry address="zookeeper://10.57.194.128:2181" />

    
    <dubbo:reference id="hello" interface="com.evan.customer.HelloWorldService"/>

客户端测试代码

public static void main(String[] args) {

        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        HelloWorldService hw = (HelloWorldService) ac.getBean("hello");

        String result = hw.sayHi("dubbo");
        System.out.println(result);
    }

调用成功输出截图:
Dubbo的helloworld maven版_第2张图片
需要注意:
1,zookeeper一定要正确安装,启动
2,先运行provider,再运行customer
3,helloworldservice包名要相同 (按理说应该将接口声明单独提出来,然后让客户端和服务端都依赖同一个jar包)

可能遇到的问题
1,如果provider的main方法运行两次,会报端口占用异常。
解决办法:查找占用端口进程对应的pid,然后kill
Dubbo的helloworld maven版_第3张图片
2,spring.xml关于dubbo的配置会出现:
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ‘dubbo:application’.
可以忽略它,不影响运行。网上有人说可以找dubbo.xsd,我自己没试过,而且看了评论好像也不管用。

你可能感兴趣的:(dubbo)