栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > 架构设计

乐优商城第四篇:乐优商城介绍与项目搭建

架构设计 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

乐优商城第四篇:乐优商城介绍与项目搭建

项目介绍
  • 乐优商城是一个全品类的电商购物网站(B2C)。
  • 用户可以在线购买商品、加入购物车、下单
  • 可以评论已购买商品
  • 管理员可以在后台管理商品的上下架、促销活动
  • 管理员可以监控商品销售状况
  • 客服可以在后台处理退款操作
  • 希望未来3到5年可以支持千万用户的使用
系统架构

系统架构解读

整个乐优商城可以分为两部分:后台管理系统、前台门户系统。

  • 后台管理:

    • 后台系统主要包含以下功能:
      • 商品管理,包括商品分类、品牌、商品规格等信息的管理
      • 销售管理,包括订单统计、订单退款处理、促销活动生成等
      • 用户管理,包括用户控制、冻结、解锁等
      • 权限管理,整个网站的权限控制,采用JWT鉴权方案,对用户及API进行权限控制
      • 统计,各种数据的统计分析展示
    • 后台系统会采用前后端分离开发,而且整个后台管理系统会使用Vue.js框架搭建出单页应用(SPA)。
  • 前台门户

    • 前台门户面向的是客户,包含与客户交互的一切功能。例如:
      • 搜索商品
      • 加入购物车
      • 下单
      • 评价商品等等
    • 前台系统我们会使用Thymeleaf模板引擎技术来完成页面开发。出于SEO优化的考虑,我们将不采用单页应用。

无论是前台还是后台系统,都共享相同的微服务集群,包括:

  • 商品微服务:商品及商品分类、品牌、库存等的服务
  • 搜索微服务:实现搜索功能
  • 订单微服务:实现订单相关
  • 购物车微服务:实现购物车相关功能
  • 用户中心:用户的登录注册等功能
  • Eureka注册中心
  • Zuul网关服务
项目搭建 技术选型

前端技术:

  • 基础的HTML、CSS、JavaScript(基于ES6标准)
  • JQuery
  • Vue.js 2.0以及基于Vue的框架:Vuetify(UI框架)
  • 前端构建工具:WebPack
  • 前端安装包工具:NPM
  • Vue脚手架:Vue-cli
  • Vue路由:vue-router
  • ajax框架:axios
  • 基于Vue的富文本框架:quill-editor

后端技术:

  • 基础的SpringMVC、Spring 5.x和MyBatis3
  • Spring Boot 2.0.7版本
  • Spring Cloud 版本Finchley.SR2
  • Redis-4.0
  • RabbitMQ-3.4
  • Elasticsearch-6.3
  • nginx-1.14.2
  • FastDFS - 5.0.8
  • MyCat
  • Thymeleaf
  • mysql 5.6
开发环境

IDEA2020.1+JDK1.8+maven3.5.x+git

域名

我们在开发的过程中,为了保证以后的生产、测试环境统一。尽量都采用域名来访问项目。

一级域名:www.leyou.com,leyou.com leyou.cn

二级域名:manage.leyou.com/item , api.leyou.com

我们可以通过switchhost工具来修改自己的host对应的地址,只要把这些域名指向127.0.0.1,那么跟你用localhost的效果是完全一样的。

创建父工程

【New Project】>【Empty Project】>【Project Name:leyou】>【finish】

编写pom.xml


    org.springframework.boot
    spring-boot-starter-parent
    2.0.6.RELEASE
     



    UTF-8
    UTF-8
    1.8
    Finchley.SR2
    1.3.2
    2.0.2
    1.1.9
    5.1.32
    1.2.3
    1.0.0-SNAPSHOT
    1.26.1-RELEASE



    
        
        
            org.springframework.cloud
            spring-cloud-dependencies
            ${spring-cloud.version}
            pom
            import
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            ${mybatis.starter.version}
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            ${mapper.starter.version}
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            ${pageHelper.starter.version}
        
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
        
            com.github.tobato
            fastdfs-client
            ${fastDFS.client.version}
        
    



    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    

创建eureka注册中心服务

(1)创建工程

  • 之所以要用leyou-registry,为了提高项目的扩展性,方便更换其他注册中心

创建过程

【leyou父工程右键【new module】】>【Maven(不要选择Create from archetype)】>【Next】>

​ Name:leyou-registry

​ GroupId:com.leyou

​ ArtifactId:leyou-registry

】>

【Finish】

(2)编写pom.xml,导入eureka服务端依赖


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server

(3)编写application.yml,覆盖默认配置

  • 通过设置register-with-eureka:false,eureka就不会再注册自身
server:
  port: 10086
spring:
  application:
    name: leyou-registry
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
    register-with-eureka: false # 设置不注册eureka本身
    fetch-registry: false #不拉取eureka服务列表
  server:
    enable-self-preservation: false # 关闭自我保护
    eviction-interval-timer-in-ms: 10000 # 定期清除无效链接

(4)在引导类上添加相关注解

@SpringBootApplication
@EnableEurekaServer
public class LeyouRegistryApplication {
    public static void main(String[] args) {
        SpringApplication.run(LeyouRegistryApplication.class);
    }
}
创建商品微服务

分析

