实战day02(四)----工程改造完成

本文介绍如何通过Dubbo进行服务拆分及发布流程,包括配置修改、依赖管理及测试部署等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、工程改造

1.1 拆分工程

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

e3-manager-web

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

e3-manager

   |--e3-manager-dao

   |--e3-manager-interface

   |--e3-manager-pojo

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

1.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>

dubbo依赖一个spring2.5.x的jar包,而我们至少都是spring4.x了,所以,这里要exclustion。dubbo依赖一个netty,其他的地方也依赖netty,所以排除。

2、在spring的配置文件中添加dubbo的约束,然后使用dubbo:service发布服务。

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

从上面的网址下载不到约束,所以,还是手动导入。

dubbo.xsd

下载一个dubbo.xsd文件
windows->preferrence->xml->xmlcatalog 
add->catalog entry  ->file system 选择刚刚下载的文件路径
修改key值和配置文件的http://code.alibabatech.com/schema/dubbo/dubbo.xsd 相同


保存。。在xml文件右键validate  ok解决了。。

applicationContext-service.xml

<?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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.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://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	<!-- 配置包扫描器 -->
	<context:component-scan base-package="cn.e3mall.service" />
	<!-- 使用dubbo发布服务 -->
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="e3-manager" />
	<dubbo:registry protocol="zookeeper" address="192.168.17.128:2181" />
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />
	<!-- 声明需要暴露的服务接口 -->
	<dubbo:service interface="cn.e3mall.service.ItemService"
		ref="itemServiceImpl" />

</beans>

如此,服务就发布了。

接下来,就看怎么引用服务了。

e3-manager-web的pom.xml中添加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>

e3-manager-web的springmvc.xml中添加dubbo的前缀和约束

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

springmvc.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: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://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-4.2.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.17.128:2181" />
	<dubbo:reference interface="cn.e3mall.service.ItemService"
		id="itemService" />
</beans>

web层要用service的服务,总是需要方法名的。这就是,当初为什么把interface单独打成jar包的原因,interface就是这两层之间的一个规范。

二、测试

e3-manager是一个web工程,e3-manager-web也是一个web工程。需要启动两个tomcat。

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

<build>
	<plugins>
		<!-- 配置Tomcat插件 -->
		<plugin>
			<groupId>org.apache.tomcat.maven</groupId>
			<artifactId>tomcat7-maven-plugin</artifactId>
			<configuration>
				<path>/</path>
				<port>8081</port>
			</configuration>
		</plugin>
	</plugins>
</build>

先启动e3-manager,成功。

在启动e3-manager-web,报错


要把interface打到本地仓库。

interface install,又报错。


因为,需要pojo。我们干脆,把e3-manager install一下。

再启动e3-manager-web,成功。

访问:http://localhost:8082/item/830972




逆向工程生成的pojo都有可能需要网络传输,所以,让它们都实现Serializable接口。

在逆向工程的配置文件中添加插件:

<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>


Example的持久化方法

pojo install


终于成功了。

源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值