配置多注冊中心
打開 provider 模塊的 spring-provider.xml 配置文件,修改成如下:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!-- 添加 DUBBO SCHEMA -->
3 <beans xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo
8 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
9
10 <!-- 應用名 -->
11 <dubbo:application name="dubbodemo-provider"/>
12 <!-- 連接到哪個本地注冊中心 -->
13 <dubbo:registry id="dubbodemo" address="zookeeper://localhost:2181"/>
14 <dubbo:registry id="dubbodemo_copy" address="zookeeper://localhost:2182"/>
15 <!-- 用dubbo協議在20880端口暴露服務 -->
16 <dubbo:protocol name="dubbo" port="28080"/>
17 <!-- 聲明需要暴露的服務接口 -->
18 <dubbo:service registry="dubbodemo" timeout="3000" interface="com.chanshuyi.service.IUserService" ref="userService"/>
19 <dubbo:service registry="dubbodemo_copy" timeout="3000" interface="com.chanshuyi.service.IBookService" ref="bookService"/>
20 </beans>
第 13、14 行分別聲明了一個 id 為 “dubbodemo”、“dubbodemo_copy”的注冊中心,分別對應本地機器的 2181 和 2182 端口。
第 18 行暴漏了 IUserService 接口並顯式指定將其注冊到 dubbodemo 注冊中心。
第 19 行暴漏了 IBookService 接口並顯示指定將其注冊到 dubbodemo_copy 注冊中心。
zookeeper查看dubbo注冊信息
查看zookeeper中dubbo服務的生產者和消費者注冊信息:
1.進入zookepeer的安裝目錄下
2.進入bin ./zkCli.sh -server ip+端口號
如圖所示
3.查看dubbo服務提供的對外接口,及消息提供者 :ls /dubbo
4.ls /dubbo/**/consumers //查看消費者命令
ls /dubbo/**/providers //查看服務提供者命令
可以看到這個服務既有消費端也有生產端