既然是一个全品类的电商购物平台,那么核心自然就是商品。因此我们要搭建的第一个服务,就是商品微服务。其中会包含对于商品相关的一系列内容的管理,包括:

  • 商品分类管理
  • 品牌管理
  • 商品规格参数管理
  • 商品管理
  • 库存管理

商品微服务的结构

leyou-item是一个微服务,它对外暴露接口,别的服务也会调用接口,获取接口数据,数据的封装需要实体类,所以接口需要关联实体类。

这里使用聚合工程,将要提供的接口和实体类分别放到不同的工程中,以后使用的时候,只需要导入坐标即可。

创建商品服务父工程(leyou-item),然后在其中创建两个子工程

  • leyou-item-interface,这个工程放pojo,异常类等,对外暴露接口,提供实体类
  • leyou-item-service,这个工程实现业务逻辑,对外提供REST接口

调用关系图:

创建商品微服务的父工程

(1)新建父工程

【leyou父工程右键【new module】】>【Maven(不要选择Create from archetype)】>【Next】>

​ Name:leyou-item

​ GroupId:com.leyou.item

​ ArtifactId:leyou-item

】>

【Finish】

(2)编写pom.xml

  • 因为要创建的是一个聚合工程,所以必须在pom.xml中的packaging标签指定为pom

pom
创建leyou-item-interface

(1)新建工程

【leyou-item父工程右键【new module】】>【Maven(不要选择Create from archetype)】>【Next】>

这里父工程一定要选择leyou-item

​ Name:leyou-item-interface

​ GroupId:com.leyou.item

​ ArtifactId:leyou-item-interface

】>

【Finish】

(2)新建pojo包:com.leyou.item.pojo

创建leyou-item-service

(1)新建工程

【leyou-item父工程右键【new module】】>【Maven(不要选择Create from archetype)】>【Next】>

这里父工程一定要选择leyou-item

​ Name:leyou-item-service

​ GroupId:com.leyou.item

​ ArtifactId:leyou-item-service

】>

【Finish】

(2)编写pom.xml

spring-boot-starter-actuator模块

spring-boot-starter-actuator模块是一个spring提供的监控模块。

我们在开运行发过程中,需要实时和定时监控服务的各项状态和可用性。

Spring Boot的spring-boot-starter-actuator 模块(健康监控)功能提供了很多监控所需的接口,可以对应用系统进行配置查看、相关功能统计等。

项目使用依赖介绍

  • web
  • mybatis
  • mysql
  • jdbc
  • 通用mapper
  • eureka-client
  • 特别注意:要使用leyou-item-interface的pojo包的类,所以要引入这个依赖

    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.boot
        spring-boot-starter-jdbc
    
    
        mysql
        mysql-connector-java
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
    
    
        com.github.pagehelper
        pagehelper-spring-boot-starter
    
    
        com.leyou.item
        leyou-item-interface
        1.0.0-SNAPSHOT
    
    
        org.springframework.boot
        spring-boot-starter-test
    
    
    
        org.springframework.boot
        spring-boot-starter-actuator
    

(3)编写application.yml,覆盖默认配置

server:
  port: 8081
spring:
  application:
    name: item-service
  datasource:
    url: jdbc:mysql:///leyou
    username: root
    password: 123456
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  instance:
    lease-renewal-interval-in-seconds: 5 # 心跳时间
    lease-expiration-duration-in-seconds: 15 #过期时间
mybatis:
  type-aliases-package: com.leyou.item.pojo # 设置扫描包别名

(4)在引导类上添加相关注解

@SpringBootApplication
@EnableDiscoveryClient
public class LeyouItemServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(LeyouItemServiceApplication.class);
    }
}
创建zuul网关工程

(1)新建工程

【leyou】父工程右键【new module】】>【Maven(不要选择Create from archetype)】>【Next】>

这里父工程一定要选择leyou

​ Name:leyou-gateway

​ GroupId:com.leyou

​ ArtifactId:leyou-gateway

】>

【Finish】

(2)编写pom.xml

  • eureka-client
  • netflix-zuul

    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client


    org.springframework.cloud
    spring-cloud-starter-netflix-zuul

(3)编写application.yml,覆盖默认配置

  • 添加对leyou-service的路由规则
server:
  port: 10010
spring:
  application:
    name: leyou-gateway
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
    registry-fetch-interval-seconds: 5 # 5s抓取一次服务列表
zuul:
  prefix: /api
  routes:
    item-service: /item/** #路由到商品的微服务

(4)在引导类上添加相关注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class LeyouGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(LeyouGatewayApplication.class);
    }
}
创建common工程

这个工程作为utils工具类等公用

(1)新建工程

【leyou】父工程右键【new module】】>【Maven(不要选择Create from archetype)】>【Next】>

这里父工程一定要选择leyou

​ Name:leyou-common

​ GroupId:com.leyou.common

​ ArtifactId:leyou-common

】>

【Finish】

测试

访问:http://localhost:10086/:

然后点击:

新打开的标签页出现:

表示项目搭建成功。

PS:测试中链接可以点是因为我们使用了actuator模块,它可以对应用程序进行监护和查看,如果没有使用,页面会显示:

项目完整结构

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/912508.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号