Dubbo與Zookeeper、SpringMVC整合和使用(個人入門demo)

 

開發工具html

MyEclipse 10.7git

JDKgithub

1.7web

容器spring

Tomcat 8(運行dubbo)apache

zookeeper版本windows

zookeeper-3.4.6tomcat

dubboapp

dubbo-admin-2.5.3webapp

dubbo-admin-2.5.3下載地址:http://pan.baidu.com/s/1bozCMzP

zookeeper下載地址:http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.6/

Dubbo+Zookeeper 的下載安裝配置啓動

1.dubbo-admin-2.5.3下載完成後。放在webapps文件夾下面。先把默認的tomcat的ROOT項目備份後移除。將dubbo-admin-2.5.3.war改爲ROOT.war 備用

2.zookeeper下載後解壓安裝便可。windows安裝完成後以下圖

 

進入到conf裏面,會看到zoo_sample.cfg文件。複製一個修改成zoo.cfg,修改相關配置內容

# The number of milliseconds of each tick

tickTime=2000

# The number of ticks that the initial

# synchronization phase can take

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.

# do not use /tmp for storage, /tmp here is just

# example sakes.

dataDir=/tmp/zookeeper

# the port at which the clients will connect

#這塊是設置zookeeper的端口。默認2181

clientPort=2181

# the maximum number of client connections.

# increase this if you need to handle more clients

#maxClientCnxns=60

#

# Be sure to read the maintenance section of the

# administrator guide before turning on autopurge.

#

# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance

#

# The number of snapshots to retain in dataDir

#autopurge.snapRetainCount=3

# Purge task interval in hours

# Set to "0" to disable auto purge feature

#autopurge.purgeInterval=1

到bin文件夾下面。啓動zookeeper,windows 點擊zkServer.cmd便可

 

 

 

3.能夠啓動tomcat了。這樣直接訪問 就能看到dubbo的頁面。要否則dubbo啓動會報錯。第一次入門的話建議就用默認的端口配置便可。默認用戶 root 密碼 root

 

 

 

 

 

 

 

 

 

登陸成功之後看到以下頁面

 

以上就是dubbo+zookeeper的配置和啓動。下面開始Java代碼的編寫

  • 整合Dubbo+Zookeeper+SpringMVC

1.建立myDubbo項目爲主項目,結構圖以下 myC P S後續才建立,剛開始是沒有的。

 

POM.XML配置以下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com</groupId>

  <artifactId>mydubbo</artifactId>

  注意這裏。打包爲POM,分佈式系統的第一步

  <packaging>pom</packaging>

  <version>0.0.1-SNAPSHOT</version>

  <name>mydubbo</name>

  <url>http://maven.apache.org</url>

   <properties> 

        <spring.version>3.2.4.RELEASE</spring.version> 

    </properties>

  <dependencies>

            <dependency> 

            <groupId>com.alibaba</groupId> 

            <artifactId>dubbo</artifactId> 

            <version>2.5.3</version> 

            <exclusions> 

                <exclusion> 

                    <groupId>org.springframework</groupId> 

                    <artifactId>spring</artifactId> 

                </exclusion> 

            </exclusions> 

        </dependency> 

        <!--dubbo註冊中心--> 

        <dependency> 

            <groupId>org.apache.zookeeper</groupId> 

            <artifactId>zookeeper</artifactId> 

            <version>3.4.6</version> 

        </dependency> 

        <!--zookeeper客戶端--> 

        <dependency> 

            <groupId>com.github.sgroschupf</groupId> 

            <artifactId>zkclient</artifactId> 

            <version>0.1</version> 

        </dependency> 

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-core</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-context</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

  </dependencies>

  <build>

    <finalName>mydubbo</finalName>

  </build>

  建立子項目後自動會加載。剛開始是沒有的

  <modules>

         <module>myService</module>

         <module>myProvider</module>

         <module>myConsumer</module>

  </modules>

</project>

2.建立服務提供商子項目,MyEclipse操做以下圖示意 對mydubbo項目鼠標右鍵new或者maven插件直接建立maven module項目,填寫子項目名稱。有的若是不選種Create a simple project 自定義建立會報錯。選擇則不會。根據實際狀況來肯定是否選擇

 


3.建立3個子項目分別爲 myService(服務商(接口),模塊提供方) myProvider(供應者,給dubbo zookeeper註冊暴露接口) myConsumer

  • myService 項目相關配置
  1. pom配置
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.   <parent>
  5.     <artifactId>mydubbo</artifactId>
  6.     <groupId>com</groupId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.   </parent>
  9.   <name>myService</name>
  10. 10.   <artifactId>myService</artifactId>
  11. 11.   <packaging>jar</packaging>

</project>

 

  1. 新建一個接口類

13. package com.xiaoshuai;

  1. 14.  

15. public interface HelloService {

  1. 16.     public String speakHello(String name);

}

 

  1. 以上工做完成後,進行maven install
  • myProvider項目相關配置
  1. pom配置
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.   <parent>
  5.     <artifactId>mydubbo</artifactId>
  6.     <groupId>com</groupId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.   </parent>
  9.   <artifactId>myProvider</artifactId>

