有关dubbo的基础、架构等介绍请参考之前博客:Dubbo背景及架构简介
1. 创建暴露服务模块(dubbo-demo-api)
本模块下没有实际的业务逻辑,主要是定义提供者和消费者公用服务接口
/**
* 需要暴露出去的服务
*
*/
public interface HelloService {
/**
* say hello
*
* @param name
* @return
*/
String sayHello(String name);
}
2. 创建服务提供者和消费者父模块管理和引入公用依赖
依赖版本管理在父工程(dubbo-demo),采用dubbo-2.7.5
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.qqxhb</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>dubbo-xml-demo</artifactId>
<packaging>pom</packaging>
<name>dubbo-xml-demo</name>
<dependencies>
<!-- 引入Dubbo依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- 使用zookeeper作为注册中心 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<!-- 使用curator作为zookeeper客户端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<!-- 引入需要暴露的服务 -->
<dependency>
<groupId>com.qqxhb</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
<modules>
<module>dubbo-xml-demo-consumer</module>
<module>dubbo-xml-demo-provider</module>
</modules>
</project>
3. 创建服务提供者
1).创建dubbo-xml-demo-provider模块
2).编写服务实现类和启动类
3).编写配置文件
1).创建dubbo-xml-demo-provider模块
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>com.qqxhb</groupId>
<artifactId>dubbo-xml-demo</artifactId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-xml-demo-provider</artifactId>
</project>
2).编写服务实现类(HelloServiceImpl)和启动类(ProviderApplication)
package com.qqxhb.xml.provider;
import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qqxhb.demo.api.HelloService;
public class HelloServiceImpl implements HelloService {
private static final Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class);
public String sayHello(String name) {
logger.info("========Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProviderApplication {
public static void main(String[] args) throws Exception {
// 使用spring的xml容器加载配置并启动
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");
context.start();
//阻塞服务,否则会直接关闭
System.in.read();
context.close();
}
}
3).编写配置文件dubbo.properties、spring/dubbo-provider.xml
dubbo.properties指定qos端口:dubbo.application.qos.port=22222
,dubbo-provider.xml配置具体的服务暴露信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 应用名称及元数据存储方案 -->
<dubbo:application metadata-type="remote" name="xml-demo-provider"/>
<!-- 元数据存储位置(2.7版本把元素数分开存储) -->
<dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
<!-- 服务注册中心地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 服务暴露的协议 -->
<dubbo:protocol name="dubbo"/>
<!-- 定义Bean -->
<bean id="helloService" class="com.qqxhb.xml.provider.HelloServiceImpl"/>
<!-- 暴露服务 -->
<dubbo:service interface="com.qqxhb.demo.api.HelloService" ref="helloService"/>
</beans>
4. 创建服务消费者
1).创建dubbo-xml-demo-consumer模块
2).编写启动类
3).编写配置文件
1).创建dubbo-xml-demo-consumer模块
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>com.qqxhb</groupId>
<artifactId>dubbo-xml-demo</artifactId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-xml-demo-consumer</artifactId>
</project>
2).编写消费者启动类(ConsumerApplication)
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qqxhb.demo.api.HelloService;
public class ConsumerApplication {
public static void main(String[] args) throws Exception {
// 加载bean的配置并启动spring容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
context.start();
// 从spring容器中获取提供者端暴露出的服务实现
HelloService helloService = context.getBean("helloService", HelloService.class);
System.out.println("======== result: " + helloService.sayHello("xml-comsumer"));
context.close();
}
}
3).编写配置文件dubbo.properties、spring/dubbo-consumer.xml
dubbo.properties指定qos端口:dubbo.application.qos.port=33333
,dubbo-consumer.xml配置具体的服务暴露信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="xml-demo-consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference id="helloService" check="false" interface="com.qqxhb.demo.api.HelloService"/>
</beans>
5. 启动测试
1).依赖zookeeper作为注册中心,因此需要优先启动zookeeper
zookeeper相关知识请参考之前博客:Zookeeper入门及单机及集群环境搭建
2). 启动 ProviderApplication 、启动ConsumerApplication
ProviderApplication 端窗口打印日志========Hello xml-comsumer, request from consumer: /192.168.25.1:4089
ConsumerApplication 端窗口打印日志======== result: Hello xml-comsumer, response from provider: 192.168.25.1:20880
源码地址:https://github.com/qqxhb/dubbo-demo
注解版请参考下一篇博客:Dubbo入门示例注解版