1、创建项目:
1.1、创建一个聚合项目,包含三个子项目结构如下:
三个字项目都是普通maven项目:
dubbo-api 定义的接口依赖。
dubbo-provider是服务提供的项目。
dubbo-consumer是服务消费的项目。
2、定义一个接口:
2.1、在dubbo-api项目中定义一个接口ISayHelloService 如下:
3、实现服务提供provider:
3.1、先添加所需的jar包依赖
dubbo-api接口依赖
<dependency>
<groupId>com.wzy</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
依赖dubbo 我们使用最新版本2.7.8
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.7</version>
</dependency>
添加zookeeper客户端,这个包是dubbbo官方提供,有统一依赖的味道
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.7</version>
</dependency>
没错就只要三个包就搞定,比老版本集成方便了不只一点点
3.2、实现接口
public class SayHelloService implements ISayHelloService {
@Override
public String sayHello() {
return "HELLO";
}
}
3.3、服务提供者配置
1、在项目dubbo-provider的resources目录下创建一个目录META-INF/spring,因为dubbo会默认的加载这个路径的所有的 *.xml文件。
2、在META-INF/spring下创建一个Application.xml文件。
3、在Application.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://dubbo.apache.org/schema/dubbo"
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="say-provider" />
<!--注册服务到zookeeper-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.wzy.api.ISayHelloService" ref="sayHelloService"/>
<bean id="sayHelloService" class="com.wzy.service.SayHelloService"/>
</beans>
4、启动项目并发布服务到zookeeper
public class App {
public static void main(String[] args) {
//在main方法中只用dubbo提供的启动类Main来启动项目区发布服务提供者
Main.main(args);
}
}
5、服务发布成功标记
日志:
zookeeper的服务注册信息:
服务的URL信息说明,其实dubbo发布服务到zookeeper的时候,是使用服务的URL来作为提供者列表的节点名称,节点的数据为服务提供的ip地址:
dubbo://192.168.236.2:20880/\ 提供者服务的ip:port
com.wzy.api.ISayHelloService?\ 接口全路径
anyhost=true&\
application=say-provider&\ 应用名称
deprecated=false&\ 是否过时
dubbo=2.0.2&\
dynamic=true&\ 是否动态注册,如果设为false,注册
后将显示后disable状态,需人工启
用,并且服务提供者停止时,也不会
自动取消册,需人工禁用。
generic=false&\ 是否缺省泛化接口,如果为泛化接
口,将返回GenericService
interface=com.wzy.api.ISayHelloService&\ 接口名称
metadata-type=remote&\
methods=sayHello&\ 方法列表
pid=15936&\ 提供者进程id
release=2.7.8&\ dubbo框架版本
side=provider&\
timestamp=1597677147450, \
dubbo version: 2.7.8, \
current host: 192.168.1.26
4、定义服务消费consumer
4.1、在项目dubbo-provider的resources目录下创建一个目录META-INF/spring,因为dubbo会默认的加载这个路径的所有的 *.xml文件。
4.2、在META-INF/spring下创建一个Application.xml文件。
4.3、在Application.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://dubbo.apache.org/schema/dubbo"
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="say-consumer" />
<!--服务注册到zookeeper-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 声明需要暴露的服务接口 -->
<dubbo:reference interface="com.wzy.api.ISayHelloService" id="sayHelloService"/>
</beans>
4.4、服务调用通过spring应用上下文获取服务消费者进行服务调用
public class Appconsumer {
public static void main( String[] args ) {
ApplicationContext context =
new ClassPathXmlApplicationContext("META-INF/spring/Application.xml");
ISayHelloService bean = (ISayHelloService) context.getBean("sayHelloService");
System.out.println(bean.sayHello());
}
}
4.5、调用结果展示
5、以上就是基于xml的方法来进行dubbo的使用。