08分布式电商项目 - 框架搭建

简介: 08分布式电商项目 - 框架搭建

工程结构分析与设计

最终完整的工程结构如下:

创建数据库

执行资源文件夹中 pinyougou-db.sql

搭建框架

顶级父工程

创建 Maven 工程 pinyougou-parent (POM) ,groupId 为 com.pinyougou ,artifactId 为pinyougou-parent , 在 pom.xml 中 添 加 锁 定 版 本 信 息 dependencyManagement 与pluginManagement,详见“资源/配置文件/pom.xml”。

以下模块均继承自此父工程

通用实体类模块

创建通用实体类模块-pinyougou-pojo

通用数据访问模块

创建通用数据访问模块 pinyougou-dao .添加依赖 Mybatis 和 pinyougou-pojo。

<dependencies>
  <!-- Mybatis -->
  <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
  </dependency>
  <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
  </dependency> 
  <dependency>
      <groupId>com.github.miemiedev</groupId>
      <artifactId>mybatis-paginator</artifactId>
  </dependency>
  <!-- MySql -->
  <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
  </dependency>
  <!-- 连接池 -->
  <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
  </dependency>
  <dependency>
      <groupId>com.pinyougou</groupId>
      <artifactId>pinyougou-pojo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
  </dependency>
 </dependencies>

将“配置文件/数据访问层”下的配置文件拷贝到 pinyougou-dao 工程。

通用工具类模块

创建通用工具类模块 pinyougou-common

商家商品服务接口 模块

创建 maven(jar)模块 pinyougou-sellergoods-interface , pom.xml 添加依赖。

<dependencies>
   <dependency>
     <groupId>com.pinyougou</groupId>
     <artifactId>pinyougou-pojo</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </dependency>
</dependencies>

商家商品服务模块

创建 maven(war)模块 pinyougou-sellergoods-service ,pom.xml 引入依赖。

<dependencies>
  <!-- Spring -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
  </dependency>
<!-- 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>junit</groupId>
    <artifactId>junit</artifactId>
  </dependency>
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
  </dependency>
  <dependency>
    <groupId>javassist</groupId>
    <artifactId>javassist</artifactId>
  </dependency>
  <dependency>
     <groupId>commons-codec</groupId>
     <artifactId>commons-codec</artifactId> 
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <scope>provided</scope>
  </dependency> 
   <dependency>
     <groupId>com.pinyougou</groupId>
     <artifactId>pinyougou-common</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </dependency>
   <dependency>
     <groupId>com.pinyougou</groupId>
     <artifactId>pinyougou-dao</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </dependency>
   <dependency>
     <groupId>com.pinyougou</groupId>
     <artifactId>pinyougou-sellergoods-interface</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </dependency>
 </dependencies>
 <build>
  <plugins>
    <!-- 配置 Tomcat 插件 -->
    <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat7-maven-plugin</artifactId>
      <configuration>
        <path>/</path>
        <port>9001</port>
      </configuration>
    </plugin>
  </plugins>
 </build>
</project>

在 webapps 下创建 WEB-INF/web.xml ,加载 spring 容器。

<?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">
<!-- 加载 spring 容器 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

创建包 com.pinyougou.sellergoods.service.impl

在 src/main/resources 下创建 spring/applicationContext-service.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.xsd
 http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">
    <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
    <dubbo:application name="pinyougou-sellergoods-service"/> 
    <dubbo:registry address="zookeeper://192.168.25.129:2181"/>
    <dubbo:annotation package="com.pinyougou.sellergoods.service.impl" /> 
</beans>

运营商管理后台

创建 maven(war)模块 pinyougou-manager-web , pom.xml 引入依赖。

