1.1 zookeeper部署
- 下載zookeeper包(zookeeper-3.3.6.tar.gz),ZooKeeper是一個(gè)分布式的游昼,開放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是Google的Chubby一個(gè)開源的實(shí)現(xiàn)尝蠕,是Hadoop和Hbase的重要組件酱床。它是一個(gè)為分布式應(yīng)用提供一致性服務(wù)的軟件,提供的功能包括:配置維護(hù)趟佃、名字服務(wù)、分布式同步昧捷、組服務(wù)等闲昭。
- 將下載的包解壓,然后在.\zookeeper-3.3.6\conf\路徑下找到:zoo_sample.cfg文件靡挥,這個(gè)是配置的實(shí)例文件序矩,復(fù)制此文件建立:zoo.cfg文件
`# The number of milliseconds of each tick//ZK中的一個(gè)時(shí)間單元2000ms`
`tickTime=2000`
`# The number of ticks that the initial`
`# synchronization phase can take//Leader允許Follower在initLimit時(shí)間內(nèi)完成初始化工作。`
`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.\\就是把內(nèi)存中的數(shù)據(jù)存儲(chǔ)成快照文件snapshot的目錄`
`dataDir=E:\\dubbo\\zookeeperB\\data`
`# the port at which the clients will connect`
`clientPort=2181`
|
- 然后就可以啟動(dòng)zk了點(diǎn)擊\bin\路徑下面的zkServer.cmd
image
- 運(yùn)行:
image
1.2 監(jiān)控中心的安裝配置
首先下載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文件,然后進(jìn)行配置毒返,默認(rèn)屬性配置如下:
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
-
然后啟動(dòng)tomcat(zookeeper已啟動(dòng)租幕,此步tomcat要訪問zookeeper,如果zk沒有啟動(dòng)拧簸,tomcat會(huì)一直等待其啟動(dòng))劲绪。在瀏覽器中輸入localhost:8080,進(jìn)入監(jiān)控中心的管理界面(默認(rèn)管理員賬戶密碼為:root盆赤,root)image
1.3 服務(wù)提供者和消費(fèi)者:
- 服務(wù)提供者和服務(wù)消費(fèi)者簡(jiǎn)單的示例:下面是eclipse平臺(tái)下的服務(wù)提供者和服務(wù)消費(fèi)者java項(xiàng)目截圖
image
image
- service的代碼:(consumer和provider中的Service接口一樣贾富,其包名也要保持一致)
package com.renhq.dubbotest.provider;
publicinterfaceService{
String sayHello(String name);
}
- ServiceIm的代碼:
package com.renhq.dubbotest.providerIm;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.renhq.dubbotest.provider.Service;
publicclassServiceImimplementsService{
publicString sayHello(String name){
return"Hello "+ name;
}
publicstaticvoid main(String[] args)throwsIOException{
ClassPathXmlApplicationContext context =newClassPathXmlApplicationContext("provider.xml");
context.start();
System.in.read();
}
}
- provider.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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">
<!-- 提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系 -->
<dubbo:applicationname="hello-world-provider"/>
<!-- 使用multicast廣播注冊(cè)中心暴露服務(wù)地址
<dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocolname="dubbo"port="20888"/>
<!-- 使用zookeeper注冊(cè)中心暴露服務(wù)地址 -->
<dubbo:registryaddress="zookeeper://127.0.0.1:2181"/>
<!-- 聲明需要暴露的服務(wù)接口 -->
<dubbo:serviceinterface="com.renhq.dubbotest.provider.Service"ref="demoService"/>
<!-- 和本地bean一樣實(shí)現(xiàn)服務(wù) -->
<beanid="demoService"class="com.renhq.dubbotest.providerIm.ServiceIm"/>
</beans>
- Consumer的代碼:
package com.renhq.dubbotest.comsumer;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.renhq.dubbotest.provider.Service;
publicclassConsumer{
publicstaticvoid main(String[] args)throwsIOException{
ClassPathXmlApplicationContext context =newClassPathXmlApplicationContext("consumer.xml");
context.start();
// System.out.println("----------------nihao ----------------------"); // 顯示調(diào)用結(jié)果
Service demoService =(Service) context.getBean("demoService");// 獲取遠(yuǎn)程服務(wù)代理
String hello = demoService.sayHello("world");// 執(zhí)行遠(yuǎn)程方法
System.out.println(hello);// 顯示調(diào)用結(jié)果
System.in.read();
}
}
- consumer.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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">
<!-- 消費(fèi)方應(yīng)用名牺六,用于計(jì)算依賴關(guān)系颤枪,不是匹配條件,不要與提供方一樣 -->
<dubbo:applicationname="consumer-of-helloworld-app"/>
<!-- 使用multicast廣播注冊(cè)中心暴露發(fā)現(xiàn)服務(wù)地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用zookeeper注冊(cè)中心暴露服務(wù)地址 -->
<dubbo:registryaddress="zookeeper://127.0.0.1:2181"/>
<!-- 生成遠(yuǎn)程服務(wù)代理淑际,可以和本地bean一樣使用demoService -->
<dubbo:referenceid="demoService"interface="com.renhq.dubbotest.provider.Service"/>
</beans>
- 沒有注冊(cè)中心情況下服務(wù)提供者和服務(wù)消費(fèi)者的鏈接:
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"/>
- 運(yùn)行結(jié)果(先運(yùn)行provider項(xiàng)目進(jìn)行服務(wù)注冊(cè)畏纲,再運(yùn)行consumer項(xiàng)目訪問服務(wù)):provider:
image
- consumer
image
轉(zhuǎn)自:https://www.cnblogs.com/renhq/p/4654925.html