Apache Dubbo is a powerful RPC framework for building enterprise-grade microservices with service discovery, load balancing, and fault tolerance.
—
The RPC core system provides the foundation abstractions for remote procedure calls in Apache Dubbo, including service invocation, proxy creation, filtering, and context management.
The Invoker interface represents a callable service endpoint and provides the core abstraction for RPC invocation.
/**
* Service invoker representing a callable service endpoint
* @param <T> Service interface type
*/
public interface Invoker<T> {
/**
* Invoke a service method
* @param invocation RPC invocation context
* @return Result of the invocation
* @throws RpcException if invocation fails
*/
Result invoke(Invocation invocation) throws RpcException;
/** Get the service interface class */
Class<T> getInterface();
/** Get the service URL configuration */
URL getUrl();
/** Check if the invoker is available for invocation */
boolean isAvailable();
/** Destroy the invoker and release resources */
void destroy();
}Usage Examples:
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcInvocation;
// Create invocation
RpcInvocation invocation = new RpcInvocation();
invocation.setMethodName("sayHello");
invocation.setParameterTypes(new Class<?>[]{String.class});
invocation.setArguments(new Object[]{"World"});
// Invoke service
Result result = invoker.invoke(invocation);
String response = (String) result.getValue();Represents an RPC invocation with method information, parameters, and attachments.
/**
* RPC invocation context containing method information and parameters
*/
public interface Invocation {
/** Get target service unique name */
String getTargetServiceUniqueName();
/** Get method name being invoked */
String getMethodName();
/** Get service name */
String getServiceName();
/** Get parameter types of the method */
Class<?>[] getParameterTypes();
/** Get method arguments */
Object[] getArguments();
/** Get all attachments */
Map<String, String> getAttachments();
/** Get attachment value by key */
String getAttachment(String key);
/** Get attachment value with default */
String getAttachment(String key, String defaultValue);
}
/**
* Concrete RPC invocation implementation
*/
public class RpcInvocation implements Invocation {
public RpcInvocation();
public RpcInvocation(String methodName, Class<?>[] parameterTypes, Object[] arguments);
public void setMethodName(String methodName);
public void setParameterTypes(Class<?>[] parameterTypes);
public void setArguments(Object[] arguments);
public void setAttachment(String key, String value);
public void setAttachments(Map<String, String> attachments);
}Container for RPC invocation results and exceptions.
/**
* RPC invocation result container
*/
public interface Result {
/** Get the return value */
Object getValue();
/** Set the return value */
void setValue(Object value);
/** Get exception if invocation failed */
Throwable getException();
/** Set exception for failed invocation */
void setException(Throwable t);
/** Check if result contains exception */
boolean hasException();
/** Get result attachments */
Map<String, String> getAttachments();
/** Get attachment by key */
String getAttachment(String key);
/** Add attachment to result */
void setAttachment(String key, String value);
}
/**
* Asynchronous RPC result
*/
public class AsyncRpcResult implements Result {
public AsyncRpcResult(CompletableFuture<Object> future);
public CompletableFuture<Object> getFuture();
public boolean isDone();
}Thread-local context for RPC invocations containing attachments and metadata.
/**
* Thread-local RPC context for storing invocation metadata
*/
public class RpcContext {
/** Get current thread's RPC context */
public static RpcContext getContext();
/** Get server-side context */
public static RpcContext getServerContext();
/** Check if current side is provider */
public boolean isProviderSide();
/** Check if current side is consumer */
public boolean isConsumerSide();
/** Set attachment value */
public RpcContext setAttachment(String key, String value);
/** Get attachment value */
public String getAttachment(String key);
/** Remove attachment */
public String removeAttachment(String key);
/** Get all attachments */
public Map<String, String> getAttachments();
/** Set request attachments */
public void setAttachments(Map<String, String> attachments);
/** Get remote address */
public InetSocketAddress getRemoteAddress();
/** Get local address */
public InetSocketAddress getLocalAddress();
/** Get remote application name */
public String getRemoteApplicationName();
/** Get local application name */
public String getLocalApplicationName();
}Usage Examples:
import org.apache.dubbo.rpc.RpcContext;
// Set attachment on consumer side
RpcContext.getContext().setAttachment("userId", "12345");
String result = greeterService.sayHello("World");
// Get attachment on provider side
@DubboService
public class GreeterServiceImpl implements GreeterService {
public String sayHello(String name) {
String userId = RpcContext.getContext().getAttachment("userId");
return "Hello " + name + ", user: " + userId;
}
}Interface for generic service invocation without compiled stubs.
/**
* Generic service interface for runtime service invocation
*/
public interface GenericService {
/**
* Generic synchronous invocation
* @param method Method name to invoke
* @param parameterTypes Parameter type names
* @param args Method arguments
* @return Method result
* @throws GenericException if invocation fails
*/
Object $invoke(String method, String[] parameterTypes, Object[] args)
throws GenericException;
/**
* Generic asynchronous invocation
* @param method Method name to invoke
* @param parameterTypes Parameter type names
* @param args Method arguments
* @return CompletableFuture containing the result
*/
CompletableFuture<Object> $invokeAsync(String method, String[] parameterTypes, Object[] args);
}
/**
* Exception for generic service invocations
*/
public class GenericException extends RuntimeException {
public GenericException(String message);
public GenericException(String message, Throwable cause);
public GenericException(Throwable cause);
}Usage Examples:
import org.apache.dubbo.rpc.service.GenericService;
// Configure generic reference
ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
reference.setInterface("com.example.GreeterService");
reference.setGeneric(true);
GenericService genericService = reference.get();
// Generic invocation
Object result = genericService.$invoke(
"sayHello",
new String[]{"java.lang.String"},
new Object[]{"World"}
);Interface for creating service proxies and invokers.
/**
* Factory for creating service proxies and invokers
*/
@SPI("javassist")
public interface ProxyFactory {
/**
* Create proxy for consumer side
* @param invoker Service invoker
* @return Service proxy
* @throws RpcException if proxy creation fails
*/
<T> T getProxy(Invoker<T> invoker) throws RpcException;
/**
* Create proxy with specific class loader
* @param invoker Service invoker
* @param classLoader Class loader to use
* @return Service proxy
*/
<T> T getProxy(Invoker<T> invoker, Class<?>... interfaces) throws RpcException;
/**
* Create invoker for provider side
* @param proxy Service implementation
* @param type Service interface
* @param url Service URL
* @return Service invoker
*/
<T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) throws RpcException;
}Interceptor pattern for request/response processing.
/**
* RPC invocation filter for intercepting requests and responses
*/
@SPI
public interface Filter {
/**
* Filter RPC invocation
* @param invoker Next invoker in chain
* @param invocation Current invocation
* @return Invocation result
* @throws RpcException if filtering fails
*/
Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException;
/**
* Callback for successful response (optional)
* @param appResponse Application response
* @param invoker Service invoker
* @param invocation Original invocation
* @return Modified response
*/
default Result onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
return appResponse;
}
/**
* Callback for exception handling (optional)
* @param exception Thrown exception
* @param invoker Service invoker
* @param invocation Original invocation
*/
default void onError(Throwable exception, Invoker<?> invoker, Invocation invocation) {
// Default implementation does nothing
}
}
/**
* Base filter implementation with common functionality
*/
public abstract class BaseFilter implements Filter {
protected boolean needToPrintLog(Invoker<?> invoker, Invocation invocation) {
return true;
}
}Usage Examples:
// Custom logging filter
public class LoggingFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
long start = System.currentTimeMillis();
try {
Result result = invoker.invoke(invocation);
long elapsed = System.currentTimeMillis() - start;
System.out.println("Method " + invocation.getMethodName() +
" took " + elapsed + "ms");
return result;
} catch (Exception e) {
System.err.println("Method " + invocation.getMethodName() + " failed: " + e.getMessage());
throw e;
}
}
}SPI interface for different transport protocols.
/**
* Protocol interface for different transport implementations
*/
@SPI("dubbo")
public interface Protocol {
/** Get default port for this protocol */
int getDefaultPort();
/**
* Export service for provider side
* @param invoker Service invoker to export
* @return Service exporter for lifecycle management
* @throws RpcException if export fails
*/
<T> Exporter<T> export(Invoker<T> invoker) throws RpcException;
/**
* Create service reference for consumer side
* @param type Service interface class
* @param url Service URL
* @return Service invoker
* @throws RpcException if reference creation fails
*/
<T> Invoker<T> refer(Class<T> type, URL url) throws RpcException;
/** Destroy protocol and release resources */
void destroy();
}
/**
* Service exporter for managing exported services
*/
public interface Exporter<T> {
/** Get the exported service invoker */
Invoker<T> getInvoker();
/** Unexport the service */
void unexport();
}Comprehensive exception handling with error codes.
/**
* RPC-specific exception with error codes
*/
public class RpcException extends RuntimeException {
// Exception types
public static final int UNKNOWN_EXCEPTION = 0;
public static final int NETWORK_EXCEPTION = 1;
public static final int TIMEOUT_EXCEPTION = 2;
public static final int BIZ_EXCEPTION = 3;
public static final int FORBIDDEN_EXCEPTION = 4;
public static final int SERIALIZATION_EXCEPTION = 5;
public static final int NO_INVOKER_AVAILABLE_AFTER_FILTER = 6;
public static final int LIMIT_EXCEEDED_EXCEPTION = 7;
public RpcException();
public RpcException(String message);
public RpcException(String message, Throwable cause);
public RpcException(int code);
public RpcException(int code, String message);
public RpcException(int code, String message, Throwable cause);
/** Get exception code */
public int getCode();
/** Set exception code */
public void setCode(int code);
/** Check if exception is of specific type */
public boolean isBiz();
public boolean isTimeout();
public boolean isNetwork();
public boolean isSerialization();
public boolean isForbidden();
}Built-in service for connectivity testing and health checks.
/**
* Built-in echo service for testing connectivity
*/
public interface EchoService {
/**
* Echo the input message back
* @param message Message to echo
* @return Same message
*/
Object $echo(Object message);
}Usage Examples:
// Test service connectivity
EchoService echoService = (EchoService) greeterService;
Object result = echoService.$echo("ping");
// result should be "ping" if service is availableInstall with Tessl CLI
npx tessl i tessl/maven-org-apache-dubbo--dubbo