您的位置:首页 > 运维架构 > 网站架构

Maven多模块+dubbo+zookeeper分布式架构搭建SSM项目

2017-12-28 16:48 736 查看
Maven多模块的构建:
一、总体结构预览
|----parent模块
    |-----manage聚合工程模块
    |       |------pojo类
    |       |------dao层
    |       |------service接口层
    |       |------service实现层
    |-----common公共组件
    |-----web表现层
Parent父类模块:集中统一管理项目中需要jar包的版本号,打包方式pom;
Manage聚合工程模块:继承parent模块,集中管理pojo、dao、serivice各层组件,manage模块打包方式为pom包;
pojo继承manage模块,存放实体类,打包为jar;
dao继承manage模块,负责与数据库交互,打包为jar;
service接口继承manager模块,提供服务模块的抽象接口,为暴露对外访问接口,打包为jar;
service实现层继承manage模块,用来实现接口,实现业务逻辑,打包为war;
common公共组件模块:继承parent模块,管理公共组件,如log4j等,打包为jar;
web表现层直接继承父类,也为控制器层,打包为war包,通过网络通信与servic接口链接,实现远程访问;下面开始抛出实战代码演示。
二,业务代码的具体实现,本项目搭建为SSM框架:
    (1).parent模块中jar包管理
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ssm</groupId>
 <artifactId>ssm-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
       <!-- 1.管理jar包的版本 -->
       <properties>
                <--这里放入jar包版本,维护室只需要更新该这里的jar包版本,由于继承关系,子类模块直接更新-->
               <zookeeper.version>3.4.7</zookeeper.version>
        </properties>
        <!-- 2.添加加jar包 -->
        <dependencyManagement>
               <dependencies>
                       <dependence>
                           
这里放入依赖文件的坐标,版本用el表达式即可
                        <dependence>
               </dependencies>
        </dependencyManagement>
        <build>
               <finalName>${project.artifactId}</finalName>
                <!--
配置插件 -->
    <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
                       <artifactId>maven-compiler-plugin</artifactId>
                       <version>3.1</version>
                       <configuration>
                               <!--
配置jdk的版本 -->
                               <source>1.8</source>
                               <target>1.8</target>
                               <encoding>UTF-8</encoding>
                       </configuration>
       </plugin>
       <plugin>
           <groupId>org.apache.tomcat.maven</groupId>
                  <artifactId>tomcat7-maven-plugin</artifactId>
                       <version>2.2</version>
                       <configuration>
                               <port>8080</port>
                               <uriEncoding>UTF-8</uriEncoding>
                               <server>tomcat7</server>
                       </configuration>
       </plugin>
    </plugins>
        </build>
</project>
(2).common公共组件模块:将需要的jar包从父类依赖进自己的pom.xml中
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.ssm</groupId>
   <artifactId>ssm-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.ssm</groupId>
 <artifactId>ssm-common</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 

  <dependencies>
        <denpendence>
              
这里写入依赖的文件jar包坐标
        <dependence>  

  </dependencies>
</project>
(3).manage模块,这个时项目整体业务逻辑的核心点,建立的manage为聚合工程,在manage聚合工程中建立model模块,分别为pojo,dao,service,一级serviceInterface,
模块建完后会在父类工成manage显示如下:
  <modules>
        <module>ssm-dao</module>
        <module>ssm-service</module>
        <module>ssm-pojo</module>
        <module>ssm-interface</module>
  </modules>
当服务集群规模进一步扩大,带动IT治理结构进一步升级,需要实现动态部署,进行流动计算,现有分布式服务架构不会带来阻力:
 <!--
添加依赖,依赖common组件 -->
  <dependencies>
    <dependency>
       <groupId>com.ssm</groupId>
      <artifactId>ssm-common</artifactId>
       <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
 

  <!--
添加编译插件 -->
  <build>
     <plugins>
         <!-- tomcat插件 -->
         <plugin>
               <groupId>org.apache.tomcat.maven</groupId>
                               <artifactId>tomcat7-maven-plugin</artifactId>
                               <version>2.2</version>
                               <configuration>
                                      <port>8080</port>
                                      <path>/</path>
                               </configuration>
         </plugin>
         <!-- jdk插件 -->
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                         <version>3.1</version>
                         <configuration>
                                      <!--
配置jdk的版本 -->
                                      <source>1.8</source>
                                      <target>1.8</target>
                                      <encoding>UTF-8</encoding>
                         </configuration>
         </plugin>
     </plugins>
  </build>
这里不在赘述manage下面的子模块的建立,只介绍下最重要的service,在service层里面需要整合spring和mybatis框架,所以这里搭建框架的步骤也不再叙述,先看下结构:



直接上配置文件:其中有dubbo相关配置文件,这里先写进去,一会慢慢介绍
<!-- 1.配置数据库连接池 -->
        <!-- 加载配置文件 -->
        <context:property-placeholderlocation="classpath:properties/db.properties" />
        <!-- 数据库连接池 -->
        <bean id="dataSource"class="com.alibaba.druid.pool.DruidDataSource"
               destroy-method="close">
               <property name="url"value="${jdbc.url}" />
               <propertyname="username" value="${jdbc.username}" />
               <propertyname="password" value="${jdbc.password}" />
               <propertyname="driverClassName" value="${jdbc.driver}" />
               <propertyname="maxActive" value="10" />
               <propertyname="minIdle" value="5" />
        </bean>
        <!-- 2.配置SqlSessionFactory -->
        <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的-->
        <!-- 事务管理器 -->
        <beanid="transactionManager"
               class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
               <!--数据源 -->
               <propertyname="dataSource" ref="dataSource" />
        </bean>
        <!--通知 -->
        <tx:adviceid="txAdvice" transaction-manager="transactionManager">
               <tx:attributes>
                       <!--传播行为 -->
                       <tx:methodname="save*" propagation="REQUIRED" />
                       <tx:methodname="insert*" propagation="REQUIRED" />
                       <tx:methodname="add*" propagation="REQUIRED" />
                       <tx:methodname="create*" propagation="REQUIRED" />
                       <tx:methodname="delete*" propagation="REQUIRED" />
                       <tx:methodname="update*" propagation="REQUIRED" />
                       <tx:methodname="find*" propagation="SUPPORTS"read-only="true" />
                       <tx:methodname="select*" propagation="SUPPORTS"read-only="true" />
                       <tx:methodname="get*" propagation="SUPPORTS"read-only="true" />
               </tx:attributes>
        </tx:advice>
        <!--切面 -->
        <aop:config>
               <aop:advisoradvice-ref="txAdvice"
                       pointcut="execution(*com.ssm.service.*.*(..))" />
        </aop:config>

<bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据库连接池 --><property name="dataSource"ref="dataSource" /><!-- 加载mybatis的全局配置文件 --><property name="configLocation"value="classpath:mybatis/SqlMapConfig.xml" /></bean><!--3.Mapper映射文件的包扫描器
--><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage" value="com.ssm.dao" /></bean>
        <!--
配置包扫描器,扫描所有带@Service注解的类 -->
        <context:component-scanbase-package="com.ssm.service"/>
        <!-- service层发布dubbo服务 -->
        <!--
提供方应用信息,用于计算依赖关系 -->
        <dubbo:applicationname="ssm-manager" />
        <!--
注册中心的地址 -->
        <dubbo:registryprotocol="zookeeper" address="192.168.40.129:2181" />
        <!--
用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo"port="20880" />
        <!--
声明需要暴露的服务接口,interface为接口,ref为实现类 -->
        <dubbo:serviceinterface="com.ssm.inter.UserServiceInter"ref="userService" timeout="300000"/>
在service层需要提供对外发布的接口,来注册至zookeeper注册中心,来供web表现层来调用,这里需要注意的是:dubbo:service interface="提供的时服务层的接口"ref="接口的实现类"
(4).web层:控制器层,这里使用springmvc:先看结构图:



其实这里就是一个springmvc框架而已,在这里说明下spring于springmvc的区别:spring和springmvc时哟个父子容器,springmvc可以访问spring容器,但是反过来不成立,或许有人开始质疑,
为什么不用springmvc直接代替spring容器呢?事实上代替不了,spring框架有着天然的可融合性,与其他的框架可以方便整合起来。所以还是不同的,各司其职。
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd

       http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd        http://code.alibabatech.com/schema/dubbo

       http://code.alibabatech.com/schema/dubbo/dubbo.xsd
       http://www.springframework.org/schema/mvc 

       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
       

  <!--
扫描包路径:多个包使用逗号隔开 -->
  <context:component-scanbase-package="com.ssm.controller"></context:component-scan>
 

  <!--
配置mvc的注解驱动 -->
  <mvc:annotation-driven/>
 

  <!--
静态资源不被拦截 -->
  <mvc:default-servlet-handler/>
 
  <!--
映射器作用:请求的URL和Bean名字映射 -->     
  <beanclass="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
 
  <!--
适配器作用:鉴别项目Bean可以作为SpringWeb MVC中的处理器-->
  <beanclass="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
 
  <!--
视图解析器 -->
  <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"value="org.springframework.web.servlet.view.JstlView"/> 

    <property name="prefix"value="/"/> 

    <property name="suffix"value=".jsp"/> 

  </bean>
 

   

  <!--
文件解析器 -->
   <bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 

        <propertyname="defaultEncoding" value="UTF-8"/> 

        <!--
指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --> 

        <propertyname="maxUploadSize" value="200000"/> 

    </bean>

   

  <!-- json数据类型转换 -->
  <beanid="mappingJackson2HttpMessageConverter"class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">   

            <propertyname="supportedMediaTypes">   
                <list>   

                   <value>text/html;charset=UTF-8</value>   

                   <value>text/json;charset=UTF-8</value>   

                    <value>application/json;charset=UTF-8</value>   

                </list>   

            </property>   

  </bean>  

        

  <!--
注解适配器:响应体 -->
  <bean  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">   

      <propertyname="messageConverters">   
          <list>   

              <refbean="mappingJackson2HttpMessageConverter" />   

          </list>   

      </property>   

  </bean>   

  <!--
引用dubbo服务 -->
          <dubbo:applicationname="ssm-web" />
          <dubbo:registryprotocol="zookeeper" address="192.168.40.129:2181"/>  

          <dubbo:referenceinterface="com.ssm.inter.UserServiceInter"id="userServiceInter"/>
 </beans>
这里dubbo为引用服务的接口,需要接入zookeeper注册中心的地址来通过和zookeeper注册中西通信,来间接访问远程的服务端口,进行数据的请求和响应。
使用dubbo为中间层不仅项目维护起来带来便捷,使得各模块的功能更加清晰。由于分布式系统中每个服务器中部署的代码只负责一个功能,使用dubbo有如下好处:
1.监控中心宕掉不影响使用,只是丢失部分采样数据
2.数据库宕掉后,注册中心仍能通过缓存提供服务列表查询,但不能注册新服务
3.注册中心对等集群,任意一台宕掉后,将自动切换到另一台
4.注册中心全部宕掉后,服务提供者和服务消费者仍能通过本地缓存通讯
5.服务提供者无状态,任意一台宕掉后,不影响使用
6.服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提供者恢复
7.当服务集群规模进一步扩大,带动IT治理结构进一步升级,需要实现动态部署,进行流动计算,现有分布式服务架构不会带来阻力。
 
 
 

 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