dubbo hello word

 dubbo  的简介网上一大堆,我也不用献丑。详细可以去http://dubbo.io/    了解,现将自己入门记录一下

1 下载 zookeeper http://mirrors.hust.edu.cn/apache/zookeeper/
解压到相应路径,到目录conf 下创建 zoo.cfg 文件,默认就是加载这个文件,文件内容可直接 从 copy zoo_sample.cfg

#zoo.cfg 的内容
#   心跳检查的时间 2秒
tickTime=2000
# 初始化时 连接到服务器端的间隔次数,总时间10*2=20秒
initLimit=10
# ZK Leader 和follower 之间通讯的次数,总时间5*2=10秒 
syncLimit=5
# 存储内存中数据库快照的位置,如果不设置参数,更新事务日志将被存储到默认位置。
dataDir=D:\\log\\zk\\tmp\\zookeeper
# 错误日志的存放位置
dataLogDirD:\\log\\zk\\logs\\zookeeper
# ZK 服务器端的监听端口
clientPort=2181

切换到bin目录 执行 zkServer.cmd 启动 zookeeper
建立测试工程

接口类
package com.xyw.demo;

/**
 * Hello world!
 *
 */
public interface IHelloWordService 
{
    String sayHello(String name);
}
实现类
package com.xyw.demo.impl;

import com.xyw.demo.IHelloWordService;

/**   
 * @author xuyw   
 * @date 2015-11-21 下午01:29:52 
 * @email xyw10000@163.com   
 * @version V1.0   
 */
public class HelloWordServiceImpl implements IHelloWordService {


    public String sayHello(String name) {

        return "hello :" +name;
    }

}

applicationProvider.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
        ">     
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app" />
    <!-- 使用zookeeper中心暴露服务地址 -->
    <dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181"  />
     <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />     
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.xyw.demo.IHelloWordService"
        ref="demoService" />  
     <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.xyw.demo.impl.HelloWordServiceImpl" />
</beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.xyw</groupId>
  <artifactId>dubbo-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>dubbo-demo</name>
  <url>http://maven.apache.org</url>

   <properties>  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    <spring.version>3.1.4.RELEASE</spring.version>  
    <slf4j.version>1.6.6</slf4j.version>  
  </properties>  

  <dependencies>  
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>3.8.1</version>  
      <scope>test</scope>  
    </dependency>  
    <!-- Spring -->  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-aop</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-asm</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-core</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-beans</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-context</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-expression</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <!-- spring end -->  
    <!-- log -->  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.16</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-api</artifactId>  
            <version>${slf4j.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-log4j12</artifactId>  
            <version>${slf4j.version}</version>  
        </dependency>  
    <!-- dubbo -->  
    <dependency>  
      <groupId>com.alibaba</groupId>  
      <artifactId>dubbo</artifactId>  
      <version>2.5.3</version>  
    </dependency>  
    <!-- zkclient  -->  
    <dependency>  
      <groupId>com.github.sgroschupf</groupId>  
      <artifactId>zkclient</artifactId>  
      <version>0.1</version>  
    </dependency>  
    <!--  zookeeper -->  
    <dependency>  
      <groupId>org.apache.zookeeper</groupId>  
      <artifactId>zookeeper</artifactId>  
      <version>3.3.6</version>  
    </dependency>  
  </dependencies>  
</project>

java 工程手动加载配置测试

package com.xyw.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**   
 * @author xuyw   
 * @date 2015-11-21 下午01:34:58 
 * @email xyw10000@163.com   
 * @version V1.0   
 */
public class StartTest {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });  
        context.start();  
        System.out.println("按任意键退出");  
        System.in.read();  
    }

}

dubbo-demo-impl 客户端调用工程
IHelloWordService 这个和服务暴漏接口一致,我偷懒直接拷贝过来 可以打成jar引用
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.xyw</groupId>
  <artifactId>dubbo-demo-impl</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>dubbo-demo-impl</name>
  <url>http://maven.apache.org</url>

     <properties>  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    <spring.version>3.1.4.RELEASE</spring.version>  
    <slf4j.version>1.6.6</slf4j.version>  
  </properties>  

  <dependencies>  
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>3.8.1</version>  
      <scope>test</scope>  
    </dependency>  
    <!-- Spring -->  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-aop</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-asm</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-core</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-beans</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-context</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-expression</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <!-- spring end -->  
    <!-- log -->  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.16</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-api</artifactId>  
            <version>${slf4j.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-log4j12</artifactId>  
            <version>${slf4j.version}</version>  
        </dependency>  
    <!-- dubbo -->  
    <dependency>  
      <groupId>com.alibaba</groupId>  
      <artifactId>dubbo</artifactId>  
      <version>2.5.3</version>  
    </dependency>  
    <!-- zkclient  -->  
    <dependency>  
      <groupId>com.github.sgroschupf</groupId>  
      <artifactId>zkclient</artifactId>  
      <version>0.1</version>  
    </dependency>  
    <!--  zookeeper -->  
    <dependency>  
      <groupId>org.apache.zookeeper</groupId>  
      <artifactId>zookeeper</artifactId>  
      <version>3.3.6</version>  
    </dependency>  
  </dependencies>  
</project>

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
        ">     
     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="hello-world-app-consumer" />
     <!-- 使用  zookeeper注册中心暴露发现服务地址 -->
    <dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181"  />
    <dubbo:protocol name="dubbo" port="20880" />     
      <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="com.xyw.demo.IHelloWordService" />
</beans>

测试调用类

package com.xyw.demo.impl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xyw.demo.IHelloWordService;

/**   
 * @author xuyw   
 * @date 2015-11-21 下午01:34:58 
 * @email xyw10000@163.com   
 * @version V1.0   
 */
public class StartTest {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });  
        context.start();  
        IHelloWordService demoServer = (IHelloWordService) context.getBean("demoService");  
        System.out.println("client:"+demoServer.sayHello("小徐"));
    }

}

启动服务工程测试类
在启动调用工程测试类

ok 搞定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值