Service Discovery

Service Discovery

Introduction to Dubbo Rust Service Discovery

Dubbo Rust provides a client-based service discovery mechanism that relies on third-party registry components to coordinate the service discovery process. Supported registries: Nacos, Zookeeper.

The following is a basic working principle diagram of the Dubbo Rust service discovery mechanism:

service-discovery

Service discovery involves three participant roles: provider, consumer, and registry. The Dubbo provider instance registers its URL with the registry, which is responsible for aggregating the data. The Dubbo consumer reads the address list from the registry and subscribes to changes. Whenever the address list changes, the registry notifies all subscribed consumer instances with the latest list.

  • Dubbo Rust aggregates instance data at the service granularity, allowing consumers to accurately subscribe based on their consumption needs.

An example of Dubbo Rust service discovery: example

Efficient Address Push Implementation

From the perspective of the registry, it aggregates the instance addresses of the entire cluster by service name (e.g., org.apache.dubbo.sample.tri.Greeter). Each service-providing instance registers its IP:port address information (e.g., 127.0.0.1:8848) with the registry.

Configuration Methods

Dubbo Rust service discovery supports two types of registry components: Nacos and Zookeeper. Different registries can be created and bound to the Dubbo Rust framework as follows.

Configuration method: Assuming there is a service: Greeter, the corresponding service implementation is GreeterServerImpl.

Server:

  1. // Register service
  2. register_server(GreeterServerImpl {
  3. name: "greeter".to_string(),
  4. });
  5. // Create registry
  6. let zkr = ZookeeperRegistry::default();
  7. let r = RootConfig::new();
  8. let r = match r.load() {
  9. Ok(config) => config,
  10. Err(_err) => panic!("err: {:?}", _err), // response was dropped
  11. };
  12. // Start Dubbo framework
  13. let mut f = Dubbo::new()
  14. .with_config(r)
  15. // Bind the created registry to the Dubbo framework
  16. .add_registry("zookeeper", Box::new(zkr));
  17. f.start().await;

For the above process, the registry used can be changed by modifying the steps to create the registry.

Client:

  1. let mut builder = ClientBuilder::new();
  2. // Get registry address via env
  3. if let Ok(zk_servers) = env::var("ZOOKEEPER_SERVERS") {
  4. // Create registry
  5. let zkr = ZookeeperRegistry::new(&zk_servers);
  6. // Bind registry
  7. let directory = RegistryDirectory::new(Box::new(zkr));
  8. builder = builder.with_directory(Box::new(directory));
  9. } else if let Ok(nacos_url_str) = env::var("NACOS_URL") {
  10. // NACOS_URL=nacos://mse-96efa264-p.nacos-ans.mse.aliyuncs.com
  11. // Create registry
  12. let nacos_url = Url::from_url(&nacos_url_str).unwrap();
  13. let registry = NacosRegistry::new(nacos_url);
  14. // Bind registry
  15. let directory = RegistryDirectory::new(Box::new(registry));
  16. builder = builder.with_directory(Box::new(directory));
  17. } else {
  18. builder = builder.with_host("http://127.0.0.1:8888");
  19. }
  20. let mut cli = GreeterClient::new(builder);

Create Nacos registry:

  1. // Create registry instance through Url
  2. let nacos_url = Url::from_url("127.0.0.1:1221").unwrap();
  3. let registry = NacosRegistry::new(nacos_url);

Create Zookeeper registry:

  1. // Directly create Zookeeper registry
  2. let zkr = ZookeeperRegistry::new("127.0.0.1:1221");
  1. // Using the default method to create Zookeeper registry will use the value of ZOOKEEPER_SERVERS in the environment variables by default
  2. let zkr = ZookeeperRegistry::default();

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)