Dubbo使用注冊中心
直接改造上一篇文章創建的工程
1、復制之前的的專案,修改專案名和pom檔案(這里只演示一個)

修改artifactId與工程名一致

2、洗掉選中的多余的檔案

3、匯入model(其余兩個操作相同)

4、匯入maven工程
一直next就行

5、修改服務提供者和消費者的pom檔案
<dependency>
<groupId>com.why</groupId>
<artifactId>ch06-interface</artifactId>
<version>1.0.0</version>
</dependency>
6、給服務提供者添加zookeeper依賴
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
7、修改服務提供者的dubbo核心組態檔
<?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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--宣告dubbo服務提供者名稱:保證唯一-->
<!--選擇dubbo.apache.org的那個-->
<dubbo:application name="ch06-StudentService-provide"/>
<!--宣告dubbo使用的協議和埠,dubbo官方推薦使用的協議為dubbo,埠號默認為20880-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--指定注冊中心的地址和埠號-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--暴露服務(使用注冊中心)-->
<!--這里就不需要使用registry屬性-->
<dubbo:service interface="com.why.service.StudentService" ref="StudentService" />
<bean id="StudentService" class="com.why.service.Imp.StudentServiceImp"/>
</beans>
8、給服務消費者添加zookeeper依賴
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
9、修改服務消費者dubbo核心組態檔
<?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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--宣告dubbo服務提供者名稱:保證唯一-->
<!--選擇dubbo.apache.org的那個-->
<dubbo:application name="ch06-StudentService-consumer"/>
<!--指定注冊中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--參考遠程服務介面-->
<!--這里也不需要registry屬性了-->
<dubbo:reference id="userService" interface="com.why.service.StudentService" />
</beans>
10、打開注冊中心

測驗:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/356934.html
標籤:其他
