抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

摘要:无。

环境

Windows 10 企业版 LTSC 21H2
Java 1.8
Maven 3.6.3
Spring 5.3.31
Spring Boot 2.7.18
Dubbo 3.3.2

1 注解配置

注解配置是最推荐的方式,特别是与Spring Boot集成时最为方便。通过注解可以直接在代码中完成服务的暴露和引用,减少配置文件的编写。

1.1 使用注解

核心注解:

  • @DubboService:标注在服务实现类上,替代XML中的<dubbo:service>。
  • @DubboReference:标注在字段或方法上,替代XML中的<dubbo:reference>。
  • @EnableDubbo:标注在启动类上,启用Dubbo注解支持,扫描上述两个注解,自动完成服务的注册和引用。

1.2 项目配置

1.2.1 提供者项目

创建application.yml文件:

application.yml
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
# 服务端口
server:
port: 8081

# 应用名称
spring:
application:
name: dubbo-provider

dubbo:
# 应用名称
application:
name: dubbo-provider
qos-enable: false
# 注册中心地址
registry:
id: demo-center
address: zookeeper://127.0.0.1:2181
# 协议配置
protocol:
name: dubbo
port: 20880
# 包扫描路径
scan:
base-packages: com.example

1.2.2 消费者项目

创建application.yml文件:

application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 服务端口
server:
port: 8082

# 应用名称
spring:
application:
name: dubbo-consumer

dubbo:
# 应用名称
application:
name: dubbo-consumer
qos-enable: false
# 注册中心地址
registry:
id: demo-center
address: zookeeper://127.0.0.1:2181
# 包扫描路径
scan:
base-packages: com.example

1.3 服务管理

1.3.1 提供者项目

使用@DubboService注解:

java
1
2
3
4
5
6
7
@DubboService(registry = "demo-center", timeout = 5000)
public class DemoServiceImpl implements DemoService {
@Override
public String demo(String name) {
return name;
}
}

1.3.2 消费者项目

使用@DubboReference注解:

java
1
2
3
4
5
6
7
8
9
10
@RestController
public class DemoController {
@DubboReference
private DemoService demoService;

@GetMapping("/demo")
public String demo(@RequestParam String name) {
return demoService.demo(name);
}
}

2 XML配置

XML配置是Dubbo最早支持的配置方式,通过Spring的XML命名空间扩展来实现。这种方式配置直观、功能完整,适合传统的Spring项目。

2.1 命名空间

在Spring的XML配置文件中引入Dubbo的命名空间:

pom.xml
1
2
3
4
5
6
7
8
9
10
<?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.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
</beans>

2.2 项目配置

2.2.1 提供者项目

创建spring-context.xml文件:

spring-context.xml
1
2
3
4
<!-- 扫描组件 -->
<context:component-scan base-package="com.example"/>
<!-- 引入提供者配置 -->
<import resource="dubbo-provider.xml"/>

创建dubbo-provider.xml文件:

dubbo-provider.xml
1
2
3
4
5
6
7
8
<!-- 应用名称 -->
<dubbo:application name="dubbo-provider" qos-enable="false"/>
<!-- 注册中心 -->
<dubbo:registry id="demo-center" address="zookeeper://127.0.0.1:2181"/>
<!-- 协议端口 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 暴露服务 -->
<dubbo:service ref="demoService" interface="com.example.service.DemoService" registry="demo-center" timeout="5000"/>

2.2.2 消费者项目

创建spring-context.xml文件:

spring-context.xml
1
2
3
4
<!-- 扫描组件 -->
<context:component-scan base-package="com.example"/>
<!-- 引入消费者配置 -->
<import resource="dubbo-consumer.xml"/>

创建dubbo-consumer.xml文件:

dubbo-consumer.xml
1
2
3
4
5
6
<!-- 应用名称 -->
<dubbo:application name="dubbo-consumer" qos-enable="false"/>
<!-- 注册中心 -->
<dubbo:registry id="demo-center" address="zookeeper://127.0.0.1:2181"/>
<!-- 引用服务 -->
<dubbo:reference id="demoService" interface="com.example.service.DemoService" registry="demo-center" timeout="5000"/>

