SOA
SOA:Service Oriented Architecture面向服务的架构。也就是把工程拆分成服务层、表现层两个工程。服务层中包含业务逻辑,只需要对外提供服务即可。表现层只需要处理和页面的交互,业务逻辑都是调用服务层的服务来实现。
将manager_web独立出来 称为表现层
从shopping_manager移出web模块,放到平级目录
改造模块
1.web模块操作
- 导入web模块--file--new--module from...
- 将web模块的父工程
引用改为parent的 - web改为依赖的是shopping_manager_interface
- 不依赖与service 所以spring的包需要重新导入 把service的pom拷贝即可
- 把除了springmvc的xml文件和db文件全部放到到service中
- 拷贝WEB-INF下面的web.xml文件导service中。去除:springmvc的前端控制器,解决post乱码。
- 记得删除web.xml的spring的监听器
2.manager模块操作
- 去除
shopping_manager_web
3.service模块操作
- 改为war包
- 拥有了xml db web.xml文件
实现远程通信
1、Webservice:效率不高基于soap协议。项目中不推荐使用。
2、使用restful形式的服务:http+json。很多项目中应用。如果服务太多,服务之间调用关系混乱,需要治疗服务。
3、使用dubbo。使用rpc协议进行远程调用,直接使用socket通信。传输效率高,并且可以统计出系统之间的调用关系、调用次数
dubbo的使用
看下面的文档
service
1.加入dubbo的jar包 service_and_web
<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>
2.使用dubbo发布服务
在servce的applicationContext-service.xml加入
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="shopping_manager" />
<dubbo:registry protocol="zookeeper"
address="192.168.0.5:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.matteo.service.ItemService" ref="itemService" timeout="1000*600"/>
web
1.执行上面的第一步
2.引用dubbo服务
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
<!-- 引用dubbo服务 -->
<dubbo:application name="shopping_manager"/>
<dubbo:registry protocol="zookeeper"
address="192.168.0.5:2181"/>
<dubbo:reference interface="com.matteo.service.ItemService" id="itemService" />
开始测试
会报错 pojo类需要实现Serializable接口
启动二个tomcat 修改其中一个端口即可
web模块也需要独立配置一个tomcat插件
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>8081</port>
</configuration>
</plugin>
</plugins>