`

Dubbo入门实例

阅读更多

原文地址:http://www.hxstrive.com/article/605.htm

 

下面是本文将介绍实例在IDEA开发环境的项目结构,该项目使用MAVEN进行编译打包和依赖管理。如下图:

上图中,dubbo-hello程序有3个子模块,分别为 dubbo-api(定义我们的服务接口)、dubbo-provider(服务接口的具体实现,也是我们要暴露出去的服务)和dubbo-consumer(服务消费者)。下面将分别对他们进行详细讲解。

注意:该实例是基于Zookeeper搭建,将Zookeeper作为Dubbo的注册中心。(如果不知道怎样安装Zookeeper,请查看《Zookeeper Windows单机安装》)

dubbo-api
该模块是一个非常简单的maven项目,其中只有一个接口 HelloService.java。如下图:
HelloService.java代码如下:
package com.huangx.dubbo.hello;
 
/**
 * 定义SOA服务的接口
 */
public interface HelloService {
 
   /**
    * 接口方法
    * @param name
    * @return
    */
   String sayHello(String name);
 
}


dubbo-provider
该模块是一个非常简单的maven项目,该模块依赖了dubbo-api模块。该模块的接口如下图:

其中:
HelloServiceImpl 实现了HelloService接口
Provider 为启动类
dubbo.properties
dubbo-demo-provider.xml dubbo的spring配置文件
log4j.properties 日志配置文件

HelloServiceImpl.java代码
package com.huangx.dubbo.provider;
 
import com.huangx.dubbo.hello.HelloService;
import com.alibaba.dubbo.rpc.RpcContext;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * 服务实现
 */
public class HelloServiceImpl implements HelloService {
 
   public String sayHello(String name) {
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
      System.out.println("[" + sdf.format(new Date()) + "] Hello " + name
            + ", request from consumer: "
            + RpcContext.getContext().getRemoteAddress());
 
      return "Hello " + name + ", response from provider: "
            + RpcContext.getContext().getLocalAddress();
   }
 
}


Provider.java代码
package com.huangx.dubbo.provider;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
 * 加载 Spring 的配置文件,启动 Dubbo 服务提供者
 */
public class Provider {
 
   public static void main(String[] args) throws Exception {
      // 防止获取IPV6地址,这种方式只工作在debug模式下。
      // 但是,你能通过 -Djava.net.preferIPv4Stack=true。
      // 那么,debug或不是debug模式都能很好的工作。
      System.setProperty("java.net.preferIPv4Stack", "true");
 
      // 加载 Spring 配置文件,启动 Dubbo
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] {"dubbo-demo-provider.xml"});
      context.start();
 
      // 按任意键退出
      System.in.read();
      context.close();
   }
}


dubbo.properties配置
dubbo.application.qos.port=22222

dubbo-demo-provider.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
      xmlns="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         http://code.alibabatech.com/schema/dubbo
         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
   <!-- 提供应用名称,用于计算依赖关系 -->
   <dubbo:application name="demo-provider"/>
 
   <!-- 使用多播注册中心导出服务 -->
   <!--<dubbo:registry address="multicast://224.0.0.100:1234"/>-->
   <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
 
   <!-- 使用 dubbo 协议在 20880 端口导出服务 -->
   <dubbo:protocol name="dubbo" port="20880"/>
 
   <!-- 服务实现,与常规本地bean相同 -->
   <bean id="demoService" class="com.huangx.dubbo.provider.HelloServiceImpl"/>
 
   <!-- 声明要导出的服务接口 -->
   <dubbo:service interface="com.huangx.dubbo.hello.HelloService" ref="demoService"/>
</beans>


log4j.properties配置
###set log levels###
log4j.rootLogger=info, stdout
 
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n


dubbo-consumer
该模块是一个非常简单的maven项目,该模块依赖了dubbo-api模块。其中:
Consumer.java 为消费者
dubbo.properties
dubbo-demo-provider.xml dubbo的spring配置文件
log4j.properties 日志配置文件

Consumer.java代码
package com.huangx.dubbo.consumer;
 
import com.huangx.dubbo.hello.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
 * 加载 Spring 配置,启动 Dubbo 服务消费者
 */
public class Consumer {
 
   public static void main(String[] args) {
      System.setProperty("java.net.preferIPv4Stack", "true");
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "dubbo-demo-consumer.xml" });
      context.start();
 
      // 获取远程服务
      HelloService demoService = (HelloService) context.getBean("demoService");
      while (true) {
         try {
            Thread.sleep(1000);
            // 调用远程方法
            String hello = demoService.sayHello("word");
            // 获取结果
            System.out.println(hello);
         } catch (Throwable throwable) {
            throwable.printStackTrace();
         }
      }
   }
 
}


dubbo.properties配置
dubbo.application.qos.port=33333

dubbo-demo-consumer.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
      xmlns="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         http://code.alibabatech.com/schema/dubbo
         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
   <!-- 使用者的应用程序名称,用于计算应用间的依赖关系(不是匹配标准),
      不要设置与提供者相同的设置 -->
   <dubbo:application name="demo-consumer"/>
 
   <!-- 使用多播注册中心发现服务 -->
   <!--<dubbo:registry address="multicast://224.0.0.100:1234"/>-->
   <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
 
   <!-- 生成远程服务的代理,然后可以像使用本地接口一样去使用demoService服务 -->
   <dubbo:reference id="demoService" check="false"
                interface="com.huangx.dubbo.hello.HelloService"/>
</beans>

log4j.properties配置
###set log levels###
log4j.rootLogger=info, stdout
 
###output to console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics