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;
tickTime:這個時間是作為 Zookeeper 伺服器之間或客戶端與伺服器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。
- clientPort=2181
- #server.1=127.0.0.1:3881:4881
- #server.2=127.0.0.1:3882:4882
- #server.3=127.0.0.1:3883:4883
- dataDir=../data
- dataLogDir=../log
- # The number of milliseconds of each tick
- tickTime=10000
- # 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=../data
- #dataLogDir=../dataLog
- # the port at which the clients will connect
- #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
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包
-
<?xml version="1.0"?>
-
<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"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance">
-
<modelVersion>4.0.0
</modelVersion>
-
<parent>
-
<groupId>user
</groupId>
-
<artifactId>user
</artifactId>
-
<version>0.0.1-SNAPSHOT
</version>
-
</parent>
-
<artifactId>user-service
</artifactId>
-
<packaging>jar
</packaging>
-
<!-- <name>user-service Maven Webapp</name>
-
<url>http://maven.apache.org</url> -->
-
<properties>
-
<project.build.sourceEncoding>UTF-8
</project.build.sourceEncoding>
-
<!-- 設定 Spring 的版本 -->
-
<org.springframework.version>4.1.7.RELEASE
</org.springframework.version>
-
</properties>
-
<dependencies>
-
<dependency>
-
<groupId>user-api
</groupId>
-
<artifactId>user-api
</artifactId>
-
<version>0.0.1-SNAPSHOT
</version>
-
</dependency>
-
<dependency>
-
<groupId>junit
</groupId>
-
<artifactId>junit
</artifactId>
-
<version>3.8.1
</version>
-
<scope>test
</scope>
-
</dependency>
-
-
<dependency>
-
<groupId>com.alibaba
</groupId>
-
<artifactId>dubbo
</artifactId>
-
<version>2.5.3
</version>
-
<exclusions>
-
<exclusion>
-
<artifactId>spring
</artifactId>
-
<groupId>org.springframework
</groupId>
-
</exclusion>
-
</exclusions>
-
</dependency>
-
<dependency>
-
<groupId>org.apache.zookeeper
</groupId>
-
<artifactId>zookeeper
</artifactId>
-
<version>3.4.6
</version>
-
<exclusions>
-
<exclusion>
-
<artifactId>log4j
</artifactId>
-
<groupId>log4j
</groupId>
-
</exclusion>
-
</exclusions>
-
</dependency>
-
<dependency>
-
<groupId>org.slf4j
</groupId>
-
<artifactId>slf4j-api
</artifactId>
-
<version>1.7.12
</version>
-
</dependency>
-
<dependency>
-
<groupId>ch.qos.logback
</groupId>
-
<artifactId>logback-core
</artifactId>
-
<version>1.1.1
</version>
-
</dependency>
-
<!-- 實現slf4j介面並整合 -->
-
<dependency>
-
<groupId>ch.qos.logback
</groupId>
-
<artifactId>logback-classic
</artifactId>
-
<version>1.1.1
</version>
-
</dependency>
-
<!--2:資料庫相關依賴 -->
-
<dependency>
-
<groupId>mysql
</groupId>
-
<artifactId>mysql-connector-java
</artifactId>
-
<version>5.1.35
</version>
-
<scope>runtime
</scope>
-
</dependency>
-
<dependency>
-
<groupId>c3p0
</groupId>
-
<artifactId>c3p0
</artifactId>
-
<version>0.9.1.2
</version>
-
</dependency>
-
-
<!-- DAO框架:MyBatis依賴 -->
-
<dependency>
-
<groupId>org.mybatis
</groupId>
-
<artifactId>mybatis
</artifactId>
-
<version>3.3.0
</version>
-
</dependency>
-
<!-- mybats自身實現的spring整合依賴 -->
-
<dependency>
-
<groupId>org.mybatis
</groupId>
-
<artifactId>mybatis-spring
</artifactId>
-
<version>1.2.3
</version>
-
</dependency>
-
-
<!-- 3:Servlet web相關依賴 -->
-
<dependency>
-
<groupId>taglibs
</groupId>
-
<artifactId>standard
</artifactId>
-
<version>1.1.2
</version>
-
</dependency>
-
<dependency>
-
<groupId>jstl
</groupId>
-
<artifactId>jstl
</artifactId>
-
<version>1.2
</version>
-
</dependency>
-
<dependency>
-
<groupId>com.fasterxml.jackson.core
</groupId>
-
<artifactId>jackson-databind
</artifactId>
-
<version>2.5.4
</version>
-
</dependency>
-
<dependency>
-
<groupId>javax.servlet
</groupId>
-
<artifactId>javax.servlet-api
</artifactId>
-
<version>3.1.0
</version>
-
</dependency>
-
-
<!--4:spring依賴 -->
-
<!--1)spring核心依賴 -->
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-core
</artifactId>
-
<version>${org.springframework.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-beans
</artifactId>
-
<version>${org.springframework.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>