Dubbo 3.x 强烈推荐使用注解方式,但 XML 配置方式依然被广泛支持和理解。
一、XML 配置方式中的标签
这是在传统 Spring 应用中集成 Dubbo 服务最经典的方式。你需要在 applicationContext.xml
或 dubbo-provider.xml
/ dubbo-consumer.xml
等配置文件中使用这些标签。
首先,必须引入 Dubbo 的 XML 命名空间(Namespace):
<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" <!-- 关键:dubbo命名空间 -->
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo <!-- 关键:dubbo的schema -->
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
常用核心标签:
-
**
<dubbo:application/>
**-
作用:配置当前 Dubbo 应用的基本信息。
-
必加:是。
-
示例:
<dubbo:application name="your-app-name"/>
-
-
**
<dubbo:registry/>
**-
作用:配置注册中心地址。Dubbo 服务会注册到这里,消费者从这里发现服务。
-
必加 :是(除非使用直连方式,用
url
属性)。 -
示例(使用 Nacos):
<dubbo:registry address="nacos://127.0.0.1:8848"/>
-
示例(使用 Zookeeper):
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
-
-
**
<dubbo:protocol/>
**-
作用:配置服务提供者暴露服务所使用的协议和端口。
-
位置 :通常只在提供者端配置。
-
示例(使用默认的 dubbo 协议):
<dubbo:protocol name="dubbo" port="20880"/>
-
-
**服务提供者标签 -
<dubbo:service/>
**-
作用:用于暴露一个服务,声明它是一个 Dubbo 服务。
-
位置 :提供者端。
-
必加:提供者端暴露服务时必须加。
-
示例:
<!-- interface 是服务的全限定接口名 --> <dubbo:service interface="com.example.service.UserService" ref="userService"/> <bean id="userService" class="com.example.service.UserServiceImpl"/>
-
-
**服务消费者标签 -
<dubbo:reference/>
**-
作用:用于引用一个远程的 Dubbo 服务,生成一个代理对象。
-
位置 :消费者端。
-
必加:消费者端调用远程服务时必须加。
-
示例:
<!-- id 是生成的Bean的ID,用于注入 --> <dubbo:reference id="userService" interface="com.example.service.UserService"/>
然后在代码中可以通过
@Autowired
注入这个userService
。
-
一个完整的 XML 配置示例
服务提供者配置文件 (provider.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<!-- 应用名 -->
<dubbo:application name="hello-world-app-provider"/>
<!-- 注册中心 -->
<dubbo:registry address="nacos://127.0.0.1:8848"/>
<!-- 通信协议 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 要暴露的服务接口实现 -->
<bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>
<!-- 声明要暴露的服务 -->
<dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>
</beans>
服务消费者配置文件 (consumer.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<!-- 应用名 -->
<dubbo:application name="hello-world-app-consumer"/>
<!-- 注册中心 -->
<dubbo:registry address="nacos://127.0.0.1:8848"/>
<!-- 声明需要引用的远程服务 -->
<dubbo:reference id="demoService" interface="org.apache.dubbo.samples.basic.api.DemoService"/>
</beans>
二、注解方式(现代推荐方式)
Dubbo 3.x 开始,官方强烈推荐使用基于注解的编程模型,更加简洁和现代。
核心注解:
-
**
@DubboService
**-
作用 :标记在服务实现类 上,替代 XML 中的
<dubbo:service/>
,用于暴露服务。 -
位置 :提供者端的服务实现类上。
-
示例:
// 提供者端 @DubboService public class UserServiceImpl implements UserService { // ... 业务实现 }
-
-
**
@DubboReference
**-
作用 :标记在消费者 端的字段或 Setter 方法上,替代 XML 中的
<dubbo:reference/>
,用于注入远程服务代理。 -
位置 :消费者端的成员变量上。
-
示例:
// 消费者端 @RestController public class UserController { // 注入一个远程的UserService @DubboReference private UserService userService; @GetMapping("/user") public User getUser() { return userService.getUserById(1L); } }
-
-
**
@EnableDubbo
**-
作用 :需要加在 Spring Boot 应用的主启动类上,用于开启 Dubbo 的自动配置和注解扫描。
-
位置:主启动类。
-
示例:
@SpringBootApplication @EnableDubbo // 开启Dubbo public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
-
配置(替代 XML 中的 <dubbo:application/>
, <dubbo:registry/>
)
在注解模式下,这些全局配置不再使用标签,而是写在 application.yml
或 application.properties
文件中。
application.yml 示例
# Dubbo 配置
dubbo:
application:
name: your-app-name # 对应 <dubbo:application name="..."/>
protocol:
name: dubbo
port: 20880 # 对应 <dubbo:protocol port="..."/>
registry:
address: nacos://127.0.0.1:8848 # 对应 <dubbo:registry address="..."/>
# 其他配置...
cloud:
metadata-service-protocol: dubbo # 推荐用于Dubbo3服务发现
总结与选择
功能 | XML 配置方式 | 注解方式(推荐) |
---|---|---|
应用配置 | <dubbo:application/> |
dubbo.application.name in YAML |
注册中心 | <dubbo:registry/> |
dubbo.registry.address in YAML |
协议配置 | <dubbo:protocol/> |
dubbo.protocol in YAML |
暴露服务 | <dubbo:service/> |
**@DubboService ** |
引用服务 | <dubbo:reference/> |
**@DubboReference ** |
启动开关 | 自动加载XML | **@EnableDubbo ** |
给你的建议:
-
如果是新项目 ,强烈建议直接使用 注解方式 (
@DubboService
+@DubboReference
+application.yml
),这是目前最主流和简洁的方式。 -
如果是维护老项目,可能会遇到 XML 配置方式,了解这些标签的含义至关重要。
希望这个详细的解释能帮助你!