国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

java實現(xiàn)分布式項目搭建的方法

2024-07-14 08:40:17
字體:
供稿:網(wǎng)友

1 分布式

1.1 什么是分布式

  1. 分布式系統(tǒng)一定是由多個節(jié)點組成的系統(tǒng)。其中,節(jié)點指的是計算機(jī)服務(wù)器,而且這些節(jié)點一般不是孤立的,而是互通的。
  2. 這些連通的節(jié)點上部署了我們的節(jié)點,并且相互的操作會有協(xié)同。分布式系統(tǒng)對于用戶而言,他們面對的就是一個服務(wù)器,提供用戶需要的服務(wù)而已,而實際上這些服務(wù)是通過背后的眾多服務(wù)器組成的一個分布式系統(tǒng),因此分布式系統(tǒng)看起來像是一個超級計算機(jī)一樣。

1.2 分布式與集群的區(qū)別

  1.  集群是同一個系統(tǒng)部在不同的服務(wù)器上,例如一個登陸系統(tǒng)部在不同的服務(wù)器上.
  2. 分布式是不同的系統(tǒng)部在不同的服務(wù)器上,服務(wù)器之間相互調(diào)用.

小飯店原來只有一個廚師,切菜洗菜備料炒菜全干。后來客人多了,廚房一個廚師忙不過來,又請了個廚師,兩個廚師都能炒一樣的菜,這兩個廚師的關(guān)系是集群。為了讓廚師專心炒菜,把菜做到極致,又請了個配菜師負(fù)責(zé)切菜,備菜,備料,廚師和配菜師的關(guān)系是分布式,一個配菜師也忙不過來了,又請了個配菜師,兩個配菜師關(guān)系是集群.

2 搭建分布式項目

準(zhǔn)備工具:eclipse,裝有CentOS7系統(tǒng)的VMwarm,zookeeper.......最重要的,一臺三年高齡的老人機(jī).

1 首先創(chuàng)建一個父類的maven項目,打包方式為pom.

在eclipse中創(chuàng)建一個父類maven項目,打包方式為pom.為什么要創(chuàng)建一個父類的maven項目呢?因為要使用這個maven項目進(jìn)行各個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>	 <!-- 管理所有項目中用到的jar包,并不做真正的依賴 -->	 <dependencyManagement>	  <dependencies>			<!-- 時間操作組件 -->			<dependency>				<groupId>joda-time</groupId>

2 創(chuàng)建一個maven的聚合工程.

2.1 創(chuàng)建maven聚合工程,繼承父工程.

聚合工程:可以將項目中的controller層,view層等都獨立成一個工程,最終運行的時候整合到一起運行.

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(實體類層).

pojo是一個普通的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.因為mapper層的方法返回的是一個實體類對象的話,那么需要用到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āng)中. 接口中方法返回值如果是實體類,需要用到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 在聚合項目中創(chuàng)建sping-manager-service(interface的實現(xiàn)類).打包方式為war

因為將controller層與service層分開了,所以在運行啟動的時候要講controller和service單獨使用tomcat發(fā)布,將聚合工程中所需要的配置文件都放入service中,這樣在Tomcat啟動的時候回將配置文件都進(jìn)行加載整合.

  1. service需要用到接口,所以依賴接口sping-manager-interface.
  2. service需要用到pojo,也需要調(diào)用到mapper,所以直接依賴mapper就可以,以為mapper已經(jīng)依賴了pojo (依賴傳遞) .
  3. service需要被spring管理,讓spring給service創(chuàng)建對象
  4. service需要dubbo的包(后面對dubbo進(jìn)行介紹)
  5. service需要使用到SOA,將service當(dāng)成一個服務(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.Driverdb.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=UTF-8db.username=rootdb.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)

 

 
java,分布式項目,java分布式搭建,java分布式項目搭建 

 

3 使用dubbo將service發(fā)布服務(wù)

首先思考?

像淘寶京東這樣的商城類網(wǎng)站,不但可以從PC端登錄,還能從手機(jī)端登錄,那么是怎么實現(xiàn)的?寫兩個controller?那肯定不會,誰會閑著沒事去找事情做,那么這就需要使用到SOA(面向服務(wù)的架構(gòu)).那么我們在寫一個項目使用分布式的時候,會有很多系統(tǒng)來相互調(diào)用,如果來回調(diào)用會使代碼結(jié)構(gòu)非常混亂.這里我們使用dubbo來管理,解決發(fā)布服務(wù)太多,搞不清楚的問題.

