---
title: "你好 dubbo他弟：dubbox"
author: "ynp"
date: "2016-05-25T17:56:15Z"
tags: []
categories: ["企业架构"]
slug: "dubbo-dubbox"
draft: false
lastmod: "2016-05-25T17:55:42Z"
id: 2300904
url: "https://ynp.iteye.com/blog/2300904"
---

你好 dubbo他弟：dubbox
---》下载dubbox
* dubbox扩展了dubbo，采用dubbox进行测试
* 地址：https://github.com/dangdangdotcom/dubbox
* 文档参考dubbo的文档，写的很详细。http://dubbo.io/User+Guide-zh.htm

---》编译dubbox
* mvn clean install -Dmaven.test.skip
* dubbox编译没啥问题，碰到问题可以再尝试编译一次，dubbo会遇到一些确库的问题，但网上都有解决方案；

---》安装zookeeper
* 大数据里必用的一个东东，也用到了这里，too strong！
* 下载：http://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.8/
* 解压
* 配置，
创建文件zoo.cfg，添加如下内容，dataDir和：dataLogDir改为自己的路径
tickTime=2000
initLimit=10
syncLimit=5
dataDir=D:/java/zookeeper-3.4.6/data
dataLogDir=D:/java/zookeeper-3.4.6/log
clientPort=2181
server.1=localhost:2287:3387

然后输入 bin/zkServer.cmd 启用zookeeper
---》撰写demo
* dubbox自带了demo，可以查看，比较复杂点，dubbo的比较简单，也有说明文档。
* 创建三个工程 myDubbox-api（开放的api）、myDubbox-provider（服务提供端）、myDubbox-consumer（服务调用端）

************  myDubbox-api 中代码************

》HelloService接口

public interface HelloService {
    String hello(String name);
}

************ myDubbox-provider 中的主要代码 ************

》HelloServiceImpl实现类

public class HelloServiceImpl implements HelloService {

    public String hello(String name) {
        return "hello " + name;
    }
}

》 DemoProvider （负责启动服务）
public class DemoProvider {
    public static void main(String args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/*.xml");
        context.start();
        System.out.println("服务已经启动...");
        System.in.read();
    }
}

》pom.xml 文件（myDubbox-api为依赖的api jar包）

   <dependencies>
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>myDubbox-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
      <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
        </dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>
    </dependencies> 

》duboox、spring相关配置文件（dubbo:registry为zookeeper的地址）

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-provider" owner="programmer" organization="dubbox"/>

    <dubbo:registry address="zookeeper://192.168.10.123:2181"/>
    
     <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.xxx.demo.service.HelloService" ref="helloService" />
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="helloService" class="com.xxx.demo.service.impl.HelloServiceImpl" />
</beans>

》log4j文件
略

************ myDubbox-consumer工程主要代码 ************

》Consumer（负责调用服务）
public class Consumer {
 
    public static void main(String args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String {"dubbo-demo-consumer.xml"});
        context.start();
 
        HelloService demoService = (HelloService)context.getBean("helloService"); // 获取远程服务代理
        String hello = demoService.hello("world"); // 执行远程方法
        System.out.println( hello ); // 显示调用结果
    }
 
}

》duboox、spring相关配置文件（dubbo:registry为zookeeper的地址）

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-consumer" owner="programmer" organization="dubbox"/>

    <dubbo:registry address="zookeeper://192.168.10.123:2181"/>

    <dubbo:reference id="helloService" interface="com.xxx.demo.service.HelloService"/>

</beans>

》log4j文件
略

---》测试
* 运行DemoProvider注册服务
INFO  com.alibaba.dubbo.registry.support.AbstractRegistry.register(302) |   Register: dubbo://192.168.10.123:20880/com.xxx.demo.service.HelloService?anyhost=true&application=demo-provider&dubbo=2.8.4&generic=false&interface=com.xxx.demo.service.HelloService&methods=hello&organization=dubbox&owner=programmer&pid=8888&side=provider&timestamp=1464166655067, dubbo version: 2.8.4, current host: 127.0.0.1
INFO  com.alibaba.dubbo.registry.support.AbstractRegistry.subscribe(325) |   Subscribe: provider://192.168.10.123:20880/com.xxx.demo.service.HelloService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.8.4&generic=false&interface=com.xxx.demo.service.HelloService&methods=hello&organization=dubbox&owner=programmer&pid=8888&side=provider&timestamp=1464166655067, dubbo version: 2.8.4, current host: 127.0.0.1
INFO  com.alibaba.dubbo.registry.support.AbstractRegistry.notify(422) |   Notify urls for subscribe url provider://192.168.10.123:20880/com.xxx.demo.service.HelloService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.8.4&generic=false&interface=com.xxx.demo.service.HelloService&methods=hello&organization=dubbox&owner=programmer&pid=8888&side=provider&timestamp=1464166655067, urls: , dubbo version: 2.8.4, current host: 127.0.0.1
服务已经启动...

* 运行Consumer查看调用结果

INFO  com.alibaba.dubbo.remoting.transport.AbstractClient.<init>(105) |   Start NettyClient yaoningpo/192.168.10.123 connect to the server /192.168.10.123:20880, dubbo version: 2.8.4, current host: 192.168.10.123
INFO  com.alibaba.dubbo.config.ReferenceConfig.createProxy(423) |   Refer dubbo service com.xxx.demo.service.HelloService from url zookeeper://192.168.10.123:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&check=false&dubbo=2.8.4&generic=false&interface=com.xxx.demo.service.HelloService&methods=hello&organization=dubbox&owner=programmer&pid=2500&side=consumer&timestamp=1464166811509, dubbo version: 2.8.4, current host: 192.168.10.123
hello world

---》部署 dubbo-admin，查看服务部署和调用情况
* 把dubbo-admin-2.8.4.war 部署到tomcat
* 修改 \dubbo-admin-2.8.4\WEB-INF\dubbo.properties 下的dubbo.registry.address
* 此文件还可以配置登录密码

参考：
http://www.cnblogs.com/yjmyzz/p/dubbox-demo.html
http://blog.csdn.net/ggibenben1314/article/details/47725241