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
<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治理结构进一步升级,需要实现动态部署,进行流动计算,现有分布式服务架构不会带来阻力。