什么是SOA

SOA是一種設(shè)計方法,其中包含多個服務(wù),而服務(wù)之間通過配合最終會提供一系列功能。一個服務(wù)通常以獨立的形式存在于操作系統(tǒng)進(jìn)程中。服務(wù)之間通過網(wǎng)絡(luò)調(diào)用,而非采用進(jìn)程內(nèi)調(diào)用的方式進(jìn)行通信。

比如你現(xiàn)在有很多服務(wù):新聞服務(wù)(提供新聞的發(fā)布,查看,修改,刪除),訂單服務(wù)(訂單添加,訂單修改,訂單查看,訂單刪除等)財務(wù)服務(wù)(收入,支出,統(tǒng)計等等)員工服務(wù)(新增,修改,查看,統(tǒng)計)考勤服務(wù)(簽到,簽退,導(dǎo)出,統(tǒng)計等)銷售服務(wù)(賣出上報,銷售統(tǒng)計。)

dubbo

什么是dubbo(是資源調(diào)度和治理中心的管理工具)

隨著互聯(lián)網(wǎng)的發(fā)展,網(wǎng)站應(yīng)用的規(guī)模不斷擴(kuò)大,常規(guī)的垂直應(yīng)用架構(gòu)已無法應(yīng)對,分布式服務(wù)架構(gòu)以及流動計算架構(gòu)勢在必行,亟需一個治理系統(tǒng)確保架構(gòu)有條不紊的演進(jìn)。

 

 
java,分布式項目,java分布式搭建,java分布式項目搭建 

 

單一應(yīng)用架構(gòu)

  1.  當(dāng)網(wǎng)站流量很小時,只需一個應(yīng)用,將所有功能都部署在一起,以減少部署節(jié)點和成本。
  2. 此時,用于簡化增刪改查工作量的 數(shù)據(jù)訪問框架(ORM) 是關(guān)鍵。

垂直應(yīng)用架構(gòu)

  1.  當(dāng)訪問量逐漸增大,單一應(yīng)用增加機(jī)器帶來的加速度越來越小,將應(yīng)用拆成互不相干的幾個應(yīng)用,以提升效率。
  2. 此時,用于加速前端頁面開發(fā)的 Web框架(MVC) 是關(guān)鍵。

分布式服務(wù)架構(gòu)

  1.  當(dāng)垂直應(yīng)用越來越多,應(yīng)用之間交互不可避免,將核心業(yè)務(wù)抽取出來,作為獨立的服務(wù),逐漸形成穩(wěn)定的服務(wù)中心,使前端應(yīng)用能更快速的響應(yīng)多變的市場需求。
  2. 此時,用于提高業(yè)務(wù)復(fù)用及整合的 分布式服務(wù)框架(RPC) 是關(guān)鍵。

流動計算架構(gòu)

  1.  當(dāng)服務(wù)越來越多,容量的評估,小服務(wù)資源的浪費等問題逐漸顯現(xiàn),此時需增加一個調(diào)度中心基于訪問壓力實時管理集群容量,提高集群利用率。
  2. 此時,用于提高機(jī)器利用率的 資源調(diào)度和治理中心(SOA) 是關(guān)鍵。

Dubbo環(huán)境的搭建:

 

 

java,分布式項目,java分布式搭建,java分布式項目搭建

節(jié)點角色說明:

  1.  Provider: 暴露服務(wù)的服務(wù)提供方。
  2. Consumer: 調(diào)用遠(yuǎn)程服務(wù)的服務(wù)消費方。
  3. Registry: 服務(wù)注冊與發(fā)現(xiàn)的注冊中心。
  4. Monitor: 統(tǒng)計服務(wù)的調(diào)用次數(shù)和調(diào)用時間的監(jiān)控中心。
  5. Container: 服務(wù)運行容器。