2.3 服务管理

2.3.1 提供者项目

实现服务:

java
1
2
3
4
5
6
7
@Service("demoService")
public class DemoServiceImpl implements DemoService {
@Override
public String demo(String name) {
return name;
}
}

2.3.2 消费者项目

使用服务:

java
1
2
3
4
5
6
7
8
9
10
@RestController
public class DemoController {
@Autowired
private DemoService demoService;

@GetMapping("/demo")
public String demo(@RequestParam String name) {
return demoService.demo(name);
}
}

3 API配置

API配置是Dubbo最底层的配置方式,通过Java代码直接创建Config对象来完成配置。XML配置和注解配置在底层都是转化为API调用来执行的。这种方式最灵活,适合需要动态配置的场景。

3.1 基本使用

3.1.1 提供者项目

通过API暴露服务:

java
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
public class ProviderApplication {
public static void main(String[] args) {
// 应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("demo-provider");

// 注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://127.0.0.1:2181");

// 协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(20880);

// 服务配置
ServiceConfig<DemoService> service = new ServiceConfig<>();
service.setInterface(DemoService.class);
service.setRef(new DemoServiceImpl());

// 统一启动
DubboBootstrap.getInstance()
.application(application)
.registry(registry)
.protocol(protocol)
.service(service)
.start()
.await();
}
}

3.1.2 消费者项目

通过API引用服务:

java
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
public class ConsumerApplication {
public static void main(String[] args) {
// 应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("demo-consumer");

// 注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://127.0.0.1:2181");

// 引用配置
ReferenceConfig<DemoService> reference = new ReferenceConfig<>();
reference.setInterface(DemoService.class);

// 统一启动并,引用服务
DubboBootstrap.getInstance()
.application(application)
.registry(registry)
.reference(reference)
.start();

// 获取代理对象
DemoService service = reference.get();

// 调用服务
String result = service.demo("测试");
System.out.println(result);
}
}

3.2 动态配置

3.2.1 提供者项目

动态暴露服务:

java
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
public class ProviderApplication {
private static final ApplicationConfig application = new ApplicationConfig("demo-provider");
private static final RegistryConfig registry = new RegistryConfig("zookeeper://127.0.0.1:2181");
private static final ProtocolConfig protocol = new ProtocolConfig("dubbo", 20880);

public static void main(String[] args) {
initBootstrap();
dynamicExport(DemoService.class, new DemoServiceImpl());
DubboBootstrap.getInstance().start().await();
}

private static volatile boolean initialized = false;

private static synchronized void initBootstrap() {
if (initialized) {
return;
}
DubboBootstrap.getInstance()
.application(application)
.registry(registry)
.protocol(protocol);
initialized = true;
}

public static <T> void dynamicExport(Class<T> interfaceClass, T impl) {
ServiceConfig<T> service = new ServiceConfig<>();
service.setInterface(interfaceClass);
service.setRef(impl);
DubboBootstrap.getInstance().service(service);
}
}

3.2.2 消费者项目

动态引用服务:

java
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
public class ConsumerApplication {
private static final ApplicationConfig application = new ApplicationConfig("demo-consumer");
private static final RegistryConfig registry = new RegistryConfig("zookeeper://127.0.0.1:2181");

public static void main(String[] args) {
initBootstrap();
DemoService service = dynamicReference(DemoService.class);
String result = service.demo("测试");
System.out.println(result);
}

private static volatile boolean initialized = false;

private static synchronized void initBootstrap() {
if (initialized) {
return;
}
DubboBootstrap.getInstance()
.application(application)
.registry(registry);
initialized = true;
}

public static <T> T dynamicReference(Class<T> interfaceClass) {
ReferenceConfig<T> reference = new ReferenceConfig<>();
reference.setInterface(interfaceClass);
DubboBootstrap.getInstance().reference(reference);
return reference.get();
}
}

评论