java分布式项目如何搭建?java分布式系统架构实例

分布式系统一定是由多个节点组成的系统,是不同的系统部在不同的服务器上,服务器之间相互调用。那java分布式项目如何搭建?下面来我们就来给大家讲解一下这方面的内容。

准备工具:eclipse,装有CentOS7系统的VMwarm,zookeeper.......最重要的,一台三年高龄的老人机.

1.首先创建一个父类的maven项目,打包方式为pom.

在eclipse中创建一个父类maven项目,打包方式为pom.为什么要创建一个父类的maven项目呢?因为要使用这个maven项目进行各个jar包版本的管理,子类想要jar包直接跟父类要就可以.

<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 .创建一个maven的聚合工程.

2.1 创建maven聚合工程,继承父工程.

聚合工程:可以将项目中的controller层,view层等都独立成一个工程,最终运行的时候整合到一起运行.

<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 在聚合工程中创建maven Module,命名sping-manager-pojo(实体类层).

pojo是一个普通的jar格式,不需要依赖父工程.

<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 在聚合工程中创建maven Module,命名sping-manager-mapper(dao层). 在pom.xml文件中依赖pojo.因为mapper层的方法返回的是一个实体类对象的话,那么需要用到pojo.

导入依赖jar包.

<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 在聚合工程中创建sping-manager-interface(接口),将所有的service接口都放到独立的工程当中. 接口中方法返回值如果是实体类,需要用到pojo.所以在pom中依赖pojo

<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 在聚合项目中创建sping-manager-service(interface的实现类).打包方式为war

因为将controller层与service层分开了,所以在运行启动的时候要讲controller和service单独使用tomcat发布,将聚合工程中所需要的配置文件都放入service中,这样在Tomcat启动的时候回将配置文件都进行加载整合.

service需要用到接口,所以依赖接口sping-manager-interface.

service需要用到pojo,也需要调用到mapper,所以直接依赖mapper就可以,以为mapper已经依赖了pojo (依赖传递) .

service需要被spring管理,让spring给service创建对象

service需要dubbo的包(后面对dubbo进行介绍)

service需要使用到SOA,将service当成一个服务发布出去.

配置文件

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>

最后工程结构

java分布式项目如何搭建?java分布式系统架构实例

以上就是java实现分布式项目搭建的方法,从文中看,操作起来还是比较复杂的,不过只要多研究多练习就会熟悉了。最后大家如果想要了解更多java实例知识,敬请关注奇Q工具网。

推荐阅读:

java标识符如何定义?标识符如何命名?

java打不开jnlp文件怎么办?jnlp文件是什么?

springcloud项目实例分享