調(diào)用關(guān)系說明:

  1. 服務(wù)容器負(fù)責(zé)啟動,加載,運行服務(wù)提供者。
  2. 服務(wù)提供者在啟動時,向注冊中心注冊自己提供的服務(wù)。
  3. 服務(wù)消費者在啟動時,向注冊中心訂閱自己所需的服務(wù)。
  4. 注冊中心返回服務(wù)提供者地址列表給消費者,如果有變更,注冊中心將基于長連接推送變更數(shù)據(jù)給消費者。
  5. 服務(wù)消費者,從提供者地址列表中,基于軟負(fù)載均衡算法,選一臺提供者進(jìn)行調(diào)用,如果調(diào)用失敗,再選另一臺調(diào)用。 服務(wù)消費者和提供者,在內(nèi)存中累計調(diào)用次數(shù)和調(diào)用時間,定時每分鐘發(fā)送一次統(tǒng)計數(shù)據(jù)到監(jiān)控中心。

這里我們主要來部注冊中心(Registry),我們使用Zookeeper來充當(dāng)注冊中心.

在linux環(huán)境下部署注冊中心,Zookeeper

第一步當(dāng)然是開虛擬機(jī)啦,我還是在CentOS7中來搞.

上網(wǎng)上搞一個Zookeeper的壓縮包,我的是zookeeper-3.4.6.tar.gz

粘貼到/opt目錄下,解壓.(需要jdk環(huán)境,如果沒有jdk環(huán)境先安裝一個jdk)

進(jìn)入zookeeper-3.4.6目錄,創(chuàng)建一個叫data的文件夾。

把./conf目錄下的zoo_sample.cfg改名為zoo.cfg 修改zoo.cfg中的data屬性:dataDir=/opt/zookeeper-3.4.6/data

第七步:

  1. 啟動zookeeper.
  2. 啟動:./zkServer.sh start
  3. 關(guān)閉:./zkServer.sh stop
  4. 查看狀態(tài):./zkServer.sh status

注意zookeeper啟動后一定要將防火墻關(guān)閉!!!這樣就搞定啦.

在service的applicationContext-service.xml中添加配置文件進(jìn)行發(fā)布服務(wù)

<!-- 使用dubbo發(fā)布服務(wù) -->  <!-- 指明服務(wù)所在的工程 -->  <dubbo:application name="sping-manager"/>  <!-- 指明注冊中心 adress地址是linux中的ip地址加上端口號,zookeeper的默認(rèn)端口號是2181 --> <dubbo:registry protocol="zookeeper" address="10.0.117.198:2181" ></dubbo:registry>   <!-- 把服務(wù)暴露在某個端口 port是端口號,選擇一個沒有被占用的端口 -->  <dubbo:protocol name="dubbo" port="20888"></dubbo:protocol>   <!-- 發(fā)布服務(wù),ref是Spring容器創(chuàng)建的Service對象的名稱 --> 			  <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)建一個sping-manager-controller,與聚合項目平級.導(dǎo)入依賴.

contoller需要使用dubbo來訪問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"/>    <!-- 指明注冊中心 -->  <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)圖:

java,分布式項目,java分布式搭建,java分布式項目搭建

5 創(chuàng)建sping-common

這個是我需要用到的一個專門放工具的地方,這里用來放一些公共需要的東西; 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>			<!-- 時間操作組件 -->			<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 測試

好啦,這樣一個偽分布式項目就搭建好了,接下來非常重要的一點就是需要將父工程,sping-manager的聚合工程,sping-common都install一下放到本地倉庫中,否則的話在啟動項目的時候會報錯說找不到父工程或其他工程.

最終工程圖:

java,分布式項目,java分布式搭建,java分布式項目搭建

總結(jié):

經(jīng)過幾個小時的艱苦奮戰(zhàn)終于搭建好了,都要給我的老人機(jī)累炸了.如果有什么錯誤或不足之處請不吝之處.

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 和政县| 九江县| 屏东县| 花莲市| 祁门县| 哈尔滨市| 宝兴县| 博客| 大厂| 分宜县| 南充市| 瓦房店市| 阜平县| 墨竹工卡县| 云霄县| 新竹市| 临颍县| 阿合奇县| 南充市| 临海市| 焦作市| 彭水| 中卫市| 宁河县| 兰西县| 四子王旗| 静海县| 汤阴县| 墨脱县| 陵川县| 芦山县| 乐东| 岳阳市| 伽师县| 齐河县| 赣州市| 罗山县| 海伦市| 城口县| 穆棱市| 阿克|