你好 dubbo他弟:dubbox

阅读更多
你好 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包)

  
       
            com.xxx
            myDubbox-api
            0.0.1-SNAPSHOT
       

     
            com.alibaba
            dubbo
            2.8.4
       


com.github.sgroschupf
zkclient
0.1

   


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


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

   

   
   
    
   

   
   


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


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

   

   

   



》log4j文件


---》测试
* 运行DemoProvider注册服务
INFO [2016-05-25 16:57:35,354] com.alibaba.dubbo.registry.support.AbstractRegistry.register(302) |  [DUBBO] 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×tamp=1464166655067, dubbo version: 2.8.4, current host: 127.0.0.1
INFO [2016-05-25 16:57:35,482] com.alibaba.dubbo.registry.support.AbstractRegistry.subscribe(325) |  [DUBBO] 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×tamp=1464166655067, dubbo version: 2.8.4, current host: 127.0.0.1
INFO [2016-05-25 16:57:35,590] com.alibaba.dubbo.registry.support.AbstractRegistry.notify(422) |  [DUBBO] 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×tamp=1464166655067, urls: [empty://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×tamp=1464166655067], dubbo version: 2.8.4, current host: 127.0.0.1
服务已经启动...

* 运行Consumer查看调用结果

INFO [2016-05-25 17:00:12,170] com.alibaba.dubbo.remoting.transport.AbstractClient.(105) |  [DUBBO] 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 [2016-05-25 17:00:12,208] com.alibaba.dubbo.config.ReferenceConfig.createProxy(423) |  [DUBBO] 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×tamp=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

你可能感兴趣的:(spring,dubbox,企业应用)