尚好房 04_服务拆分
尚好房:服务拆分
一、业务介绍
1、项目模块划分
根据前面的介绍,目前我们的系统规划了3个dubbo服务提供者模块:权限服务、房源服务与会员服务,及2个服务消费者模块:尚好房管理平台(web-admin)与网站前端(web-front)
2、服务调用关系
3、项目拆分说明
当前我们项目为单体的SSM项目,目前开发了权限管理的用户管理与角色管理,接着要开发房源管理的数据字典、小区管理与房源管理,权限管理与房源管理属于两不同的dubbo服务,当前我们就来把单体架构拆分为dubbo通信的分布式架构,拆分步骤:
1、shf-parent模块管理dubbo相关的依赖
2、common-util模块引入依赖
3、提取api接口模块:service-api
4、分解web-admin模块:
web-admin作为服务消费者,只负责dubbo消费端
service及dao层作为服务提供者,通过dubbo发布服务
二、服务拆分
1、shf-parent模块添加dubbo依赖管理
在shf-parent模块pom.xml新增依赖(第一天搭建环境的时候已经添加,不用重复添加)
-
版本锁定
<dubbo.version>2.6.0</dubbo.version> <zookeeper.version>3.4.7</zookeeper.version> <junit.version>4.12</junit.version> <zkclient.version>0.1</zkclient.version>
-
管理依赖
<!-- dubbo相关 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.1.GA</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency>
2、common-util模块引入依赖
在common-util模块pom.xml引入依赖(第一天搭建环境的时候已经添加,不用重复添加)
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
3、搭建service-api模块
用于存放所有业务层接口
3.1 创建工程
在shf-parent
工程中创建子工程service-api
3.2 引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>shf-parent</artifactId>
<groupId>com.atguigu</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-api</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>common-util</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>model</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
3.3 复制service接口
复制AdminService
与RoleService
到service-api
模块的com.atguigu.service
包中
4、搭建服务提供者模块
4.1 搭建service父工程
-
在
shf-parent
工程中搭建子工程命名为service
,这个工程作为所有服务提供者的父工程 -
删除
service
工程的src
目录 -
修改
service
工程的pom.xml
文件<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>shf-parent</artifactId> <groupId>com.atguigu</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service</artifactId> <packaging>pom</packaging> <dependencies> <dependency> <groupId>com.atguigu</groupId> <artifactId>service-api</artifactId> <version>1.0</version> </dependency> </dependencies> </project>
4.2 搭建service-acl工程
4.2.1 创建工程
在service
工程中创建子工程service-acl
子工程,并且使用插件将其转成javaweb
项目
4.2.2 修改pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>service</artifactId>
<groupId>com.atguigu</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-acl</artifactId>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.15.v20190215</version>
<configuration>
<!-- 如果检测到项目有更改则自动热部署,每隔n秒扫描一次。默认为0,即不扫描-->
<scanIntervalSeconds>2</scanIntervalSeconds>
<webAppConfig>
<!--指定web项目的根路径,默认为/ -->
<contextPath>/</contextPath>
</webAppConfig>
<httpConnector>
<!--端口号,默认 8080-->
<port>7001</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.2.3 拷贝代码
拷贝web-admin
工程中的业务层和持久层内容
-
创建相同的业务层和持久层包结构
-
拷贝
AdminServiceImpl
和RoleServiceImpl
实现类到com.atguigu.service.impl
包 -
替换业务层实现类上的
@Service
注解为Dubbo的Service注解
@Service(interfaceClass = AdminService.class) @Transactional(propagation = Propagation.REQUIRED) public class AdminServiceImpl extends BaseServiceImpl<Admin> implements AdminService {
@Service(interfaceClass = RoleService.class) @Transactional(propagation = Propagation.REQUIRED) public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService{
-
拷贝持久层接口
AdminMapper
和RoleMapper
到com.atguigu.mapper
包中 -
拷贝
mappers
目录到resources
目录中 -
拷贝日志配置文件和
jdbc.properties
文件到resources
目录中 -
拷贝
spring-persist.xml
和spring-service.xml
到resources/spring
目录中 -
修改
spring-service.xml
配置文件① 删除包扫描配置,因为包扫描应该有dubbo完成,进行服务发布
② 修改事务注解驱动配置
<!-- 开启事务控制的注解支持 注意:此处必须加入proxy-target-class="true", 需要进行事务控制,会由Spring框架产生代理对象, Dubbo需要将Service发布为服务,要求必须使用cglib创建代理对象。 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
-
添加发布dubbo服务的配置文件:
spring/spring-registry.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:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--指定应用名称--> <dubbo:application name="service-acl"/> <!--配置协议名和端口,如果不配置的话,端口默认为20880--> <dubbo:protocol name="dubbo" port="20881"/> <!--配置注册中心地址--> <dubbo:registry address="zookeeper://localhost:2181" /> <!--包扫描发布服务--> <dubbo:annotation package="com.atguigu"/> </beans>
-
修改
web.xml
文件<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!--配置ContextLoaderListener--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
注意:
spring-service.xml
文件中可以不使用import
标签导入spring-persist.xml
了
5、搭建服务消费者模块
5.1 搭建web父工程
-
在
shf-parent
工程中搭建子工程命名为web
,这个工程作为所有服务消费者的父工程 -
删除
web
工程的src
目录 -
修改
web
工程的pom.xml
文件<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>shf-parent</artifactId> <groupId>com.atguigu</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>web</artifactId> <packaging>pom</packaging> <dependencies> <dependency> <groupId>com.atguigu</groupId> <artifactId>service-api</artifactId> <version>1.0</version> </dependency> </dependencies> </project>
5.2 搭建web-admin工程
5.2.1 移动模块
① 将web-admin
移动到web
模块中
② 删除shf-parent
模块pom.xml
文件的<module>web-admin</module>
标签,该模块已移动到web模块
③ 在web
模块的pom.xml
中新增
<modules>
<module>web-admin</module>
</modules>
5.2.2 修改web-admin工程的pom.xml
修改了父模块为web,删除了依赖(父模块已引入依赖)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>web</artifactId>
<groupId>com.atguigu</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>web-admin</artifactId>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.15.v20190215</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
<webAppConfig>
<!--指定web项目的根路径,默认为/ -->
<contextPath>/</contextPath>
</webAppConfig>
<httpConnector>
<!--端口号,默认 8080-->
<port>8000</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>
</project>
5.2.3 删除service与dao层
① 删除com.atguigu.service
包和com.atguigu.mapper
包
② 删除resources/mappers
目录
③ 删除resources/spring/spring-service.xml
和resources/spring/spring-persist.xml
文件
5.2.4 新增dubbo配置文件
创建resources/spring/spring-registry.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--配置应用名-->
<dubbo:application name="web-admin"/>
<!--配置注册中心地址-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--配置dubbo包扫描-->
<dubbo:annotation package="com.atguigu"/>
<!--启动时候不检查 设置连接超时时间-->
<dubbo:consumer check="false" timeout="600000"></dubbo:consumer>
</beans>
5.2.5 修改web.xml
删除ContextLoadListener
和comtext-param
配置,并且将加载的配置文件路径改成classpath:spring/spring-*.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置解决乱码的过滤器-->
<!-- 配置过滤器解决 POST 请求的字符乱码问题 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- encoding参数指定要使用的字符集名称 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- 请求强制编码 -->
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<!-- 响应强制编码 -->
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5.2.6 改造表现层
将表现层注入服务对象的@Autowired
注解改成@Reference
注解
@Reference
private AdminService adminService;
@Reference
private RoleService roleService;
6、测试
① 在shf-parent
项目中执行install
② 启动zookeeper
③ 使用jetty
插件启动service-acl
与web-admin
模块
④ 访问:http://localhost:8000/