• 尚好房 04_服务拆分


    尚好房:服务拆分

    一、业务介绍

    1、项目模块划分

    根据前面的介绍,目前我们的系统规划了3个dubbo服务提供者模块:权限服务、房源服务与会员服务,及2个服务消费者模块:尚好房管理平台(web-admin)与网站前端(web-front)

    2、服务调用关系

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JKmBjFpK-1661870386677)(images/01/img_001.png)]

    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新增依赖(第一天搭建环境的时候已经添加,不用重复添加)

    1. 版本锁定

      <dubbo.version>2.6.0dubbo.version>
      <zookeeper.version>3.4.7zookeeper.version>
      <junit.version>4.12junit.version>
      <zkclient.version>0.1zkclient.version>
      
      • 1
      • 2
      • 3
      • 4
    2. 管理依赖

      
      <dependency>
          <groupId>com.alibabagroupId>
          <artifactId>dubboartifactId>
          <version>${dubbo.version}version>
      dependency>
      <dependency>
          <groupId>org.apache.zookeepergroupId>
          <artifactId>zookeeperartifactId>
          <version>${zookeeper.version}version>
      dependency>
      <dependency>
          <groupId>com.github.sgroschupfgroupId>
          <artifactId>zkclientartifactId>
          <version>${zkclient.version}version>
      dependency>
      <dependency>
          <groupId>javassistgroupId>
          <artifactId>javassistartifactId>
          <version>3.12.1.GAversion>
      dependency>
      <dependency>
          <groupId>commons-codecgroupId>
          <artifactId>commons-codecartifactId>
          <version>1.10version>
      dependency>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26

    2、common-util模块引入依赖

    在common-util模块pom.xml引入依赖(第一天搭建环境的时候已经添加,不用重复添加)

    
    <dependency>
     <groupId>com.alibabagroupId>
     <artifactId>dubboartifactId>
    dependency>
    <dependency>
     <groupId>org.apache.zookeepergroupId>
     <artifactId>zookeeperartifactId>
    dependency>
    <dependency>
     <groupId>com.github.sgroschupfgroupId>
     <artifactId>zkclientartifactId>
    dependency>
    <dependency>
     <groupId>javassistgroupId>
     <artifactId>javassistartifactId>
    dependency>
    <dependency>
     <groupId>commons-codecgroupId>
     <artifactId>commons-codecartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3、搭建service-api模块

    用于存放所有业务层接口

    3.1 创建工程

    shf-parent工程中创建子工程service-api

    3.2 引入依赖
    
    <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-parentartifactId>
            <groupId>com.atguigugroupId>
            <version>1.0version>
        parent>
        <modelVersion>4.0.0modelVersion>
        <artifactId>service-apiartifactId>
        <packaging>jarpackaging>
    
        <dependencies>
            <dependency>
                <groupId>com.atguigugroupId>
                <artifactId>common-utilartifactId>
                <version>1.0version>
            dependency>
            <dependency>
                <groupId>com.atguigugroupId>
                <artifactId>modelartifactId>
                <version>1.0version>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    3.3 复制service接口

    复制AdminServiceRoleServiceservice-api模块的com.atguigu.service包中

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZRKjU1Hp-1661870386682)(images/04/img_002.png)]

    4、搭建服务提供者模块

    4.1 搭建service父工程
    1. shf-parent工程中搭建子工程命名为service,这个工程作为所有服务提供者的父工程

    2. 删除service工程的src目录

    3. 修改service工程的pom.xml文件

      
      <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-parentartifactId>
              <groupId>com.atguigugroupId>
              <version>1.0version>
          parent>
          <modelVersion>4.0.0modelVersion>
      
          <artifactId>serviceartifactId>
          <packaging>pompackaging>
      
          <dependencies>
              <dependency>
                  <groupId>com.atguigugroupId>
                  <artifactId>service-apiartifactId>
                  <version>1.0version>
              dependency>
          dependencies>
      project>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
    4.2 搭建service-acl工程
    4.2.1 创建工程

    service工程中创建子工程service-acl子工程,并且使用插件将其转成javaweb项目

    4.2.2 修改pom.xml
    
    
    <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>serviceartifactId>
            <groupId>com.atguigugroupId>
            <version>1.0version>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>service-aclartifactId>
        <packaging>warpackaging>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.jettygroupId>
                    <artifactId>jetty-maven-pluginartifactId>
                    <version>9.4.15.v20190215version>
                    <configuration>
                        
                        <scanIntervalSeconds>2scanIntervalSeconds>
                        <webAppConfig>
                            
                            <contextPath>/contextPath>
                        webAppConfig>
                        <httpConnector>
                            
                            <port>7001port>
                        httpConnector>
                    configuration>
                plugin>
            plugins>
        build>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    4.2.3 拷贝代码

    拷贝web-admin工程中的业务层和持久层内容

    1. 创建相同的业务层和持久层包结构

    2. 拷贝AdminServiceImplRoleServiceImpl实现类到com.atguigu.service.impl

    3. 替换业务层实现类上的@Service注解为Dubbo的Service注解

      @Service(interfaceClass = AdminService.class)
      @Transactional(propagation = Propagation.REQUIRED)
      public class AdminServiceImpl extends BaseServiceImpl<Admin> implements AdminService {
      
      • 1
      • 2
      • 3
      @Service(interfaceClass = RoleService.class)
      @Transactional(propagation = Propagation.REQUIRED)
      public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService{
      
      • 1
      • 2
      • 3
    4. 拷贝持久层接口AdminMapperRoleMappercom.atguigu.mapper包中

    5. 拷贝mappers目录到resources目录中

    6. 拷贝日志配置文件和jdbc.properties文件到resources目录中

    7. 拷贝spring-persist.xmlspring-service.xmlresources/spring目录中

    8. 修改spring-service.xml配置文件

      ① 删除包扫描配置,因为包扫描应该有dubbo完成,进行服务发布

      ② 修改事务注解驱动配置

      
      <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    9. 添加发布dubbo服务的配置文件:spring/spring-registry.xml

      
      <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"/>
          
          <dubbo:protocol name="dubbo" port="20881"/>
          
          <dubbo:registry address="zookeeper://localhost:2181" />
          
          <dubbo:annotation package="com.atguigu"/>
      beans>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
    10. 修改web.xml文件

      
      <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">
          
          <context-param>
              <param-name>contextConfigLocationparam-name>
              <param-value>classpath:spring/spring-*.xmlparam-value>
          context-param>
          <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
          listener>
      web-app>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

      注意: spring-service.xml文件中可以不使用import标签导入spring-persist.xml

    5、搭建服务消费者模块

    5.1 搭建web父工程
    1. shf-parent工程中搭建子工程命名为web,这个工程作为所有服务消费者的父工程

    2. 删除web工程的src目录

    3. 修改web工程的pom.xml文件

      
      <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-parentartifactId>
              <groupId>com.atguigugroupId>
              <version>1.0version>
          parent>
          <modelVersion>4.0.0modelVersion>
          <artifactId>webartifactId>
          <packaging>pompackaging>
          <dependencies>
              <dependency>
                  <groupId>com.atguigugroupId>
                  <artifactId>service-apiartifactId>
                  <version>1.0version>
              dependency>
          dependencies>
      project>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
    5.2 搭建web-admin工程
    5.2.1 移动模块

    ① 将web-admin移动到web模块中

    ② 删除shf-parent模块pom.xml文件的web-admin标签,该模块已移动到web模块

    ③ 在web模块的pom.xml中新增

    <modules>
        <module>web-adminmodule>
    modules>
    
    • 1
    • 2
    • 3
    5.2.2 修改web-admin工程的pom.xml

    修改了父模块为web,删除了依赖(父模块已引入依赖)

    
    
    <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>webartifactId>
            <groupId>com.atguigugroupId>
            <version>1.0version>
        parent>
        <modelVersion>4.0.0modelVersion>
        <artifactId>web-adminartifactId>
        <packaging>warpackaging>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.jettygroupId>
                    <artifactId>jetty-maven-pluginartifactId>
                    <version>9.4.15.v20190215version>
                    <configuration>
                        <scanIntervalSeconds>2scanIntervalSeconds>
                        <webAppConfig>
                            
                            <contextPath>/contextPath>
                        webAppConfig>
                        <httpConnector>
                            
                            <port>8000port>
                        httpConnector>
                    configuration>
                plugin>
            plugins>
        build>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    5.2.3 删除service与dao层

    ① 删除com.atguigu.service包和com.atguigu.mapper

    ② 删除resources/mappers目录

    ③ 删除resources/spring/spring-service.xmlresources/spring/spring-persist.xml文件

    5.2.4 新增dubbo配置文件

    创建resources/spring/spring-registry.xml

    
    <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:annotation package="com.atguigu"/>
        
        <dubbo:consumer check="false" timeout="600000">dubbo:consumer>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    5.2.5 修改web.xml

    删除ContextLoadListenercomtext-param配置,并且将加载的配置文件路径改成classpath:spring/spring-*.xml

    
    <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>dispatcherServletservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:spring/spring-*.xmlparam-value>
            init-param>
            <load-on-startup>1load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>dispatcherServletservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
        
        
        <filter>
            <filter-name>CharacterEncodingFilterfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            
            <init-param>
                <param-name>encodingparam-name>
                <param-value>UTF-8param-value>
            init-param>
            
            <init-param>
                <param-name>forceRequestEncodingparam-name>
                <param-value>trueparam-value>
            init-param>
            
            <init-param>
                <param-name>forceResponseEncodingparam-name>
                <param-value>trueparam-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    5.2.6 改造表现层

    将表现层注入服务对象的@Autowired注解改成@Reference注解

    @Reference
    private AdminService adminService;
    
    • 1
    • 2
    @Reference
    private RoleService roleService;
    
    • 1
    • 2

    6、测试

    ① 在shf-parent项目中执行install

    ② 启动zookeeper

    ③ 使用jetty插件启动service-aclweb-admin模块

    ④ 访问:http://localhost:8000/

  • 相关阅读:
    Mini-dashboard 和meilisearch配合使用
    《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
    Java Math toIntExact() 使用方法及示例 long转int
    【面试题】有了Docker为啥还需要k8s?
    抽丝剥茧,C#面向对象快速上手
    Python3中的for循环
    Android 13.0 user模式下解除系统进入recovery功能的限制
    3D模型格式转换工具HOOPS Exchange:如何将3D PDF转换为STEP格式?
    open SUSE 安装Vmware Workstation Pro 17
    SpringCloud Alibaba微服务实战三 - 服务调用
  • 原文地址:https://blog.csdn.net/lbw18/article/details/126614249