dubbo+zookeeper配置及使用
Dubbo?
Dubbo是資源排程和治理中心的管理工具
服務端:釋出服務---註冊服務
客戶端:連線註冊---返回結果---呼叫服務
官方推薦使用Zookeeper作為Dubbo的註冊中心
Zookeeper:
1.可以作為叢集管理工具使用。
2.可以集中管理配置檔案。
安裝zoopeeker:
進入Apache官網,找到zookeeper專案下載http://www.apache.org/index.html#projects-list
導航:project------點選
找到:Getting Started---Download
利用工具吧zookeeper上傳到linux系統中
解壓:
建立一個目錄:
進入conf資料夾中,拷貝zoo_sample.cfg--->zoo.cfg
編輯剛剛複製好的cfg檔案
vim zoo.cfg
修改:把資料檔案放到之前建立好的data目錄中去
進入bin目錄中啟動zookeeper
啟動:./zkServer.sh start
檢視狀態:./zkServer.sh status
關閉:./zkServer.sh stop
如果遇到啟動了 但是檢視狀態的時候說沒啟動的時候,吧data裡面的pid檔案刪除 重啟ok
自選:1.關閉防火牆(自己的虛擬機器,開毛線的防火牆呀)
2.配置防火牆,開啟埠 2181
防火牆開放埠
vim /etc/sysconfig/iptables
二選一
到此,註冊中心配置文成
釋出服務:
工程中需要新增dubbo依賴的jar包,zookeeper以及zookeeper的客戶端zkclient
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> |
spring配置檔案:
加上dubbo的字首以及約束 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd |
<!-- 使用dubbo釋出服務 --> <dubbo:application name="服務的工程名(別重複)" /> <!-- 表明註冊中心是什麼 叢集:192.168.233.128:2181,192.168.233.128:2182..... --> <dubbo:registry protocol="zookeeper" address="192.168.233.128:2181" /><!-- 用dubbo協議在20880埠暴露服務,預設:20880 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 宣告需要暴露的服務介面 --> <dubbo:service interface="介面的全限定名[com.xyh.service.TestService]" ref="testServiceImpl[實現類物件]" timeout="600000" /> |
到此,服務已經可以釋出,接下來是引用服務
同樣的需要引入相關的jar包
SpringNVC.XML: xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd |
pom.xml: <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> |
引用服務-----reference
<dubbo:application name="工程名"/> <dubbo:registry protocol="zookeeper" address="192.168.233.128:2181"/> <dubbo:reference interface="com.xyh.service.TestService" id="testService"/> |
Controller中就可以直接注入使用
@Autowired private TestService testService; |
到此,已經可以使用,現在需要啟動工程來測試就ok~~
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd