下面将运行dubbo-demo,先来直观感受dubbo的作用。
1.先启动Zookeeper;Zookeeper的安装请参考: Java实习日记(2-1)
2.将打包好的dubbo-admin.war部署到Tomcat的webapps下,并启动;具体流程请参考:Java实习日记(2-2)
3.Eclipse中打开官网自带的dubbo-demo,先来修改以下两个配置文件:dubbo-demo-provider中的dubbo-demo-provider.xml和dubbo-demo-consumer中的dubbo-demo-consumer.xml
4.修改dubbo-demo-provider.xml中注册中心的配置
修改前 <dubbo:registry address="multicast://224.5.6.7:1234"/>
修改后 <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
因为Zookeeper安装在本地,所以此处的IP地址写成127.0.0.1
dubbo-demo-provider.xml如下:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="demo-provider"/>
<!-- use multicast registry center to export service -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service implementation, as same as regular local bean -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
<!-- declare the service interface to be exported -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
</beans>
5.同理,修改dubbo-demo-consumer.xml中注册中心的配置
修改前 <dubbo:registry address="multicast://224.5.6.7:1234"/>
修改后 <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
dubbo-demo-consumer.xml如下:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="demo-consumer"/>
<!-- use multicast registry center to discover service -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234"/> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>
6.先运行dubbo-demo-provider项目中的Provider,控制台打印以下信息,说明Provider启动成功
打开浏览器,输入:http://localhost:10080/dubbo-admin-2.5.8/governance/providers,账号登录,可看到以下内容,说明Provider在Zookeeper注册完成,成功的暴露出服务接口
7.紧接着运行dubbo-demo-consumer项目中的Consumer,控制台打印以下信息,说明Consumer启动成功,并成功调用Provider提供的DemoService接口
打开浏览器也会出现以下情形
至此,Dubbo官网提供的Demo已经完全成功运行,下面将结合Dubbo原理详细分析这个Demo的源码。
简单总结一下我理解的Dubbo,如有错误,请批评指正!
在分布式系统中,A模块提供接口并实现,B模块想使用A模块的接口,于是A和B都到第三方Zookeeper那里注册,A把接口暴露给zookeeper,B通过zookeeper找到A的接口实现类,直接调用来满足自己需求。
用生活的例子来理解就像是租房子,A是房东,B是租客,Zookeeper是房屋中介。