4、環境搭建
4.1、zookeeper注冊中心的配置安裝(在windows平台下,Linux類似,見官方文檔)(Redis注冊中心安裝,簡易注冊中心安裝,簡易監控中心安裝,見官方文檔)
-
-
# The number of milliseconds of each tick//ZK中的一個時間單元2000ms tickTime=2000 # The number of ticks that the initial # synchronization phase can take//Leader允許Follower在initLimit時間內完成初始化工作。 initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored.\\就是把內存中的數據存儲成快照文件snapshot的目錄 dataDir=E:\\dubbo\\zookeeperB\\data # the port at which the clients will connect clientPort=2181
-
- 然后就可以啟動zk了點擊\bin\路徑下面的zkServer.cmd

運行:

4.2、監控中心的安裝配置
- 首先下載tomacat,安裝配置(略)
- 下載dubbo-admin包(dubbo-admin-2.5.4-SNAPSHOT.war)
- 找到 tomcat安裝路徑下的 .\webapps\ROOT目錄,然后清空里面的所有文件
- 將dubbo-admin包(dubbo-admin-2.5.4-SNAPSHOT.war)解壓到此文件中
- 在解壓的文件中找到\WEB-INF文件夾下的dubbo.properties文件,然后進行配置,默認屬性配置如下:
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
- 然后啟動tomcat(zookeeper已啟動,此步tomcat要訪問zookeeper,如果zk沒有啟動,tomcat會一直等待其啟動)。在瀏覽器中輸入localhost:8080,進入監控中心的管理界面(默認管理員賬戶密碼為:root,root)
4.3、服務提供者和消費者:
- 服務提供者和服務消費者簡單的示例:下面是eclipse平台下的服務提供者和服務消費者java項目截圖


- service的代碼:(consumer和provider中的Service接口一樣,其包名也要保持一致)
-
-
1 package com.renhq.dubbotest.provider; 2 publicinterfaceService{ 3 String sayHello(String name); 4 }
-
- ServiceIm的代碼:
-
-
1 package com.renhq.dubbotest.providerIm; 2 import java.io.IOException; 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 import com.renhq.dubbotest.provider.Service; 5 publicclassServiceImimplementsService{ 6 publicString sayHello(String name){ 7 return"Hello "+ name; 8 } 9 publicstaticvoid main(String[] args)throwsIOException{ 10 ClassPathXmlApplicationContext context =newClassPathXmlApplicationContext("provider.xml"); 11 context.start(); 12 System.in.read(); 13 } 14 }
-
- provider.xml的配置:
-
-
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beansxmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://code.alibabatech.com/schema/dubbo 8 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 9 <!-- 提供方應用信息,用於計算依賴關系 --> 10 <dubbo:applicationname="hello-world-provider"/> 11 <!-- 使用multicast廣播注冊中心暴露服務地址 12 <dubbo:registry address="multicast://224.5.6.7:1234" /> --> 13 <!-- 用dubbo協議在20880端口暴露服務 --> 14 <dubbo:protocolname="dubbo"port="20888"/> 15 <!-- 使用zookeeper注冊中心暴露服務地址 --> 16 <dubbo:registryaddress="zookeeper://127.0.0.1:2181"/> 17 <!-- 聲明需要暴露的服務接口 --> 18 <dubbo:serviceinterface="com.renhq.dubbotest.provider.Service"ref="demoService"/> 19 <!-- 和本地bean一樣實現服務 --> 20 <beanid="demoService"class="com.renhq.dubbotest.providerIm.ServiceIm"/> 21 </beans>
-
- Consumer的代碼:
-
-
1 package com.renhq.dubbotest.comsumer; 2 import java.io.IOException; 3 import java.util.Collections; 4 import java.util.HashMap; 5 import java.util.Map; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 import com.renhq.dubbotest.provider.Service; 8 publicclassConsumer{ 9 publicstaticvoid main(String[] args)throwsIOException{ 10 ClassPathXmlApplicationContext context =newClassPathXmlApplicationContext("consumer.xml"); 11 context.start(); 12 // System.out.println("----------------nihao ----------------------"); // 顯示調用結果 13 Service demoService =(Service) context.getBean("demoService");// 獲取遠程服務代理 14 String hello = demoService.sayHello("world");// 執行遠程方法 15 System.out.println(hello);// 顯示調用結果 16 System.in.read(); 17 } 18 }
-
- consumer.xml的配置:
-
-
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beansxmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://code.alibabatech.com/schema/dubbo 8 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 9 <!-- 消費方應用名,用於計算依賴關系,不是匹配條件,不要與提供方一樣 --> 10 <dubbo:applicationname="consumer-of-helloworld-app"/> 11 <!-- 使用multicast廣播注冊中心暴露發現服務地址 --> 12 <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> 13 <!-- 使用zookeeper注冊中心暴露服務地址 --> 14 <dubbo:registryaddress="zookeeper://127.0.0.1:2181"/> 15 <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService --> 16 <dubbo:referenceid="demoService"interface="com.renhq.dubbotest.provider.Service"/> 17 </beans>
-
- 沒有注冊中心情況下服務提供者和服務消費者的鏈接:
provider.xml的配置:
<!--在沒有注冊中心,直接暴露提供者的情況下,即:-->
<dubbo:serviceinterface="com.renhq.dubbotest.provider.Service"ref="demoService"registry="N/A"/>
consumer.xml的配置:
<!--直連引用服務:在沒有注冊中心,直連提供者的情況下-->
<dubbo:referenceid="demoService"interface="com.renhq.dubbotest.provider.Service"url="dubbo://192.168.1.103:20880/com.renhq.dubbotest.provider.Service"/>
- 運行結果(先運行provider項目進行服務注冊,再運行consumer項目訪問服務):provider:
- consumer
附件列表