电子商城——将工程改造为SOA架构(4)_uu_uuu的博客-程序员秘密

技术标签: 电子商城项目搭建  

目录

1 分析

2 工程改造

2.1 拆分工程

2.2 服务层工程

2.3 表现层工程

3 工程测试


1 分析

由于电子商城是基于soa的架构,表现层和服务层是不同的工程。所以要实现类似于商品列表查询这样的功能,需要两个系统之间进行通信。

如何实现远程通信?

1、Webservice:效率不高基于soap协议。项目中不推荐使用。

2、使用restful形式的服务:http+json。很多项目中应用。如果服务太多,服务之间调用关系混乱,需要治疗服务。

3、使用dubbo。(类似于服务中心)使用rpc协议进行远程调用,直接使用socket通信。传输效率高,并且可以统计出系统之间的调用关系、调用次数。

涉及dubbo和zookeeper的介绍及安装分别参见:

dubbo:https://blog.csdn.net/uu_uuu/article/details/100182295

zookeeper:https://blog.csdn.net/uu_uuu/article/details/100182493


2 工程改造

2.1 拆分工程

1)将表现层工程独立出来:

e3-manager-web

2)将原来的e3-manager改为如下结构

e3-manager

   |--e3-manager-dao

   |--e3-manager-interface

   |--e3-manager-pojo

   |--e3-manager-service(打包方式改为war)

2.2 服务层工程

第一步:把e3-manager的pom文件中删除e3-manager-web模块。

第二步:把e3-manager-web文件夹移动到e3-manager同一级目录。

第三步:e3-manager-service的pom文件修改打包方式

<packaging>war</packaging>

第四步:在e3-manager-service工程中添加web.xml文件。

第五步:把e3-manager-web的配置文件复制到e3-manager-service中。

删除springmvc.xml

第六步:web.xml 中只配置spring容器。删除前端控制器

第七步:发布服务

  1. 在e3-manager-Service工程中添加dubbo依赖的jar包。
<!-- dubbo相关 -->
		<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、在spring的配置文件applicationContext-service.xml中添加dubbo的约束,然后使用dubbo:service发布服务。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<context:component-scan base-package="cn.e3mall.service"></context:component-scan>

	<!-- 使用dubbo发布服务 -->
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="e3-manager" />
	<dubbo:registry protocol="zookeeper" address="192.168.25.128:2181" />
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />
	<!-- 声明需要暴露的服务接口 -->
	<dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" />

</beans>

配置完后会报错,原因是这个约束不能联网添加,需要手动在eclipse中添加约束dubbo.xsd

dubbo.xsd下载:https://pan.baidu.com/s/1I-dPbPUY0XeZNmRNJ_fsfA

2.3 表现层工程

改造e3-manager-web工程。

第一步:删除mybatis、和spring的配置文件。只保留springmvc.xml

第二步:修改e3-manager-web的pom文件,

  1. 修改parent为e3-parent
  2. 添加spring和springmvc的jar包的依赖
  3. 删除e3-mangager-service的依赖
  4. 添加dubbo的依赖
<!-- dubbo相关 -->
		<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>

5、e3-mangager-web添加对e3-manager-Interface的依赖。

第三步:修改springmvc.xml,在springmvc的配置文件中添加服务的引用。

<?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: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-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="cn.e3mall.controller" />
	<mvc:annotation-driven />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 引用dubbo服务 -->
	<dubbo:application name="e3-manager-web"/>
	<dubbo:registry protocol="zookeeper" address="192.168.25.128:2181"/>	
	<dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />
	
</beans>

第四步:在e3-manager-web工程中添加tomcat插件配置。

<build>
		<plugins>
			<!-- 配置Tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
            		<!-- 直接访问 Tomcat 服务器的 manager -->
            		<url>http://localhost:8080/manager/text</url>
            		<server>tomcat8</server>
                    <path>/</path>
					<port>8081</port>
        		</configuration>
			</plugin>
		</plugins>
	</build>

3 工程测试

对以上配置进行测试,因为web依赖manager,所以先启动manager,此时项目只是初始化一个Spring容器,没有服务。再启动web,报错:

Failed to execute goal on project e3-manager-web: Could not resolve dependencies for project cn.e3mall:e3-manager-web:war:0.0.1-SNAPSHOT: Could not find artifact cn.e3mall:e3-manager-interface:jar:0.0.1-SNAPSHOT -> [Help 1]

因为web依赖interface,所以需要先把interface添加到本地仓库,还是报错,没有添加interface的依赖pojo,最直接的办法,把整个manager添加到本地仓库。

添加成功,再启动web。

运行成功!

测试之前的例子的时候报错,要求使用的pojo类TbItem实现序列化接口。

序列化:

重新添加项目到本地仓库,然后启动项目。

工程改造完成。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/uu_uuu/article/details/100182675

