springboot+dubbo+zookeeper基礎學習
小菜鳥記錄第一次寫部落格
起始階段:
zookeeper官網下載地址:https://www.apache.org/dyn/closer.cgi/zookeeper/
下載成功後進入zookeeper/bin目錄下點選zkServer.cmd,檢視2181埠是否開啟,開啟則成功
dubbo下載地址:https://github.com/apache/incubator-dubbo
下載完成後把dubbo-admin打成war包部署在tomcat的webapps下面,啟動tomcat後訪問dubbo專案名即可
注意:先啟動zookeeper,再啟動tomcat
專案搭建:
provider:
目錄結構
pom檔案
<!-- springboot專案必須需要一個parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.2.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <springboot.version>1.3.2.RELEASE</springboot.version> <!--<swagger2.version>2.2.2</swagger2.version> --> <mybatis.version>1.1.1</mybatis.version> <!-- <mongodb.version>3.2.2</mongodb.version> --> </properties> <dependencies> <!-- springboot 基礎包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <!-- 引入log4j日誌時需去掉預設的logback --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 日誌管理 log4j --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency> <!-- springboot 測試包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- springboot web包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 新增dubbo依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.10</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <!-- 新增zk客戶端依賴 --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- zookeeperjar包 --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency> <!-- thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--資料庫相關--> <!-- springboot jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mysql支援 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Spring-data-jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 整合mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <!-- aop --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- alibaba的druid資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.0</version> </dependency> <!-- springboot工具 修改程式碼後不需重啟即生效 --> <!-- spring boot devtools 依賴包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>com.csf.springbootdzDemo</groupId> <artifactId>springboot-dubbo-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
springboot-provider配置檔案
provider啟動類:
配置@ImportResource讓啟動類啟動的時候掃描provider配置檔案
consumer
目錄結構
pom檔案
<!-- springboot專案必須需要一個parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.2.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <springboot.version>1.3.2.RELEASE</springboot.version> <!--<swagger2.version>2.2.2</swagger2.version> --> <mybatis.version>1.1.1</mybatis.version> <!-- <mongodb.version>3.2.2</mongodb.version> --> </properties> <dependencies> <!-- springboot 基礎包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <!-- 引入log4j日誌時需去掉預設的logback --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 日誌管理 log4j --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency> <!-- springboot 測試包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- springboot web包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 新增dubbo依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.10</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <!-- 新增zk客戶端依賴 --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- zookeeperjar包 --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency> <!-- thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--資料庫相關--> <!-- springboot jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mysql支援 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Spring-data-jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 整合mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <!-- aop --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- alibaba的druid資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.0</version> </dependency> <!-- springboot工具 修改程式碼後不需重啟即生效 --> <!-- spring boot devtools 依賴包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>com.csf.springbootdzDemo</groupId> <artifactId>springboot-dubbo-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
springboot-consumer配置檔案
consumer啟動類:
配置@ImportResource讓啟動類啟動的時候掃描consumer配置檔案
interface(此處service用於暴露介面供消費者呼叫)
目錄結構
程式碼實現流程
暴露服務的介面
位於provider的具體實現類
controller
總結:
dubbo作為一個分散式服務框架,需要服務提供者,服務消費者以及用於暴露服務的介面,如上圖所示,服務提供者在zookeeper註冊中心註冊服務,由服務消費者進行呼叫,如果所註冊的服務崩了,基於zookeeper自身負載均衡演算法進行計算再選擇下一個,而dubbo則作為監控中心記錄請求數和請求時間
這次的專案搭建實為第二次,第一次搭建的時候由於初學,不太弄得清楚他的實現原理以及流程,所以在dubbo主介面始終看不到消費者,現在知道是服務提供者提供服務,把服務暴露在介面,由消費方去呼叫暴露於介面的服務就好了~