下面是本文将介绍实例在IDEA开发环境的项目结构,该项目使用MAVEN进行编译打包和依赖管理。如下图:
上图中,dubbo-hello程序有3个子模块,分别为 dubbo-api(定义我们的服务接口)、dubbo-provider(服务接口的具体实现,也是我们要暴露出去的服务)和dubbo-consumer(服务消费者)。下面将分别对他们进行详细讲解。
注意:该实例是基于Zookeeper搭建,将Zookeeper作为Dubbo的注册中心。(如果不知道怎样安装Zookeeper,请查看《Zookeeper Windows单机安装》)
该模块是一个非常简单的maven项目,其中只有一个接口 HelloService.java。如下图:
HelloService.java代码如下:
package com.huangx.dubbo.hello; /** * 定义SOA服务的接口 */ public interface HelloService { /** * 接口方法 * @param name * @return */ String sayHello(String name); }
该模块是一个非常简单的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="https://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="https://code.alibabatech.com/schema/dubbo" xmlns="https://www.springframework.org/schema/beans" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-4.3.xsd https://code.alibabatech.com/schema/dubbo https://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
该模块是一个非常简单的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="https://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="https://code.alibabatech.com/schema/dubbo" xmlns="https://www.springframework.org/schema/beans" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-4.3.xsd https://code.alibabatech.com/schema/dubbo https://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
运行效果如下图:
provider
[23/03/19 08:24:17:017 CST] main INFO support.ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7dc5e7b4: startup date [Sat Mar 23 08:24:17 CST 2019]; root of context hierarchy [23/03/19 08:24:17:017 CST] main INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [dubbo-demo-provider.xml] [23/03/19 08:24:18:018 CST] main INFO logger.LoggerFactory: using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter [23/03/19 08:24:19:019 CST] main INFO config.AbstractConfig: [DUBBO] The service ready on spring started. service: com.huangx.dubbo.hello.HelloService, dubbo version: 2.5.3, current host: 127.0.0.1 [23/03/19 08:24:19:019 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.huangx.dubbo.hello.HelloService to local registry, dubbo version: 2.5.3, current host: 127.0.0.1 ... [23/03/19 08:24:20:020 CST] ZkClient-EventThread-16-127.0.0.1:2181 INFO zkclient.ZkEventThread: Starting ZkClient event thread. [08:25:01] Hello word, request from consumer: /192.168.238.1:25229 [08:25:03] Hello word, request from consumer: /192.168.238.1:25229 [08:25:04] Hello word, request from consumer: /192.168.238.1:25229 ...
consumer
[23/03/19 08:24:48:048 CST] main INFO support.ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7dc5e7b4: startup date [Sat Mar 23 08:24:48 CST 2019]; root of context hierarchy [23/03/19 08:24:49:049 CST] main INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [dubbo-demo-consumer.xml] [23/03/19 08:24:50:050 CST] main INFO logger.LoggerFactory: using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter [23/03/19 08:24:50:050 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Load registry store file C:\Users\Administrator\.dubbo\dubbo-registry-127.0.0.1.cache, data: {com.huangx.dubbo.hello.HelloService=empty://192.168.238.1:20880/com.huangx.dubbo.hello.HelloService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.5.3&interface=com.huangx.dubbo.hello.HelloService&methods=sayHello&pid=12276&side=provider×tamp=1553300659717}, dubbo version: 2.5.3, current host: 127.0.0.1 [23/03/19 08:24:51:051 CST] ZkClient-EventThread-14-127.0.0.1:2181 INFO zkclient.ZkEventThread: Starting ZkClient event thread. [23/03/19 08:25:00:000 CST] main INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.3.3-1073969, built on 02/23/2011 22:27 GMT [23/03/19 08:25:00:000 CST] main INFO zookeeper.ZooKeeper: Client environment:host.name=ZQCBUIB1T9KD4J6 [23/03/19 08:25:00:000 CST] main INFO zookeeper.ZooKeeper: Client environment:java.version=1.8.0_45 ... Hello word, response from provider: 192.168.238.1:20880 Hello word, response from provider: 192.168.238.1:20880
提示:
在后续的Dubbo文章中均会使用到本文章附件的Dubbo基础环境。