java實(shí)現(xiàn)分布式項(xiàng)目搭建的方法
1 分布式
1.1 什么是分布式
- 分布式系統(tǒng)一定是由多個(gè)節(jié)點(diǎn)組成的系統(tǒng)。其中,節(jié)點(diǎn)指的是計(jì)算機(jī)服務(wù)器,而且這些節(jié)點(diǎn)一般不是孤立的,而是互通的。
- 這些連通的節(jié)點(diǎn)上部署了我們的節(jié)點(diǎn),并且相互的操作會(huì)有協(xié)同。分布式系統(tǒng)對(duì)于用戶而言,他們面對(duì)的就是一個(gè)服務(wù)器,提供用戶需要的服務(wù)而已,而實(shí)際上這些服務(wù)是通過(guò)背后的眾多服務(wù)器組成的一個(gè)分布式系統(tǒng),因此分布式系統(tǒng)看起來(lái)像是一個(gè)超級(jí)計(jì)算機(jī)一樣。
1.2 分布式與集群的區(qū)別
- 集群是同一個(gè)系統(tǒng)部在不同的服務(wù)器上,例如一個(gè)登陸系統(tǒng)部在不同的服務(wù)器上.
- 分布式是不同的系統(tǒng)部在不同的服務(wù)器上,服務(wù)器之間相互調(diào)用.
小飯店原來(lái)只有一個(gè)廚師,切菜洗菜備料炒菜全干。后來(lái)客人多了,廚房一個(gè)廚師忙不過(guò)來(lái),又請(qǐng)了個(gè)廚師,兩個(gè)廚師都能炒一樣的菜,這兩個(gè)廚師的關(guān)系是集群。為了讓廚師專心炒菜,把菜做到極致,又請(qǐng)了個(gè)配菜師負(fù)責(zé)切菜,備菜,備料,廚師和配菜師的關(guān)系是分布式,一個(gè)配菜師也忙不過(guò)來(lái)了,又請(qǐng)了個(gè)配菜師,兩個(gè)配菜師關(guān)系是集群.
2 搭建分布式項(xiàng)目
準(zhǔn)備工具:eclipse,裝有CentOS7系統(tǒng)的VMwarm,zookeeper.......最重要的,一臺(tái)三年高齡的老人機(jī).
1 首先創(chuàng)建一個(gè)父類的maven項(xiàng)目,打包方式為pom.
在eclipse中創(chuàng)建一個(gè)父類maven項(xiàng)目,打包方式為pom.為什么要?jiǎng)?chuàng)建一個(gè)父類的maven項(xiàng)目呢?因?yàn)橐褂眠@個(gè)maven項(xiàng)目進(jìn)行各個(gè)jar包版本的管理,子類想要jar包直接跟父類要就可以. 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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.itqf</groupId> <artifactId>sping-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- 定義所有jar包的版本 --> <properties> <junit.version>4.12</junit.version> <spring.version>4.2.4.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <mybatis.paginator.version>1.2.15</mybatis.paginator.version> <mysql.version>5.1.32</mysql.version> <slf4j.version>1.6.4</slf4j.version> <jackson.version>2.4.2</jackson.version> <druid.version>1.0.9</druid.version> <httpclient.version>4.3.5</httpclient.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.0</jsp-api.version> <joda-time.version>2.5</joda-time.version> <commons-lang3.version>3.3.2</commons-lang3.version> <commons-io.version>1.3.2</commons-io.version> <commons-net.version>3.3</commons-net.version> <pagehelper.version>3.4.2-fix</pagehelper.version> <jsqlparser.version>0.9.1</jsqlparser.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <jedis.version>2.7.2</jedis.version> <solrj.version>4.10.3</solrj.version> <dubbo.version>2.5.3</dubbo.version> <zookeeper.version>3.4.7</zookeeper.version> <zkclient.version>0.1</zkclient.version> <activemq.version>5.11.2</activemq.version> <freemarker.version>2.3.23</freemarker.version> <quartz.version>2.2.2</quartz.version> </properties> <!-- 管理所有項(xiàng)目中用到的jar包,并不做真正的依賴 --> <dependencyManagement> <dependencies> <!-- 時(shí)間操作組件 --> <dependency> <groupId>joda-time</groupId>
2 創(chuàng)建一個(gè)maven的聚合工程.
2.1 創(chuàng)建maven聚合工程,繼承父工程.
聚合工程:可以將項(xiàng)目中的controller層,view層等都獨(dú)立成一個(gè)工程,最終運(yùn)行的時(shí)候整合到一起運(yùn)行.
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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itqf</groupId> <artifactId>sping-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.itqf</groupId> <artifactId>sping-manager</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <dependencies> <dependency> <groupId>com.itqf</groupId> <artifactId>sping-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8083</port> <path>/</path> </configuration> </plugin> </plugins> </build> <modules> <module>sping-manager-pojo</module> <module>sping-manager-interface</module> <module>sping-manager-service</module> <module>sping-manager-mapper</module> </modules> </project>
2.2 在聚合工程中創(chuàng)建maven Module,命名sping-manager-pojo(實(shí)體類層).
pojo是一個(gè)普通的jar格式,不需要依賴父工程.
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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itqf</groupId> <artifactId>sping-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>sping-manager-pojo</artifactId> </project>
2.3 在聚合工程中創(chuàng)建maven Module,命名sping-manager-mapper(dao層). 在pom.xml文件中依賴pojo.因?yàn)閙apper層的方法返回的是一個(gè)實(shí)體類對(duì)象的話,那么需要用到pojo.
導(dǎo)入依賴jar包.
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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itqf</groupId> <artifactId>sping-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>sping-manager-mapper</artifactId> <!-- 依賴pojo --> <dependencies> <dependency> <groupId>com.itqf</groupId> <artifactId>sping-manager-pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> <version>${mybatis.paginator.version}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- 連接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> </dependencies> </project>
2.4 在聚合工程中創(chuàng)建sping-manager-interface(接口),將所有的service接口都放到獨(dú)立的工程當(dāng)中. 接口中方法返回值如果是實(shí)體類,需要用到pojo.所以在pom中依賴pojo 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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itqf</groupId> <artifactId>sping-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>sping-manager-interface</artifactId> <dependencies> <dependency> <groupId>com.itqf</groupId> <artifactId>sping-manager-pojo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
2.5 在聚合項(xiàng)目中創(chuàng)建sping-manager-service(interface的實(shí)現(xiàn)類).打包方式為war
因?yàn)閷ontroller層與service層分開(kāi)了,所以在運(yùn)行啟動(dòng)的時(shí)候要講controller和service單獨(dú)使用tomcat發(fā)布,將聚合工程中所需要的配置文件都放入service中,這樣在Tomcat啟動(dòng)的時(shí)候回將配置文件都進(jìn)行加載整合.
- service需要用到接口,所以依賴接口sping-manager-interface.
- service需要用到pojo,也需要調(diào)用到mapper,所以直接依賴mapper就可以,以為mapper已經(jīng)依賴了pojo (依賴傳遞) .
- service需要被spring管理,讓spring給service創(chuàng)建對(duì)象
- service需要dubbo的包(后面對(duì)dubbo進(jìn)行介紹)
- service需要使用到SOA,將service當(dāng)成一個(gè)服務(wù)發(fā)布出去.
配置文件
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
db.properties
db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=UTF-8 db.username=root db.password=root
applicationContext-tx.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="adviceId" transaction-manager="txManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="adviceId" pointcut="execution(* com.itqf.service..*.*(..))"/> </aop:config> </beans>
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:property-placeholder location="classpath:resource/*.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${db.driver}"></property> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> <property name="maxActive" value="10"></property> <property name="minIdle" value="5"></property> </bean> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itqf.mapper"></property> </bean> </beans>
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:component-scan base-package="com.itqf.service"></context:component-scan> </beans>
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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.qianfeng</groupId> <artifactId>sping-manager</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>sping-manager-service</artifactId> <packaging>war</packaging> <dependencies> <dependency> <groupId>com.qianfeng</groupId> <artifactId>sping-manager-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.qianfeng</groupId> <artifactId>sping-manager-mapper</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project>
最后工程結(jié)構(gòu)
3 使用dubbo將service發(fā)布服務(wù)
首先思考?
像淘寶京東這樣的商城類網(wǎng)站,不但可以從PC端登錄,還能從手機(jī)端登錄,那么是怎么實(shí)現(xiàn)的?寫兩個(gè)controller?那肯定不會(huì),誰(shuí)會(huì)閑著沒(méi)事去找事情做,那么這就需要使用到SOA(面向服務(wù)的架構(gòu)).那么我們?cè)趯懸粋€(gè)項(xiàng)目使用分布式的時(shí)候,會(huì)有很多系統(tǒng)來(lái)相互調(diào)用,如果來(lái)回調(diào)用會(huì)使代碼結(jié)構(gòu)非?;靵y.這里我們使用dubbo來(lái)管理,解決發(fā)布服務(wù)太多,搞不清楚的問(wèn)題.
什么是SOA
SOA是一種設(shè)計(jì)方法,其中包含多個(gè)服務(wù),而服務(wù)之間通過(guò)配合最終會(huì)提供一系列功能。一個(gè)服務(wù)通常以獨(dú)立的形式存在于操作系統(tǒng)進(jìn)程中。服務(wù)之間通過(guò)網(wǎng)絡(luò)調(diào)用,而非采用進(jìn)程內(nèi)調(diào)用的方式進(jìn)行通信。
比如你現(xiàn)在有很多服務(wù):新聞服務(wù)(提供新聞的發(fā)布,查看,修改,刪除),訂單服務(wù)(訂單添加,訂單修改,訂單查看,訂單刪除等)財(cái)務(wù)服務(wù)(收入,支出,統(tǒng)計(jì)等等)員工服務(wù)(新增,修改,查看,統(tǒng)計(jì))考勤服務(wù)(簽到,簽退,導(dǎo)出,統(tǒng)計(jì)等)銷售服務(wù)(賣出上報(bào),銷售統(tǒng)計(jì)。)
dubbo
什么是dubbo(是資源調(diào)度和治理中心的管理工具)
隨著互聯(lián)網(wǎng)的發(fā)展,網(wǎng)站應(yīng)用的規(guī)模不斷擴(kuò)大,常規(guī)的垂直應(yīng)用架構(gòu)已無(wú)法應(yīng)對(duì),分布式服務(wù)架構(gòu)以及流動(dòng)計(jì)算架構(gòu)勢(shì)在必行,亟需一個(gè)治理系統(tǒng)確保架構(gòu)有條不紊的演進(jìn)。
單一應(yīng)用架構(gòu)
- 當(dāng)網(wǎng)站流量很小時(shí),只需一個(gè)應(yīng)用,將所有功能都部署在一起,以減少部署節(jié)點(diǎn)和成本。
- 此時(shí),用于簡(jiǎn)化增刪改查工作量的 數(shù)據(jù)訪問(wèn)框架(ORM) 是關(guān)鍵。
垂直應(yīng)用架構(gòu)
- 當(dāng)訪問(wèn)量逐漸增大,單一應(yīng)用增加機(jī)器帶來(lái)的加速度越來(lái)越小,將應(yīng)用拆成互不相干的幾個(gè)應(yīng)用,以提升效率。
- 此時(shí),用于加速前端頁(yè)面開(kāi)發(fā)的 Web框架(MVC) 是關(guān)鍵。
分布式服務(wù)架構(gòu)
- 當(dāng)垂直應(yīng)用越來(lái)越多,應(yīng)用之間交互不可避免,將核心業(yè)務(wù)抽取出來(lái),作為獨(dú)立的服務(wù),逐漸形成穩(wěn)定的服務(wù)中心,使前端應(yīng)用能更快速的響應(yīng)多變的市場(chǎng)需求。
- 此時(shí),用于提高業(yè)務(wù)復(fù)用及整合的 分布式服務(wù)框架(RPC) 是關(guān)鍵。
流動(dòng)計(jì)算架構(gòu)
- 當(dāng)服務(wù)越來(lái)越多,容量的評(píng)估,小服務(wù)資源的浪費(fèi)等問(wèn)題逐漸顯現(xiàn),此時(shí)需增加一個(gè)調(diào)度中心基于訪問(wèn)壓力實(shí)時(shí)管理集群容量,提高集群利用率。
- 此時(shí),用于提高機(jī)器利用率的 資源調(diào)度和治理中心(SOA) 是關(guān)鍵。
Dubbo環(huán)境的搭建:
節(jié)點(diǎn)角色說(shuō)明:
- Provider: 暴露服務(wù)的服務(wù)提供方。
- Consumer: 調(diào)用遠(yuǎn)程服務(wù)的服務(wù)消費(fèi)方。
- Registry: 服務(wù)注冊(cè)與發(fā)現(xiàn)的注冊(cè)中心。
- Monitor: 統(tǒng)計(jì)服務(wù)的調(diào)用次數(shù)和調(diào)用時(shí)間的監(jiān)控中心。
- Container: 服務(wù)運(yùn)行容器。
調(diào)用關(guān)系說(shuō)明:
- 服務(wù)容器負(fù)責(zé)啟動(dòng),加載,運(yùn)行服務(wù)提供者。
- 服務(wù)提供者在啟動(dòng)時(shí),向注冊(cè)中心注冊(cè)自己提供的服務(wù)。
- 服務(wù)消費(fèi)者在啟動(dòng)時(shí),向注冊(cè)中心訂閱自己所需的服務(wù)。
- 注冊(cè)中心返回服務(wù)提供者地址列表給消費(fèi)者,如果有變更,注冊(cè)中心將基于長(zhǎng)連接推送變更數(shù)據(jù)給消費(fèi)者。
- 服務(wù)消費(fèi)者,從提供者地址列表中,基于軟負(fù)載均衡算法,選一臺(tái)提供者進(jìn)行調(diào)用,如果調(diào)用失敗,再選另一臺(tái)調(diào)用。 服務(wù)消費(fèi)者和提供者,在內(nèi)存中累計(jì)調(diào)用次數(shù)和調(diào)用時(shí)間,定時(shí)每分鐘發(fā)送一次統(tǒng)計(jì)數(shù)據(jù)到監(jiān)控中心。
這里我們主要來(lái)部注冊(cè)中心(Registry),我們使用Zookeeper來(lái)充當(dāng)注冊(cè)中心.
在linux環(huán)境下部署注冊(cè)中心,Zookeeper
第一步當(dāng)然是開(kāi)虛擬機(jī)啦,我還是在CentOS7中來(lái)搞.
上網(wǎng)上搞一個(gè)Zookeeper的壓縮包,我的是zookeeper-3.4.6.tar.gz
粘貼到/opt目錄下,解壓.(需要jdk環(huán)境,如果沒(méi)有jdk環(huán)境先安裝一個(gè)jdk)
進(jìn)入zookeeper-3.4.6目錄,創(chuàng)建一個(gè)叫data的文件夾。
把./conf目錄下的zoo_sample.cfg改名為zoo.cfg 修改zoo.cfg中的data屬性:dataDir=/opt/zookeeper-3.4.6/data
第七步:
- 啟動(dòng)zookeeper.
- 啟動(dòng):./zkServer.sh start
- 關(guān)閉:./zkServer.sh stop
- 查看狀態(tài):./zkServer.sh status
注意zookeeper啟動(dòng)后一定要將防火墻關(guān)閉!!!這樣就搞定啦.
在service的applicationContext-service.xml中添加配置文件進(jìn)行發(fā)布服務(wù)
<!-- 使用dubbo發(fā)布服務(wù) --> <!-- 指明服務(wù)所在的工程 --> <dubbo:application name="sping-manager"/> <!-- 指明注冊(cè)中心 adress地址是linux中的ip地址加上端口號(hào),zookeeper的默認(rèn)端口號(hào)是2181 --> <dubbo:registry protocol="zookeeper" address="10.0.117.198:2181" ></dubbo:registry> <!-- 把服務(wù)暴露在某個(gè)端口 port是端口號(hào),選擇一個(gè)沒(méi)有被占用的端口 --> <dubbo:protocol name="dubbo" port="20888"></dubbo:protocol> <!-- 發(fā)布服務(wù),ref是Spring容器創(chuàng)建的Service對(duì)象的名稱 --> <dubbo:service interface="com.itqf.service.ItemService" ref="itemServiceImpl" timeout="6000000"></dubbo:service>
在service的pom.xml中導(dǎo)入包
<!-- dubbo相關(guān) --> <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>
4 創(chuàng)建一個(gè)sping-manager-controller,與聚合項(xiàng)目平級(jí).導(dǎo)入依賴.
contoller需要使用dubbo來(lái)訪問(wèn)service層發(fā)布的服務(wù).要使用Tomcat服務(wù)器進(jìn)行發(fā)布,當(dāng)然還需要用到springmvc.
xml
<dependencies> <!-- 只需依賴業(yè)務(wù)的接口 --> <dependency> <groupId>com.qianfeng</groupId> <artifactId>sping-manager-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- JSP相關(guān) --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <scope>provided</scope> </dependency> <!-- dubbo相關(guān) --> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8081</port> <path>/</path> </configuration> </plugin> </plugins> </build>
spingmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:component-scan base-package="com.itqf.controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--指明所在工程 --> <dubbo:application name="sping-manager-controller"/> <!-- 指明注冊(cè)中心 --> <dubbo:registry protocol="zookeeper" address="10.0.117.198:2181"></dubbo:registry> <!--調(diào)用服務(wù) --> <dubbo:reference interface="com.itqf.service.ItemService" id="itemService"></dubbo:reference> </beans>
結(jié)構(gòu)圖:
5 創(chuàng)建sping-common
這個(gè)是我需要用到的一個(gè)專門放工具的地方,這里用來(lái)放一些公共需要的東西; 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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itqf</groupId> <artifactId>sping-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.itqf</groupId> <artifactId>sping-common</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- 時(shí)間操作組件 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>${joda-time.version}</version> </dependency> <!-- Apache工具組件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>${commons-net.version}</version> </dependency> <!-- Jackson Json處理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> </dependencies> </project>
6 測(cè)試
好啦,這樣一個(gè)偽分布式項(xiàng)目就搭建好了,接下來(lái)非常重要的一點(diǎn)就是需要將父工程,sping-manager的聚合工程,sping-common都install一下放到本地倉(cāng)庫(kù)中,否則的話在啟動(dòng)項(xiàng)目的時(shí)候會(huì)報(bào)錯(cuò)說(shuō)找不到父工程或其他工程.
最終工程圖:
總結(jié):
經(jīng)過(guò)幾個(gè)小時(shí)的艱苦奮戰(zhàn)終于搭建好了,都要給我的老人機(jī)累炸了.如果有什么錯(cuò)誤或不足之處請(qǐng)不吝之處.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot2.x 整合 thumbnailator 圖片處理的示例代碼
這篇文章主要介紹了SpringBoot2.x 之整合 thumbnailator 圖片處理,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10java中SpringBoot?自動(dòng)裝配的原理分析
這篇文章主要介紹了SpringBoot?自動(dòng)裝配的原理分析的相關(guān)資料,需要的朋友可以參考下2022-12-12使用jekins自動(dòng)構(gòu)建部署java maven項(xiàng)目的方法步驟
這篇文章主要介紹了使用jekins自動(dòng)構(gòu)建部署java maven項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java SpringBoot實(shí)現(xiàn)帶界面的代碼生成器詳解
這篇文章主要介紹了Java SpringBoot如何實(shí)現(xiàn)帶界面的代碼生成器,幫助大家更好的理解和使用Java SpringBoot編程語(yǔ)言,感興趣的朋友可以了解下2021-09-09Spring MVC傳遞接收參數(shù)方式小結(jié)
大家在開(kāi)發(fā)中經(jīng)常會(huì)用到Spring MVC Controller來(lái)接收請(qǐng)求參數(shù),主要常用的接收方式就是通過(guò)實(shí)體對(duì)象以及形參等方式、有些用于GET請(qǐng)求,有些用于POST請(qǐng)求,有些用于兩者,下面介紹幾種常見(jiàn)的Spring MVC傳遞接收參數(shù)的方式2021-11-11Spring Boot Cache使用方法整合代碼實(shí)例
這篇文章主要介紹了Spring Boot Cache使用方法整合代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Java實(shí)現(xiàn)布隆過(guò)濾器的幾種方式總結(jié)
這篇文章給大家總結(jié)了幾種Java實(shí)現(xiàn)布隆過(guò)濾器的方式,手動(dòng)硬編碼實(shí)現(xiàn),引入Guava實(shí)現(xiàn),引入hutool實(shí)現(xiàn),通過(guò)redis實(shí)現(xiàn)等幾種方式,文中有詳細(xì)的代碼和圖解,需要的朋友可以參考下2023-07-07解決Spring Batch框架job任務(wù)只跑一次的問(wèn)題
這篇文章主要介紹了解決Spring Batch框架job任務(wù)只跑一次的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java實(shí)戰(zhàn)練習(xí)之撲克牌魔術(shù)
這篇文章主要介紹了Java實(shí)戰(zhàn)練習(xí)之撲克牌魔術(shù),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-04-04