Dubbo custom protocol extension welcome to like, like, like.
Follow my public account Solomon for more highlights
Dubbo custom protocol extension
1. Expand instructions
RPC protocol extension to encapsulate remote call details.
Contract:
- When the user calls
refer()
The returnedInvoker
The object’sinvoke()
Method, the protocol needs to be executed accordingly with the URL remoteexport()
The incomingInvoker
The object’sinvoke()
Methods. - Among them,
refer()
The returnedInvoker
Implemented by protocol, which is usually required hereInvoker
To send a remote request,export()
The incomingInvoker
Implemented by the framework and passed in, the protocol doesn’t need to care.
Note:
- The protocol does not care about transparent proxy of the business interface to
Invoker
Is centered by the outer layerInvoker
Convert to a business interface. - The protocol does not have to be TCP network communication, such as file sharing, IPC interprocess communication, etc.
2. Extended interfaces
org.apache.dubbo.rpc.Protocol
org.apache.dubbo.rpc.Exporter
org.apache.dubbo.rpc.Invoker
public interface Protocol {
Rpccontext.getcontext ().setremoteAddress (); rpcContext.getContext (). <br> * 2.export () must be idempotent, that is, exposing the same URL Invoker twice is no different from exposing it once. <br> * 3. export() the Invoker passed in is implemented by the framework and passed in without the protocol being concerned. <br> * *@param<T> Type of service *@paramAn executable of the Invoker service@returnA reference to the exporter exposure service to cancel exposure *@throwsRpcException is thrown when exposing a service error, such as a port occupied */
<T> Exporter<T> export(Invoker<T> invoker) throws RpcException;
1. When the user invokes the invoke() method of the Invoker object returned by refer(), the protocol accordingly executes the invoke() method of the Invoker object passed in by the URL remote export(). The Invoker returned by <br> * 2. refer() is implemented by the protocol that normally needs to send remote requests in this Invoker. <br> * 3. If check=false is set in the URL, the connection fails and an exception cannot be thrown. <br> * *@param<T> Type of service *@paramType Service type *@paramUrl Indicates the URL of the remote service *@returnLocal proxy for the Invoker service *@throwsRpcException Throws */ when the connection to the service provider fails
<T> Invoker<T> refer(Class<T> type, URL url) throws RpcException;
}
Copy the code
3. Extend the configuration
<! If id is not specified, name will be used as id.
<dubbo:protocol id="xxx1" name="xxx" />
<! If the protocol attribute is not configured, the protocol configuration will be automatically scanned in ApplicationContext.
<dubbo:service protocol="xxx1" />
<! -- reference protocol default, used when <dubbo:service> has no prototol attribute configured -->
<dubbo:provider protocol="xxx1" />
Copy the code
4. Known extensions
org.apache.dubbo.rpc.protocol.injvm.InjvmProtocol
org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol
org.apache.dubbo.rpc.protocol.rmi.RmiProtocol
org.apache.dubbo.rpc.protocol.http.HttpProtocol
org.apache.dubbo.rpc.protocol.http.hessian.HessianProtocol
5. Extend the example
5.1 Maven Project Structure:
SRC | - the main | - Java. | | - com | - XXX - XxxProtocol Java (implementation Protocol interface) | - XxxExporter. Java (implementation Exporter interface) | - XxxInvoker. Java Realize the Invoker interface () | - resources | - meta-inf | - dubbo | - org. Apache. Dubbo.. RPC Protocol (= com plain text files, content is: XXX. XXX. XxxProtocol)Copy the code
5.2 SolomonProtocol. Java:
package com.xxx;
import org.apache.dubbo.rpc.Protocol;
public class SolomonProtocol implements Protocol {
public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
return new SolomonExporter(invoker);
}
public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
return newSolomonInvoker(type, url); }}Copy the code
5.3 SolomonExporter. Java:
package com.xxx;
import org.apache.dubbo.rpc.support.AbstractExporter;
public class SolomonExporter<T> extends AbstractExporter<T> {
public SolomonExporter(Invoker<T> invoker) throws RemotingException{
super(invoker);
// ...
}
public void unexport(a) {
super.unexport();
// ...}}Copy the code
5.4 SolomonInvoker. Java:
package com.xxx;
import org.apache.dubbo.rpc.support.AbstractInvoker;
public class SolomonInvoker<T> extends AbstractInvoker<T> {
public SolomonInvoker(Class<T> type, URL url) throws RemotingException{
super(type, url);
}
@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
// ...}}Copy the code
5.5 the metadata
Meta-inf/dubbo/org. Apache. Dubbo.. RPC Protocol:
solomon=com.xxx.SolomonProtocol
Copy the code