1. 程式人生 > >java後端學習 SSM整合dubbo+zookeeper

java後端學習 SSM整合dubbo+zookeeper

最近剛好有點時間,特意來整合一下專案,以後會陸續把前端框架和一些元件也給整合進去,今天就先來整合一下SSM+mysql+dubbo+zookeeper來做一個使用者列表功能。關於理論性的知識,大家可以自行查閱下資料,這裡重點講解一下怎麼搭建這一套開發環境。

背景

隨著網際網路的發展,網站應用的規模不斷擴大,常規的垂直應用架構已無法應對,分散式服務架構以及流動計算架構勢在必行,亟需一個治理系統確保架構有條不紊的演進。

  • 單一應用架構
    • 當網站流量很小時,只需一個應用,將所有功能都部署在一起,以減少部署節點和成本。
    • 此時,用於簡化增刪改查工作量的 資料訪問框架(ORM) 是關鍵。
    • 早期的JSP,ASP,PHP都是把資料操作寫在前端,功能相當簡單。
  • 垂直應用架構
    • 當訪問量逐漸增大,單一應用增加機器帶來的加速度越來越小,將應用拆成互不相干的幾個應用,以提升效率。
    • 此時,用於加速前端頁面開發的 Web框架(MVC) 是關鍵。現在很多非網際網路公司仍然採用這種架構設計。
  • 分散式服務架構
    • 當垂直應用越來越多,應用之間互動不可避免,將核心業務抽取出來,作為獨立的服務,逐漸形成穩定的服務中心,使前端應用能更快速的響應多變的市場需求。
    • 此時,用於提高業務複用及整合的 分散式服務框架(RPC) 是關鍵。
  • 流動計算架構
    • 當服務越來越多,容量的評估,小服務資源的浪費等問題逐漸顯現,此時需增加一個排程中心基於訪問壓力實時管理叢集容量,提高叢集利用率。
    • 此時,用於提高機器利用率的 資源排程和治理中心(SOA) 是關鍵。

開發工具:

這裡貼出自己的開發工具和版本號,因為有的時候可能會因為版本號而造成很多不必要的衝突。

zookeeper-3.4.6+windows系統+mysql 5.6+jdk7+eclipse+tomcat7+dubbo 2.5.3

zookeeper:

下載zookeeper註冊中心,下載地址:http://www.apache.org/dyn/closer.cgi/zookeeper/ 下載後解壓即可。


然後進入conf裡面,改一下我們的配置,開啟zoo.cfg,有得可能不叫這個名字,把zoo_sample.cfg改成zoo.cfg


    
  1. clientPort=2181
  2. #server.1=127.0.0.1:3881:4881
  3. #server.2=127.0.0.1:3882:4882
  4. #server.3=127.0.0.1:3883:4883
  5. dataDir=../data
  6. dataLogDir=../log
  7. # The number of milliseconds of each tick
  8. tickTime=10000
  9. # The number of ticks that the initial
  10. # synchronization phase can take
  11. initLimit=10
  12. # The number of ticks that can pass between
  13. # sending a request and getting an acknowledgement
  14. syncLimit=5
  15. # the directory where the snapshot is stored.
  16. # do not use /tmp for storage, /tmp here is just
  17. # example sakes.
  18. #dataDir=../data
  19. #dataLogDir=../dataLog
  20. # the port at which the clients will connect
  21. #clientPort=2181
  22. # the maximum number of client connections.
  23. # increase this if you need to handle more clients
  24. #maxClientCnxns=60
  25. #
  26. # Be sure to read the maintenance section of the
  27. # administrator guide before turning on autopurge.
  28. #
  29. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  30. #
  31. # The number of snapshots to retain in dataDir
  32. #autopurge.snapRetainCount=3
  33. # Purge task interval in hours
  34. # Set to "0" to disable auto purge feature
  35. #autopurge.purgeInterval=1
tickTime:這個時間是作為 Zookeeper 伺服器之間或客戶端與伺服器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。
dataDir:顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡clientPort:這個埠就是客戶端連線 Zookeeper 伺服器的埠,Zookeeper 會監聽這個埠,接受客戶端的訪問請求。
當這些配置項配置好後,你現在就可以啟動 Zookeeper 了,啟動後要檢查 Zookeeper 是否已經在服務,可以通過 netstat – ano 命令檢視是否有你配置的 clientPort 埠號在監聽服務。

  • server.A=B:C:D:其中 A 是一個數字,表示這個是第幾號伺服器;B 是這個伺服器的 ip 地址;C 表示的是這個伺服器與叢集中的 Leader 伺服器交換資訊的埠;D 表示的是萬一叢集中的 Leader 伺服器掛了,需要一個埠來重新進行選舉,選出一個新的 Leader,而這個埠就是用來執行選舉時伺服器相互通訊的埠。如果是偽叢集的配置方式,由於 B 都是一樣,所以不同的 Zookeeper 例項通訊埠號不能一樣,所以要給它們分配不同的埠號。


最後到bin目錄下面啟動,客戶端和服務端。


Dubbo:

需要下載:dubbo-admin-2.5.3的war

然後Tomcat啟動一下。這裡有兩種啟動方式,一種是找到自己的tomcat安裝目錄,替換掉tomcat/webapps下自帶的ROOT資料夾內容(即替換tomcat的啟動主頁),將下載的war包解壓到webapps/ROOT中,直接替換即可。

