第五章:注册中心¶
注册中心概述¶
支持的注册中心¶
| 注册中心 | 说明 | 适用场景 |
|---|---|---|
| ZooKeeper | 分布式协调服务 | 生产环境 |
| Nacos | 阿里开源注册中心 | 生产环境 |
| Redis | 缓存数据库 | 测试环境 |
| Multicast | 组播 | 测试环境 |
| Simple | 简单注册中心 | 测试环境 |
ZooKeeper 注册中心¶
配置¶
# application.yml
dubbo:
registry:
address: zookeeper://localhost:2181
# 或者集群
# address: zookeeper://node1:2181,node2:2181,node3:2181
timeout: 10000
session: 60000
依赖¶
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>3.2.0</version>
</dependency>
工作原理¶
┌─────────────────────────────────────────────────────────────┐
│ ZooKeeper 注册中心 │
├─────────────────────────────────────────────────────────────┤
│ │
│ /dubbo │
│ ├── com.example.UserService │
│ │ ├── providers │
│ │ │ ├── URL1 (provider 地址) │
│ │ │ └── URL2 (provider 地址) │
│ │ ├── consumers │
│ │ │ └── URL1 (consumer 地址) │
│ │ ├── routers │
│ │ │ └── router 配置 │
│ │ └── configurators │
│ │ └── 动态配置 │
│ │ │
│ └── ... │
│ │
└─────────────────────────────────────────────────────────────┘
Nacos 注册中心¶
配置¶
# application.yml
dubbo:
registry:
address: nacos://localhost:8848
parameters:
namespace: public
group: DEFAULT_GROUP
依赖¶
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>3.2.0</version>
</dependency>
Nacos 控制台¶
访问 Nacos 控制台:http://localhost:8848/nacos
服务列表:
- user-service-provider
- user-service-consumer
服务详情:
- 实例列表
- 健康状态
- 元数据
Redis 注册中心¶
配置¶
# application.yml
dubbo:
registry:
address: redis://localhost:6379
username: default
password: password
依赖¶
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-redis</artifactId>
<version>3.2.0</version>
</dependency>
多注册中心¶
配置¶
# application.yml
dubbo:
registries:
registry1:
address: zookeeper://localhost:2181
registry2:
address: nacos://localhost:8848
使用¶
// 注册到多个注册中心
@DubboService(registry = {"registry1", "registry2"})
public class UserServiceImpl implements UserService {
// ...
}
// 从多个注册中心订阅
@DubboReference(registry = {"registry1", "registry2"})
private UserService userService;
服务注册¶
注册流程¶
注册 URL¶
dubbo://192.168.1.10:20880/com.example.UserService?
version=1.0.0&
group=user-group&
timeout=5000&
retries=2&
loadbalance=roundrobin&
timestamp=1234567890
动态注册¶
import org.apache.dubbo.config.RegistryConfig;
// 动态注册
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://localhost:2181");
ServiceConfig<UserService> service = new ServiceConfig<>();
service.setRegistry(registry);
service.export();
服务发现¶
订阅流程¶
服务变更通知¶
import org.apache.dubbo.common.URL;
import org.apache.dubbo.registry.NotifyListener;
// 订阅服务变更
registry.subscribe(url, new NotifyListener() {
@Override
public void notify(List<URL> urls) {
// 处理服务变更
System.out.println("服务变更: " + urls);
}
});
注册中心管理¶
查看注册服务¶
# ZooKeeper
zkCli.sh
ls /dubbo/com.example.UserService/providers
# Nacos
curl http://localhost:8848/nacos/v1/ns/instance/list?serviceName=user-service
下线服务¶
# ZooKeeper
delete /dubbo/com.example.UserService/providers/URL
# Nacos
curl -X DELETE "http://localhost:8848/nacos/v1/ns/instance?serviceName=user-service&ip=192.168.1.10&port=20880"
小结¶
注册中心要点:
- 支持的注册中心:ZooKeeper、Nacos、Redis
- ZooKeeper:配置、工作原理
- Nacos:配置、控制台
- 多注册中心:配置、使用
- 服务注册:注册流程、注册 URL
- 服务发现:订阅流程、变更通知
下一章我们将学习负载均衡。