Service Reference Configuration Object Cache

Caching ReferenceConfig in Dubbo

Function Description

ReferenceConfig instances are heavy because they encapsulate the connections to the registry and the providers, necessitating caching. Otherwise, repeatedly generating ReferenceConfig can lead to performance issues and memory and connection leaks. This problem is often overlooked in API-oriented programming.

Therefore, starting from version 2.4.0, Dubbo provides a simple utility class ReferenceConfigCache for caching ReferenceConfig instances.

Usage Scenarios

In scenarios such as gateways where subscriptions are dynamically created, the ReferenceConfig itself is heavy and creates many intermediate objects, while the proxy can be reused. Hence, ReferenceConfigCache can cache these properties.

Usage Method

Eviction and Destruction

Evict ReferenceConfig from the Cache, which will destroy the ReferenceConfig and release corresponding resources.

  1. ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>();
  2. reference.setInterface(XxxService.class);
  3. reference.setVersion("1.0.0");
  4. ......
  5. ReferenceConfigCache cache = ReferenceConfigCache.getCache();
  6. // The cache.get method will cache the Reference object and invoke ReferenceConfig.get to start ReferenceConfig
  7. XxxService xxxService = cache.get(reference);
  8. // Note! The cache will hold the ReferenceConfig, do not call the destroy method of ReferenceConfig externally, as this will invalidate the ReferenceConfig in the cache!
  9. // Use xxxService object
  10. xxxService.sayHello();
  1. ReferenceConfigCache cache = ReferenceConfigCache.getCache();
  2. cache.destroy(reference);

By default, ReferenceConfigCache treats ReferenceConfig with the same service Group, interface, and version as identical, caching only one. The service Group, interface, and version serve as the cache key.

Modification Strategy

This strategy can be modified by passing a KeyGenerator when calling ReferenceConfigCache.getCache. Refer to the methods in ReferenceConfigCache class for more details.

  1. KeyGenerator keyGenerator = new ...
  2. ReferenceConfigCache cache = ReferenceConfigCache.getCache(keyGenerator);

Feedback

Was this page helpful?

Yes No

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