智能推荐

基于r-Kernel的LiteOS操作系统_ovd_system_liteos_刘秋杉的博客-程序员秘密

LiteOS是应用于资源受限的传感网络的一种基于线程的类UNIX操作系统。它采用r-kernel内核,r-kernel有三个特征

二叉搜索树的创建 && 查找 & 插入 & 删除_为什么关键字没有右子树,后继是最低的祖先_鱼思故渊的博客-程序员秘密

二叉搜索树的删除:在删除之前需要从树中查找到这个节点,然后再针对情况来判断如何删除。分为三种情况,首先是此节点没有孩子节点,此节点有一个孩子节点,此节点有两个孩子节点void Delete(BinTree*& root,int value){ BinTree* delnode= NULL; if(root == NULL) return ; BinTree* temp

Maticsoft 自动化智能软件系列_weixin_30251829的博客-程序员秘密

软件名称: 动软.net代码生成器软件版本: 2.1.8 powered by 25175.net软件语言: 简体中文运行环境: Win2003/XP/2000 .net Framework 2.0软件简介: 动软.net代码生成器Codematic 是一款为C#数据库程序员设计的自动代码生成器,Codematic 生成的代码基于面向对象的思想和三层架构设计,结合了Pe...

adb shell top 命令详解_Johnny2004的博客-程序员秘密

一.使用adb shell 命令:adb shell top -m 10 -s 9二.解释Tasks: 552 total, 1 running, 510 sleeping, 0 stopped, 0 zombie 任务(进程) 系统现在共有552个进程,其中处于运行中的有1个,510个在休眠(sleep),stoped状态的有0个,zombie状态(僵尸)的有0个。Mem: 5849960k total, 4014628k used, 1835332k f

Keras训练模型部署到C++环境并生成DLL_keras c++_李小肥的YY的博客-程序员秘密

一. 准备工作在Windows上搭建完成Tensorflow的C++环境,这里参考本人的上一篇博客在windows上部署opencv3的C++环境python3的keras和tf2.X的环境:pip3 install keras==2.4.3和pip3 install tensorflow==2.3.1python3的opencv环境:pip3 install opencv-python二. python模型训练python模型构建:Kaggle猫狗分类python完整代

sun.misc.Base64Encoder 和 sun.misc.Base64Decoder无法引用问题_完美缺陷的博客-程序员秘密

java7以前:BASE64Encoder encoder = new BASE64Encoder();BASE64Decoder decoder = new BASE64Decoder();java7之后:import java.util.Base64.Encoderimport java.util.Base64.DecoderEncoder encoder = B...

随便推点

Twitter的用户推荐算法_tt搜人后的推荐关注逻辑_大魁的博客-程序员秘密

关于Twitter的用户推荐算法,Quora上的文章有一个说明。算法基本分4步:First and foremost, we looked at who your friends follow, who they talk to, who they RT as gauges of your interest.Then we applied either positive/negative

给.Net程序员和WEB程序员的一些工作行为建议_普通网友的博客-程序员秘密

 本文仅仅是一篇程序员职业行为建议,不存在任何源代码,也并不能自动化解决问题。但是相信做的人多了,自然也会有效果了。适用于两类读者: <!-- alimama_pid="mm_10249644_1605763_5018464"; alimama_type="f";alimama_sizecode ="tl_1x1_8"; alimama_fontsize=12;

计算机发sci什么水平,计算机专业发一篇sci算什么水平_名宠社的博客-程序员秘密

计算机专业发一篇sci相对比较容易,所以必须看你在那个杂志上发表,下面最顶级的也就是计算机一区的期刊,你可以比对下:HUMAN-COMPUTER INTERACTION 计算机:控制论2.905BIOINFORMATICS 计算机:跨学科应用4.328IEEE TRANSACTIONS ON MEDICAL IMAGING 计算机:跨学科应用4.004JOURNAL OF THE AMERICAN...

oracle wait events(等待事件) 一览表_congtangguan9702的博客-程序员秘密

在oracle出现性能问题时,通过等待事件,非常有助于我们去分析问题。在oracle的每个大的版本里面,都会增加或者删除一些等待事件。 我们可以通过查询v$event_name查看所有的等待事件(wait eve...

DataGridView常用操作_weixin_30511107的博客-程序员秘密

一.DataGridView列右击菜单事件处理(1). 添加一个快捷菜单contextMenuStrip1;(2). 给dataGridView1的CellMouseDown事件添加处理程序:private void DataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { ...

计算机体系结构的一知半解_半吊子全栈工匠的博客-程序员秘密

计算机的体系结构是关于计算机自身的系统架构,而软件指令集架构在计算机体系结构中处于核心地位,因为软件和硬件之间都是通过软件指令集架构(ISA)来对话的。例如,在20世纪60年代早期,IBM...

推荐文章

热门文章

相关标签