Dubbo 是一个高性能的 Java RPC 框架,广泛应用于微服务架构中。学习 Dubbo 的基础语法和配置对于构建和管理分布式系统至关重要。以下是关于 Dubbo 的主要基础语法和关键概念。
基础概念
-
Provider(服务提供者)
- 提供远程服务的应用。
-
Consumer(服务消费者)
- 调用远程服务的应用。
-
Registry(注册中心)
- 用于服务注册与发现的中间件,如 ZooKeeper、Nacos 等。
-
Monitor(监控中心)
- 监控服务调用情况。
-
Container(容器)
- 用于启动、加载、运行服务提供者和消费者的进程,如 Spring Boot 容器。
-
Protocol(协议)
- 通信协议,如 Dubbo、HTTP、RMI 等。
-
Serialization(序列化)
- 数据传输格式,如 Hessian2、JSON 等。
-
Cluster(集群)
- 故障转移、负载均衡等机制。
主要配置文件
Dubbo 支持多种配置方式,包括 XML 配置、注解配置和 API 配置。以下是一些常见的配置示例。
XML 配置
- 服务提供者配置
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.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 应用声明 -->
<dubbo:application name="demo-provider"/>
<!-- 注册中心地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 协议声明 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.example.DemoService" ref="demoService"/>
<!-- 和本地 bean 一样实现服务 -->
<bean id="demoService" class="com.example.impl.DemoServiceImpl"/>
</beans>
2.服务消费者配置
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.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 应用声明 -->
<dubbo:application name="demo-consumer"/>
<!-- 注册中心地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 声明需要引用的服务接口 -->
<dubbo:reference id="demoService" interface="com.example.DemoService"/>
</beans>
注解配置
- 服务提供者配置
java
package com.example;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
@Service(version = "1.0.0")
@Component
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
2.服务消费者配置
java
package com.example;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
@Component
public class ConsumerComponent {
@Reference(version = "1.0.0")
private DemoService demoService;
public void test() {
System.out.println(demoService.sayHello("World"));
}
}
API 配置
- 服务提供者配置
java
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ServiceConfig;
import com.example.DemoService;
import com.example.impl.DemoServiceImpl;
public class ProviderBootstrap {
public static void main(String[] args) throws Exception {
// 当前应用配置
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为重对象,内部封装了线程池等与连接相关的资源,实例很重,必须缓存复用
ServiceConfig<DemoService> service = new ServiceConfig<>();
service.setApplication(application);
service.setRegistry(registry);
service.setProtocol(protocol);
service.setInterface(DemoService.class);
service.setRef(new DemoServiceImpl());
// 暴露及注册服务
service.export();
System.in.read(); // 按任意键退出
}
}
2.服务消费者配置
java
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import com.example.DemoService;
public class ConsumerBootstrap {
public static void main(String[] args) throws Exception {
// 当前应用配置
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.setApplication(application);
reference.setRegistry(registry);
reference.setInterface(DemoService.class);
reference.setVersion("1.0.0");
// 创建代理实例
DemoService demoService = reference.get();
// 执行远程方法
String hello = demoService.sayHello("world");
System.out.println(hello);
}
}
关键配置项
- 应用配置 (
<dubbo:application>
)
xml
<dubbo:application name="demo-provider"/>
2.注册中心配置 (<dubbo:registry>
)
xml
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
3.协议配置 (<dubbo:protocol>
)
xml
<dubbo:protocol name="dubbo" port="20880"/>
4.服务暴露配置 (<dubbo:service>
)
xml
<dubbo:service interface="com.example.DemoService" ref="demoService"/>
5.服务引用配置 (<dubbo:reference>
)
xml
<dubbo:reference id="demoService" interface="com.example.DemoService"/>
示例代码
以下是一个完整的示例,展示如何在 Spring Boot 应用中使用 Dubbo 进行服务注册与发现。
1. 添加依赖
在 pom.xml
中添加 Dubbo 和相关依赖:
xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
2. 配置 application.yml
在 src/main/resources/application.yml
中配置 Dubbo 和注册中心地址:
yaml
spring:
application:
name: example-service
dubbo:
application:
name: example-service
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 20880
3. 定义服务接口
java
package com.example.service;
public interface DemoService {
String sayHello(String name);
}
4. 实现服务接口(服务提供者)
java
package com.example.service.impl;
import com.example.service.DemoService;
import org.apache.dubbo.config.annotation.Service;
@Service(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
5. 启动类(服务提供者)
java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ExampleServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleServiceProviderApplication.class, args);
}
}
6. 引用服务接口(服务消费者)
java
package com.example.controller;
import com.example.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Reference(version = "1.0.0")
private DemoService demoService;
@GetMapping("/hello")
public String sayHello(@RequestParam String name) {
return demoService.sayHello(name);
}
}
7. 启动类(服务消费者)
java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ExampleServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleServiceConsumerApplication.class, args);
}
}
总结
通过掌握上述 Dubbo 的基本语法和配置选项,你可以有效地实现服务注册与发现、服务调用等功能,并确保分布式系统的高效性和可维护性。进一步的学习可以通过官方文档和其他资源来深入理解每个配置项的详细用法和最佳实践。
官方文档
这些资源提供了更详细的配置和高级功能介绍,帮助你更好地理解和使用 Dubbo。