您的位置:首页 > 其它

dubbo 入门教程 基于zookeeper

2016-07-25 14:36 435 查看
第一步 在spring官网下载一个spring比较全的包。 我用的还是以前的3.2

第二步 新建一个web项目。将spring的包放在lib下面。

第三步 在dubbo.io官网的版本库下载dubbo的jar包。

第四步 在Apache的官网下载zookeeper的项目。 zookeeper的安装方法在上篇文章讲过了。拿出zookeeper根目录下面的zookeeper.jar就可以。

附加一些jar 工程下面有的 可以无视。slf4j-api-1.7.5.jar,slf4j-log4j12-1.7.5.jar,netty-3.7.0.Final.jar,jetty-util-6.1.26.jar,jetty-6.1.26.jar,commons-cli-1.2.jar 这些jar在zookeeper3.5的lib下面都有。直接拷贝就可以。

还有一个就是zkclient-0.1.0.jar 一定要下载I0Itec的,这个应该不是zk开发的。这个我也忘记在哪里下载的了。不好意思。要是需要的可以在下面评论 留个邮箱什么的。

下面新建一个接口:

package com.unj.dubbotest.provider;
public abstract interface DemoService {
 
 public abstract String build(String name) throws Exception;
 
}

新建一个实现类

package com.unj.dubbotest.provider.impl;
import com.unj.dubbotest.provider.DemoService;
public class DemoServiceImpl implements DemoService {
 public String build(String name) throws Exception {
  System.out.println("name is === " + name);
  return "你好 名称是 ------------- >>>> " + name;
 }
}

在src下面新建一个applicationContext.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.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">
 <!-- 具体的实现bean -->
 <bean id="demoService"
  class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />
 <!-- 提供方应用信息,用于计算依赖关系 -->
 <dubbo:application name="xixi_provider" />
 <!-- 使用multicast广播注册中心暴露服务地址 
  <dubbo:registry address="multicast://224.5.6.7:1234" />-->
 <!-- 使用zookeeper注册中心暴露服务地址 -->
 <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 <!-- 用dubbo协议在20880端口暴露服务 -->
 <dubbo:protocol name="dubbo" port="20880" />
 <!-- 声明需要暴露的服务接口 -->
 <dubbo:service interface="com.unj.dubbotest.provider.DemoService"
  ref="demoService" />
</beans>

最后写一个启动服务的类 (用过cxf的都知道)

package com.unj.dubbotest.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
 
    public static void main(String[] args) throws Exception {
     
     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
        context.start();
 
        System.out.println("新浪微博:疯狂的杨中仁.");
        
        System.in.read(); // 按任意键退出
    }
 
}


启动zookeeper。在执行启动服务的类 控制台打印了如下信息

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
SLF4J: This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version
新浪微博:疯狂的杨中仁.

说明启动成功了。

下面在做客户端。同意 新建一个web项目 把服务端的jar全部拷贝到lib下面去。

新建一个接口 包名和服务端一样 这边接口 其实要在服务器那边拿过来的。

package com.unj.dubbotest.provider;
public abstract interface DemoService {
 
 public abstract String build(String name) throws Exception;
 
}

在src下面新建一个 applicationConsumer.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.xsd  
        http://code.alibabatech.com/schema/dubbo  
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
        ">
 <!-- consumer application name -->
 <dubbo:application name="consumer-of-helloworld-app" />
 <!-- registry address, used for consumer to discover services -->
 <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 <dubbo:consumer timeout="5000" />
 <!-- which service to consume? -->
 <dubbo:reference id="demoService"
  interface="com.unj.dubbotest.provider.DemoService" />
</beans>


新建一个main类来测试:

package com;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.unj.dubbotest.provider.DemoService;
public class Main {
 
 public static void main(String[] args) {
  
  
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
    new String[] { "applicationConsumer.xml" });
  context.start();
  DemoService demoService = (DemoService) context.getBean("demoService"); // get
  // service
  // invocation
  // proxy
  String hello = "";
  try {
   hello = demoService.build("新浪微博:疯狂的杨中仁");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } // do invoke!
  System.out.println(Thread.currentThread().getName() + " " + hello);
 }
 
}

打印信息如下:

 log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
main 你好 名称是 ------------- >>>> 新浪微博:疯狂的杨中仁

在看一下服务器端打印的信息

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: