Dubbo中的概念URL、Invocation、Invoker、Protocol

本文深入探讨了Dubbo框架中的核心模型,包括URL、Invocation和Invoker的定义与作用,解析了服务提供者如何暴露服务及服务消费者如何消费服务的详细过程。同时,介绍了Protocol在Invoker生命周期管理中的关键角色。

URL:定义了调用的url如协议、协议、参数等信息

public final class URL implements Serializable {

    private static final long serialVersionUID = -1985165475234910535L;

    private final String protocol;

    private final String username;

    private final String password;

    // by default, host to registry
    private final String host;

    // by default, port to registry
    private final int port;

    private final String path;

    private final Map<String, String> parameters;

    // ==== cache ====

    private volatile transient Map<String, Number> numbers;

    private volatile transient Map<String, URL> urls;

    private volatile transient String ip;

    private volatile transient String full;

    private volatile transient String identity;

    private volatile transient String parameter;

    private volatile transient String string;
}

Invocation:是会话域,它持有调用过程中的变量,比如方法名,参数等。

public interface Invocation {

    String getMethodName();

    Class<?>[] getParameterTypes();

    Object[] getArguments();

    Map<String, String> getAttachments();

    String getAttachment(String key);

    String getAttachment(String key, String defaultValue);

    Invoker<?> getInvoker();
}

Invoker:是实体域,它是 Dubbo 的核心模型,其它模型都向它靠扰,或转换成它,它代表一个可执行体,可向它发起 invoke 调用,它有可能是一个本地的实现,也可能是一个远程的实现,也可能一个集群实现。

public interface Invoker<T> extends Node {
    /**
     * @return service interface.
     */
    Class<T> getInterface();

    Result invoke(Invocation invocation) throws RpcException;
}

服务提供者暴露一个服务的详细过程

服务消费者消费一个服务的详细过程

Protocol:是服务域,它是 Invoker 暴露和引用的主功能入口,它负责 Invoker 的生命周期管理。

@SPI("dubbo")
public interface Protocol {

    int getDefaultPort();
  
    @Adaptive
    <T> Exporter<T> export(Invoker<T> invoker) throws RpcException;
    
    @Adaptive
    <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException;
  
    void destroy();
}

 

Dubbo中,router的主要作用是根据一定的规则将请求路由到合适的InvokerInvokerDubbo中的一个核心概念,表示一个可执行的服务对象。在某些场景下,我们可能需要在router中动态创建其他Invoker,以便实现更复杂的路由逻辑。 动态创建Invoker的步骤如下: 1. **获取服务接口**:首先需要获取到需要调用的服务接口。 2. **创建Invoker实例**:使用Dubbo提供的API,根据服务接口和目标地址创建Invoker实例。 3. **配置Invoker**:根据需要配置Invoker的参数,例如超时时间、重试次数等。 4. **添加Invoker到路由列表**:将创建的Invoker添加到路由列表中,以便后续的路由选择。 以下是一个简单的示例代码,展示了如何在router中动态创建Invoker: ```java import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.cluster.router.Router; import org.apache.dubbo.rpc.cluster.RouterChain; import org.apache.dubbo.rpc.cluster.RouterFactory; import org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol; public class CustomRouter implements Router { @Override public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException { List<Invoker<T>> result = new ArrayList<>(); // 动态创建Invoker Invoker<T> dynamicInvoker = DubboProtocol.getDubboProtocol().refer( (Class<T>) invocation.getInvoker().getInterface(), new URL("dubbo", "localhost", 20880, invocation.getInvoker().getInterface().getName()) ); result.add(dynamicInvoker); // 添加其他Invoker result.addAll(invokers); return result; } } public class CustomRouterFactory implements RouterFactory { @Override public Router getRouter(URL url) { return new CustomRouter(); } } ``` 在上述代码中,`CustomRouter`类实现了`Router`接口,并在`route`方法中动态创建了一个Invoker实例。`CustomRouterFactory`类实现了`RouterFactory`接口,用于创建`CustomRouter`实例。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值