我用的是下面這種,比較簡單點。開啟eclipse裡面的tomcat管理頁面。


然後選擇第二個,找到自己剛剛下載的war包,部署並且啟動一下。

這個時候在瀏覽器輸入自己的ip和埠,我的是localhost:8080


看到這個管理頁面就代表成功了。可以來檢測我們的消費者和生產者了。

SSM整合:

這裡先編寫我們的服務端,我把提供服務的統稱為服務端,所以這裡先建立一個服務端。

所以這裡新建一個專案叫做user-service

Maven引入需要的JAR包


     
  1. <?xml version="1.0"?>
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance">
  4. <modelVersion>4.0.0 </modelVersion>
  5. <parent>
  6. <groupId>user </groupId>
  7. <artifactId>user </artifactId>
  8. <version>0.0.1-SNAPSHOT </version>
  9. </parent>
  10. <artifactId>user-service </artifactId>
  11. <packaging>jar </packaging>
  12. <!-- <name>user-service Maven Webapp</name>
  13. <url>http://maven.apache.org</url> -->
  14. <properties>
  15. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  16. <!-- 設定 Spring 的版本 -->
  17. <org.springframework.version>4.1.7.RELEASE </org.springframework.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>user-api </groupId>
  22. <artifactId>user-api </artifactId>
  23. <version>0.0.1-SNAPSHOT </version>
  24. </dependency>
  25. <dependency>
  26. <groupId>junit </groupId>
  27. <artifactId>junit </artifactId>
  28. <version>3.8.1 </version>
  29. <scope>test </scope>
  30. </dependency>
  31. <dependency>
  32. <groupId>com.alibaba </groupId>
  33. <artifactId>dubbo </artifactId>
  34. <version>2.5.3 </version>
  35. <exclusions>
  36. <exclusion>
  37. <artifactId>spring </artifactId>
  38. <groupId>org.springframework </groupId>
  39. </exclusion>
  40. </exclusions>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.apache.zookeeper </groupId>
  44. <artifactId>zookeeper </artifactId>
  45. <version>3.4.6 </version>
  46. <exclusions>
  47. <exclusion>
  48. <artifactId>log4j </artifactId>
  49. <groupId>log4j </groupId>
  50. </exclusion>
  51. </exclusions>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.slf4j </groupId>
  55. <artifactId>slf4j-api </artifactId>
  56. <version>1.7.12 </version>
  57. </dependency>
  58. <dependency>
  59. <groupId>ch.qos.logback </groupId>
  60. <artifactId>logback-core </artifactId>
  61. <version>1.1.1 </version>
  62. </dependency>
  63. <!-- 實現slf4j介面並整合 -->
  64. <dependency>
  65. <groupId>ch.qos.logback </groupId>
  66. <artifactId>logback-classic </artifactId>
  67. <version>1.1.1 </version>
  68. </dependency>
  69. <!--2:資料庫相關依賴 -->
  70. <dependency>
  71. <groupId>mysql </groupId>
  72. <artifactId>mysql-connector-java </artifactId>
  73. <version>5.1.35 </version>
  74. <scope>runtime </scope>
  75. </dependency>
  76. <dependency>
  77. <groupId>c3p0 </groupId>
  78. <artifactId>c3p0 </artifactId>
  79. <version>0.9.1.2 </version>
  80. </dependency>
  81. <!-- DAO框架:MyBatis依賴 -->
  82. <dependency>
  83. <groupId>org.mybatis </groupId>
  84. <artifactId>mybatis </artifactId>
  85. <version>3.3.0 </version>
  86. </dependency>
  87. <!-- mybats自身實現的spring整合依賴 -->
  88. <dependency>
  89. <groupId>org.mybatis </groupId>
  90. <artifactId>mybatis-spring </artifactId>
  91. <version>1.2.3 </version>
  92. </dependency>
  93. <!-- 3:Servlet web相關依賴 -->
  94. <dependency>
  95. <groupId>taglibs </groupId>
  96. <artifactId>standard </artifactId>
  97. <version>1.1.2 </version>
  98. </dependency>
  99. <dependency>
  100. <groupId>jstl </groupId>
  101. <artifactId>jstl </artifactId>
  102. <version>1.2 </version>
  103. </dependency>
  104. <dependency>
  105. <groupId>com.fasterxml.jackson.core </groupId>
  106. <artifactId>jackson-databind </artifactId>
  107. <version>2.5.4 </version>
  108. </dependency>
  109. <dependency>
  110. <groupId>javax.servlet </groupId>
  111. <artifactId>javax.servlet-api </artifactId>
  112. <version>3.1.0 </version>
  113. </dependency>
  114. <!--4:spring依賴 -->
  115. <!--1)spring核心依賴 -->
  116. <dependency>
  117. <groupId>org.springframework </groupId>
  118. <artifactId>spring-core </artifactId>
  119. <version>${org.springframework.version} </version>
  120. </dependency>
  121. <dependency>
  122. <groupId>org.springframework </groupId>
  123. <artifactId>spring-beans </artifactId>
  124. <version>${org.springframework.version} </version>
  125. </dependency>
  126. <dependency>
  127. <groupId>org.springframework </groupId>