dubbo+sping+zookeeper入门教程

jopen 9年前

条件受限我这里是win7

准备工作:dubbo-admin-2.5.4-SNAPSHOT.war、zookeeper-3.4.5.tar.gz、tomcat(配的是8080端口)

一、安装zookeeper(实际开发中都是运维直接配置好的)

把zookeeper解压到D盘,进入D:\zookeeper-3.4.5\conf ;把zoo_sample.cfg文件中的dataDir=修改为dataDir=D:\\zookeeper-3.4.5\\data,保存然后重命名为:zoo.cfg,可以再log4j.properties里面修改日志路径

二、启动cmd d:     cd:D:\zookeeper-3.4.5\bin   输入zkServer.cmd 就启动了,使用netstat – ano命令查看clientPort 端口号(clientPort=2181)在监听服务

三、安装dubbo ,直接把war包丢到tomcat webapps中解压 全部放到root中,检查WEB-INF下的dubbo.properties配置的dubbo.registry.address是不是跟已装的zk一致;启动tomcat:

访问:http://127.0.0.1:8080;用户名密码都是:root

四、测试

dubbo+sping+zookeeper入门教程

dubbo+sping+zookeeper入门教程

部分jar包在dubbo的lib下面直接复制过来

生产者:

package com.dk.provider.service;    import java.util.List;    /**   * PS  :   * User: alec   * Date: 13-08-12   * Time: 下午4:11   */  public interface ProviderService {            String sayHello(String name);      public List getNames();  }
实现类
package com.dk.provider.service.impl;    import com.dk.provider.service.ProviderService;  import com.google.common.collect.Lists;    import java.util.List;    /**   * PS  :   * User: alec   * Date: 13-08-12   * Time: 下午4:12   */  public class ProviderServiceImpl implements ProviderService {          @Override      public String sayHello(String name) {          return "hello" + name;      }        @Override      public List getNames() {          return Lists.newArrayList("张三", "李四", "王五");      }  }
启动:
package com.dk.provider;    import org.springframework.context.support.ClassPathXmlApplicationContext;    /**   * PS  :   * User: alec   * Date: 13-08-12   * Time: 下午4:31   */  public class Provider {      public static void main(String[] args) throws Exception {          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"application-provider.xml"});          context.start();          System.out.println("provider ready");          System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟        }  }


application-provider.xml
<?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-3.2.xsd         http://code.alibabatech.com/schema/dubbo         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">        <!-- 具体的实现bean -->      <bean id="providerService" class="com.dk.provider.service.impl.ProviderServiceImpl" />        <!-- 提供方应用信息,用于计算依赖关系 -->      <dubbo:application name="fuck_provider"  />        <!-- 使用zookeeper注册中心暴露服务地址 -->      <dubbo:registry address="zookeeper://127.0.0.1:2181" />        <!-- 用dubbo协议在20880端口暴露服务 -->      <dubbo:protocol name="dubbo" port="20880" />        <!-- 声明需要暴露的服务接口 -->      <dubbo:service interface="com.dk.provider.service.ProviderService" ref="providerService" />  </beans>


消费者:
package com.dk.consumer;    import com.dk.provider.service.ProviderService;  import org.springframework.context.support.ClassPathXmlApplicationContext;    import java.util.List;    /**   * PS  :   * User: alec   * Date: 13-08-12   * Time: 下午4:36   */  public class Consumer {      public static void main(String[] args) throws Exception {          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"application-consumer.xml"});          context.start();            ProviderService providerService = (ProviderService) context.getBean("providerService");          String hello = providerService.sayHello("alec");          System.out.println(hello);            List list = providerService.getNames();          for (Object obj : list) {              System.out.println(obj);          }            System.in.read();      }  }

application-consumer.xml


<?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-3.2.xsd         http://code.alibabatech.com/schema/dubbo         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">        <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->      <dubbo:application name="fuck_consumer" />        <!-- 使用zookeeper注册中心暴露服务地址 -->      <dubbo:registry address="zookeeper://127.0.0.1:2181" />        <!-- 生成远程服务代理-->      <dubbo:reference id="providerService" interface="com.dk.provider.service.ProviderService" />  </beans>




依次执行Provider,Consumer

不粗什么意外应该输出以下结果:

dubbo+sping+zookeeper入门教程

dubbo+sping+zookeeper入门教程

进入dubbo页面,导航栏 ====》》》 服务选项

dubbo+sping+zookeeper入门教程




来自:http://my.oschina.net/ffy/blog/537535