<dependencies>
  <!-- Spring -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
  </dependency>
  <!-- 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>junit</groupId>
    <artifactId>junit</artifactId>
  </dependency>
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
  </dependency>
  <dependency>
    <groupId>javassist</groupId>
    <artifactId>javassist</artifactId>
  </dependency>
  <dependency>
     <groupId>commons-codec</groupId>
     <artifactId>commons-codec</artifactId> 
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <scope>provided</scope>
  </dependency> 
   <dependency>
     <groupId>com.pinyougou</groupId>
     <artifactId>pinyougou-common</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </dependency>
   <dependency>
     <groupId>com.pinyougou</groupId>
     <artifactId>pinyougou-sellergoods-interface</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </dependency>
 </dependencies>
 <build>
  <plugins>
    <!-- 配置 Tomcat 插件 -->
    <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat7-maven-plugin</artifactId>
      <configuration>
        <path>/</path>
        <port>9101</port>
      </configuration>
    </plugin>
  </plugins>
 </build>

在 webapps 下创建 WEB-INF/web.xml ,加载 spring 容器。

<?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">
   <!-- 解决 post 乱码 -->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param> 
       <param-name>forceEncoding</param-name> 
       <param-value>true</param-value> 
    </init-param> 
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
   <servlet>
     <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- 指定加载的配置文件 ,通过参数 contextConfigLocation 加载-->
     <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:spring/springmvc.xml</param-value>
     </init-param>
   </servlet>
  <servlet-mapping>
     <servlet-name>springmvc</servlet-name>
     <url-pattern>*.do</url-pattern>
   </servlet-mapping>
</web-app>

创建包 com.pinyougou.manager.controller

在 src/main/resources 下创建 spring/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.xsd
 http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">
<mvc:annotation-driven>
  <mvc:message-converters register-defaults="true">
     <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> 
       <property name="supportedMediaTypes" value="application/json"/>
       <property name="features">
         <array>
           <value>WriteMapNullValue</value>
           <value>WriteDateUseDateFormat</value>
         </array>
       </property>
     </bean>
   </mvc:message-converters> 
</mvc:annotation-driven>
<!-- 引用 dubbo 服务 -->
<dubbo:application name="pinyougou-manager-web" />
<dubbo:registry address="zookeeper://192.168.25.132:2181"/>
<dubbo:annotation package="com.pinyougou.manager.controller" /> 
</beans>

商家管理后台

构建 web 模块 pinyougou-shop-web 与运营商管理后台的构建方式类似。区别:

(1)定义 tomcat 的启动端口为 9102

(2)springmvc.xml

<!-- 引用 dubbo 服务 -->
<dubbo:application name="pinyougou-shop-web" />
<dubbo:registry address="zookeeper://192.168.25.132:2181"/>
<dubbo:annotation package="com.pinyougou.shop.controller" />

实体类与数据访问层模块

生成代码

利用反向工程 generatorSqlmapCustom 实现实体类与数据访问层代码的自动生成

拷贝代码

将 com.pinyougou.pojo 包拷贝到 pojo 工程

将 com.pinyougou.mapper 包和 resouce 下的 com.pinyougou.mapper 文件夹拷贝到 dao 工程

修改实体类代码

修改每个实体类,让其实现 Serializable 接口