10. 引用接口服務項目 子項目maven install 爲jar

  1. 11.     <dependencies>
  2. 12.              <dependency>
  3. 13.                       <groupId> myService </groupId>
  4. 14.                       <artifactId>myService</artifactId>
  5. 15.                       <version>0.0.1-SNAPSHOT</version>
  6. 16.              </dependency>
  7. 17.     </dependencies>

</project>

18. 實現接口服務者

19. package com.xiaoshuai.impl;

  1. 20.  
  2. 21.  

22. import com.xiaoshuai.HelloService;

23. public class HelloServiceImpl implements HelloService{

  1. 24.  
  2. 25.     public String speakHello(String name) {
  3. 26.              return "你好:"+name+"歡迎查閱小帥丶博客";
  4. 27.     }
  5. 28.    

29. }

  1. 將實現類接口類暴露給dubbo+zookeeper 在myProvider建立provider.xml 內容以下

31. <?xml version="1.0" encoding="UTF-8"?>

32. <beans xmlns="http://www.springframework.org/schema/beans" 

  1. 33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  2. 34.        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
  3. 35.        xsi:schemaLocation="http://www.springframework.org/schema/beans       
  4. 36.        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 
  5. 37.   
  6. 38.     <!-- 提供方應用信息,用於計算依賴關係 --> 
  7. 39.     <dubbo:application name="hello-provider"  /> 
  8. 40.   
  9. 41.     <!-- 使用multicast廣播註冊中心暴露服務地址 --> 
  10. 42.     <dubbo:registry address="zookeeper://127.0.0.1:2181" /> 
  11. 43.   
  12. 44.     <!-- 用dubbo協議在20880端口暴露服務 --> 
  13. 45.     <dubbo:protocol name="dubbo" port="20880" /> 
  14. 46.   
  15. 47.     <!-- 聲明須要暴露的服務接口 --> 
  16. 48.     <dubbo:service interface="com.xiaoshuai.HelloService" ref="helloService" /> 
  17. 49.  
  18. 50.     <!-- 和本地bean同樣實現服務 --> 
  19. 51.     <bean id="helloService" class="com.xiaoshuai.impl.HelloServiceImpl" /> 
  20. 52.   

53. </beans> 

  1. 須要建立一個Java類寫個方法 去 加載provider.xml 註冊到dubbo + zookeeper

55. package com.xiaoshuai.impl;

56. import org.springframework.context.support.ClassPathXmlApplicationContext;

  1. 57.  

58. public class ProviderServer {

  1. 59.     public static void main(String[] args){
  2. 60.              try {
  3. 61.                       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
  4. 62.                       context.start();
  5. 63.                       System.in.read();
  6. 64.              } catch (Exception e) {
  7. 65.                       e.printStackTrace();
  8. 66.              }
  9. 67.     }

68. }

  1. 能夠運行。啓動該提供者服務,編寫消費者。
  2. 能夠看到dubbo 提供者服務已經註冊

 

 

  • myConsumer 項目相關配置
  1. pom配置
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.   <parent>
  5.     <artifactId>mydubbo</artifactId>
  6.     <groupId>com</groupId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.   </parent>
  9.   <artifactId>myConsumer</artifactId>
  10. 10.     <dependencies>
  11. 11.              <dependency>
  12. 12.                       <groupId>com</groupId>
  13. 13.                       <artifactId>myService</artifactId>
  14. 14.                       <version>0.0.1-SNAPSHOT</version>
  15. 15.              </dependency>
  16. 16.     </dependencies>

</project>

  1. 消費者進行訂閱provider的服務,也就是須要去zookeeper讀取加載服務,那就須要建立一個consumer.xml去加載zookeeper

18. <?xml version="1.0" encoding="UTF-8"?>

19. <beans xmlns="http://www.springframework.org/schema/beans"

  1. 20.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  2. 21.     xsi:schemaLocation="http://www.springframework.org/schema/beans       
  3. 22.        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  4. 23.     <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方同樣 -->
  5. 24.     <dubbo:application name="hello-consumer" />
  6. 25.     <!-- 使用multicast廣播註冊中心暴露發現服務地址 -->
  7. 26.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  8. 27.     <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService -->
  9. 28.     <dubbo:reference id="helloService" interface="com.xiaoshuai.HelloService" />

29. </beans>

  1. 建立一個消費者方法,進行調用ConsumerClient

31. package com.xiaoshuai;

  1. 32.  

33. import org.springframework.context.support.ClassPathXmlApplicationContext;

  1. 34.  

35. public class ConsumerClient {

  1. 36.     public static void main(String[] args) {
  2. 37.              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
  3. 38.              HelloService helloService = (HelloService) context.getBean("helloService");
  4. 39.              String result = helloService.speakHello("xiaoshuai");
  5. 40.              System.out.println(result);
  6. 41.     }

42. }

  1. 採起debug的方式運行該方法輸出如下內容
  2. 接下來看dubbo中心有什麼內容。
  3. 看看消費者裏面顯示些什麼內容
  4. 詳細消費者信息如圖所示
    • 以上一個簡單的分佈式的Demo就已經完結
相關文章
相關標籤/搜索