美文网首页
Dubbo - Provider服务提供者

Dubbo - Provider服务提供者

作者: 沐兮_d64c | 来源:发表于2019-05-26 15:31 被阅读0次

1,Provider服务提供者

1)ServiceBean暴露服务,dubbo解析<dubbo:service>节点,针对每个节点,创建单例的ServiceBean。
xml文件<dubbo:service interface="com.hzq.IDemoService" ref="demoService">
2)ServiceBean的afterPropertiesSet 中export() -> ServiceConfig.export() -> ServiceConfig.doExport() -> doExportUrls->doExportUrlsFor1Protocol ->exportLocal(URL url) 将URL构造成Exporter,加入List<Exporter<?>>列表中。
3)生成Invoker对象,放入AbstractProtocol的exportMap中。

image.png
创建invoker并启动NettyServer,此时服务可以被请求。
image.png
4)注册服务provider的信息注册到registry,并且订阅configurators。
RegistryProtocol#export开始,最终调用ZookeeperRegistry#doRegister
image.png
image.png

2,Dubbo provider注册到zookeeper

1)暴露服务的两种协议
不使用注册中心时,直接调用对应协议(如 Dubbo 协议)的 export() 暴露服务。(DubboProtocol)
如果使用了注册中心,则在通过具体协议(如 Dubbo 协议)暴露服务之后(即在 2.8 基础之上)进入服务注册流程,将服务节点注册到注册中心如zookeeper。(RegistryProtocol)
2)注册两个provider register到zookeeper,两个consumer subscribe到zookeeper。

image.png
image.png

3,Provider端简单配置和使用

1)provider-first.xml 配置

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="demo-provider-first"/>

    <!-- 集群配置<dubbo:registry address="zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181" /> -->
    <!-- 单节点配置 -->
    <dubbo:registry id="hzqDubboV1" protocol="zookeeper" address="127.0.0.1:2181" />

    <!-- 使用dubbo协议,暴露服务在20880端口 use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 服务实现的bean service implementation, as same as regular local bean -->
    <bean id="demoService" class="com.hzq.DemoService"/>

    <!-- 需要暴露的api declare the service interface to be exported -->
    <dubbo:service interface="com.hzq.IDemoService" ref="demoService"/>
</beans>

2)注册服务到zookeeper。


image.png

4,调用本地服务过程

1)DubbleProtocol将生成的invoker对象,放入exporterMap中。
key为com.hzq.IDemoService:20880,value为通过invoker封装的DubboExporter对象。
2)Provider端启动了NettyServier通过创建HeaderExchangeServer

image.png
image.png
image.png
image.png
3)DubboProtocol的Map<String, ExchangeServer> serverMap中ExchangeServer持有一个Server对象(可以是NettyServer)。
image.png
4)DubboProtocol的ExchangeHandler的received方法。
根据Invocation获取invoker,执行invoker的invoke方法,反射调用方法,将结果通过Netty框架,将结果返回给consumer。
image.png
image.png

相关文章

网友评论

      本文标题:Dubbo - Provider服务提供者

      本文链接:https://www.haomeiwen.com/subject/bwhizqtx.html