目录
相关文章
|
10月前
|
数据采集 存储 数据可视化
分布式爬虫框架Scrapy-Redis实战指南
本文介绍如何使用Scrapy-Redis构建分布式爬虫系统,采集携程平台上热门城市的酒店价格与评价信息。通过代理IP、Cookie和User-Agent设置规避反爬策略,实现高效数据抓取。结合价格动态趋势分析,助力酒店业优化市场策略、提升服务质量。技术架构涵盖Scrapy-Redis核心调度、代理中间件及数据解析存储,提供完整的技术路线图与代码示例。
1069 0
分布式爬虫框架Scrapy-Redis实战指南
|
8月前
|
监控 Java 调度
SpringBoot中@Scheduled和Quartz的区别是什么?分布式定时任务框架选型实战
本文对比分析了SpringBoot中的`@Scheduled`与Quartz定时任务框架。`@Scheduled`轻量易用,适合单机简单场景,但存在多实例重复执行、无持久化等缺陷;Quartz功能强大,支持分布式调度、任务持久化、动态调整和失败重试,适用于复杂企业级需求。文章通过特性对比、代码示例及常见问题解答,帮助开发者理解两者差异,合理选择方案。记住口诀:单机简单用注解,多节点上Quartz;若是任务要可靠,持久化配置不能少。
786 4
|
消息中间件 运维 数据库
Seata框架和其他分布式事务框架有什么区别
Seata框架和其他分布式事务框架有什么区别
491 153
|
存储 监控 数据可视化
常见的分布式定时任务调度框架
分布式定时任务调度框架用于在分布式系统中管理和调度定时任务,确保任务按预定时间和频率执行。其核心概念包括Job(任务)、Trigger(触发器)、Executor(执行器)和Scheduler(调度器)。这类框架应具备任务管理、任务监控、良好的可扩展性和高可用性等功能。常用的Java生态中的分布式任务调度框架有Quartz Scheduler、ElasticJob和XXL-JOB。
4778 66
|
11月前
|
安全 开发工具 git
git分布式版本控制系统及在码云上创建项目并pull和push
通过本文的介绍,我们详细讲解了Git的基本概念和工作流程,并展示了如何在码云上创建项目及进行pull和push操作。Git作为一种分布式版本控制系统,为开发者提供了强大的工具来管理代码变更和协作开发。希望本文能帮助您更好地理解和使用Git及码云,提高开发效率和代码质量。
543 18
|
11月前
|
安全 开发工具 git
git分布式版本控制系统及在码云上创建项目并pull和push
通过本文的介绍,我们详细讲解了Git的基本概念和工作流程,并展示了如何在码云上创建项目及进行pull和push操作。Git作为一种分布式版本控制系统,为开发者提供了强大的工具来管理代码变更和协作开发。希望本文能帮助您更好地理解和使用Git及码云,提高开发效率和代码质量。
358 16
|
数据采集 人工智能 分布式计算
MaxFrame:链接大数据与AI的高效分布式计算框架深度评测与实践!
阿里云推出的MaxFrame是链接大数据与AI的分布式Python计算框架,提供类似Pandas的操作接口和分布式处理能力。本文从部署、功能验证到实际场景全面评测MaxFrame,涵盖分布式Pandas操作、大语言模型数据预处理及企业级应用。结果显示,MaxFrame在处理大规模数据时性能显著提升,代码兼容性强,适合从数据清洗到训练数据生成的全链路场景...
662 5
MaxFrame:链接大数据与AI的高效分布式计算框架深度评测与实践!
|
人工智能 分布式计算 大数据
MaxFrame 产品评测:大数据与AI融合的Python分布式计算框架
MaxFrame是阿里云MaxCompute推出的自研Python分布式计算框架,支持大规模数据处理与AI应用。它提供类似Pandas的API,简化开发流程,并兼容多种机器学习库,加速模型训练前的数据准备。MaxFrame融合大数据和AI,提升效率、促进协作、增强创新能力。尽管初次配置稍显复杂,但其强大的功能集、性能优化及开放性使其成为现代企业与研究机构的理想选择。未来有望进一步简化使用门槛并加强社区建设。
566 8
|
分布式计算 大数据 数据处理
技术评测:MaxCompute MaxFrame——阿里云自研分布式计算框架的Python编程接口
随着大数据和人工智能技术的发展,数据处理的需求日益增长。阿里云推出的MaxCompute MaxFrame(简称“MaxFrame”)是一个专为Python开发者设计的分布式计算框架,它不仅支持Python编程接口,还能直接利用MaxCompute的云原生大数据计算资源和服务。本文将通过一系列最佳实践测评,探讨MaxFrame在分布式Pandas处理以及大语言模型数据处理场景中的表现,并分析其在实际工作中的应用潜力。
492 2
|
5月前
|
存储 负载均衡 NoSQL
【赵渝强老师】Redis Cluster分布式集群
Redis Cluster是Redis的分布式存储解决方案,通过哈希槽(slot)实现数据分片,支持水平扩展,具备高可用性和负载均衡能力,适用于大规模数据场景。
429 2