在快速开始案例中,我们没有使用任何的注册中心,而是用一种直连的方式进行的。
但是,实际上很多时候都是使用 dubbo + zookeeper 的方式,使用 zookeeper 作为注册中心,
这里,我们就介绍一下 zookeeper 作为注册中心的使用方法。
这里,我们对前面的 快速开始案例 中进行改造。
一、服务端
在服务端中,我们只需要修改provider.xml 即可。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签--><dubbo:application name="provider" owner="yuanzi"><dubbo:parameter key="qos.enable" value="true"/><dubbo:parameter key="qos.accept.foreign.ip" value="false"/><dubbo:parameter key="qos.port" value="55555"/></dubbo:application><dubbo:monitor protocol="registry"/><!--dubbo这个服务所要暴露的服务地址所对应的注册中心--><!--<dubbo:registry address="N/A"/>--><dubbo:registry address="zookeeper://localhost:2181" check="false"/><!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http--><dubbo:protocol name="dubbo" port="20880"/><!--服务发布的配置,需要暴露的服务接口--><dubbo:serviceinterface="com.yuanzi.dubbo.ProviderService"ref="providerService"/><!--Bean bean定义--><bean id="providerService" class="com.yuanzi.dubbo.ProviderServiceImpl"/></beans>
重点关注这句话<dubbo:registry address="zookeeper://localhost:2181" />
在 address 中,使用我们的 zookeeper 的地址。
如果是 zookeeper 集群的话,使用下面的方式。
<dubbo:registry protocol="zookeeper"
address="192.168.11.129:2181,192.168.11.137:2181,192.168.11.138:2181"/>
服务端的配置就好了,其他的跟 快速开始案例 一样。
二、消费端
跟服务端一样,在消费端,我们也只需要修改 consumer.xml 即可。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签-->
<dubbo:application name="consumer" owner="yuanzi"/>
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<!--点对点的方式-->
<dubbo:registry address="zookeeper://localhost:2181" check="false"/>
<!--<dubbo:registry address="zookeeper://localhost:2181" check="false"/>-->
<!--生成一个远程服务的调用代理-->
<!--点对点方式-->
<dubbo:reference id="providerService"
interface="com.yuanzi.dubbo.ProviderService"/>
<!-- url="dubbo://10.57.242.17:20880/com.yuanzi.dubbo.ProviderService"/>-->
</beans>
① 注册中心配置跟服务端一样。<dubbo:registry address="zookeeper://localhost:2181"/>
② dubbo:reference
由于我们这里使用 zookeeper 作为注册中心,所以,跟点对点的方式是不一样的,这里不再需要 dubbo 服务端提供的 url 了,只需要直接引用服务端提供的接口即可。
好了,消费端也配置好了,这样就可以使用修改的入门案例,重新启动运行了。

同样成功了。
这时候的区别在于,将 dubbo 发布的 url 注册到了 zookeeper,消费端从 zookeeper 消费,zookeeper 相当于一个中介,给消费者提供服务。
项目中的配置
