新建一个子模块项目名称为Consumer

引入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.35.Final</version>
</dependency>

新建dubbo-demo-consumer.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!--消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="demo-consumer"/>

<!--使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://localhost:2181"/>

<!--生成远程服务代理,可以和本地bean一样使用demoProviderService check属性,启动的时候是否检查 一般设置为false 启动的时候不检查-->
<dubbo:reference id="demoProviderService" check="false" interface="service.DemoProviderService"/>
</beans>

Java代码

DemoProviderService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package service;

/**
* 服务提供者接口
*
* @author LeDao
* @company
* @create 2021-08-10 2:02
*/
public interface DemoProviderService {

/**
* 测试接口
*
* @param name
* @return
*/
String sayHello(String name);
}

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.DemoProviderService;

import java.io.IOException;

/**
* @author LeDao
* @company
* @create 2021-08-10 2:03
*/
public class Test {

public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-demo-consumer.xml");
context.start();
DemoProviderService demoProviderService = (DemoProviderService) context.getBean("demoProviderService");
String result = demoProviderService.sayHello("小米");
System.out.println("远程调用的结果:" + result);
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
context.close();
}
}

运行结果

Consumer子模块项目没有服务提供者接口的实现类,但是调用了Provider子模块项目的服务提供者接口的实现类,从而实现消费Dubbo服务