ITeye博客

你好 dubbo他弟:dubbox

ynp · 2016-05-25 17:56 · java,咱们有缘吗 · 企业架构 · Markdown

你好 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包)

》duboox、spring相关配置文件(dubbo:registry为zookeeper的地址)

<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" />

》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的地址)

<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"/>

》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.(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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。