Dubbo Consumer Initialization Process and Service Reference Mechanism
This article explains the Dubbo consumer initialization workflow, detailing how ReferenceConfig creates proxies, how service URLs are registered with Zookeeper, the steps to obtain Invoker objects via Protocol, and how ProxyFactory generates client-side service proxies, including code examples and asynchronous handling.
This article provides a detailed walkthrough of the Dubbo service consumer initialization and service reference process, covering configuration, URL registration, invoker creation, protocol handling, and proxy generation, with code examples and explanations of asynchronous and synchronous invocation.
Consumer Initialization Process
The consumer starts by invoking ReferenceConfig.init(), which checks and loads all configuration, then calls createProxy() to generate a service proxy. The proxy is created by calling the Protocol implementation's refer() method, which returns an Invoker instance that represents the remote service.
Key Steps
Encapsulate service reference information into a URL and register it to Zookeeper.
Listen for service provider online/offline events.
Connect to the provider and create a Netty client.
Wrap these details into a DubboInvoker and return the service proxy.
Service Reference Process
The reference process consists of three main steps: collecting configuration parameters, obtaining the service address from the registry to create an Invoker, and using ProxyFactory to generate a client-side proxy.
Step 1: Collect Configuration Parameters
methods=hello,
timestamp=1443695417847,
dubbo=2.5.3,
application=consumer-of-helloService,
side=consumer,
pid=7748,
interface=com.demo.dubbo.service.HelloServiceStep 2: Get Invoker from Registry
For a single registry, the code obtains a Protocol adaptive extension and calls refer() with the service interface and URL.
Protocol refprotocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
Invoker<T> invoker = refprotocol.refer(interfaceClass, url);The URL contains parameters such as registry=zookeeper, application=consumer-of-helloService, and method information.
Step 3: Create Proxy with ProxyFactory
ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
Object proxy = proxyFactory.getProxy(invoker);The ProxyFactory uses JDK dynamic proxies to wrap the Invoker. For the client side, the proxy forwards method calls to the InvokerInvocationHandler, which ultimately invokes the remote method via the Invoker.
Concepts Overview
Invoker : Represents an executable object. Three types exist – local (server), remote (client), and clustered (client aggregation). The remote DubboInvoker handles synchronous, asynchronous, and one‑way calls by using ExchangeClient methods such as send() and request().
Protocol : The Protocol implementation (e.g., DubboProtocol) converts the service URL into an Invoker. It is wrapped by ProtocolFilterWrapper, ProtocolListenerWrapper, and RegistryProtocol to add filtering, listening, and registry integration.
ProxyFactory : Generates the client‑side proxy. In the default JavassistProxyFactory, JDK Proxy creates a dynamic proxy that delegates to InvokerInvocationHandler. The handler translates method calls into RpcInvocation objects and forwards them to the underlying Invoker.
InvokerInvocationHandler Example
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
if (method.getDeclaringClass() == Object.class) {
return method.invoke(invoker, args);
}
// Build RpcInvocation and invoke the remote service
return invoker.invoke(new RpcInvocation(method, args)).recreate();
}These steps together enable a Dubbo consumer to discover services via Zookeeper, create remote invokers, and invoke methods transparently, supporting both synchronous and asynchronous communication.
References: Dubbo Development Book , OSChina Blog .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
