博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
乐优商城总结
阅读量:2388 次
发布时间:2019-05-10

本文共 6876 字,大约阅读时间需要 22 分钟。

文章目录

收获一:明白了电商系统中的一些知识点,例如sku和spu的概念,另外,类目、品牌、商品、规格参数、规格参数组的设计以及它们关系也是相对复杂的,学完之后自己也能梳理清楚了。

收获二:之前以为在微服务中,所有的请求都会过网关,由网关进行路由,但是并不是这样。例如上传服务,如果上传文件也经Zuul网关,会造成不必要的网络负担,在高并发的情况下,有可能会造成整个系统的瘫痪,因此文件上传的请求是不经过网关来处理的。

收获三:库表设计的技巧。像spu和spuDetail会分在不同的表里,主要是spu和spuDetail读的频率不一样,并且spuDetail中商品详情属于大字段,分开存储会好一些,同样的,sku和库存也会分开存储,这是因为读写的频率不一样。另外,常规库表设计应该遵循三范式,但是有时候字段冗余可以减少不必要查询,这是值得的。

收获四:学习了JWT,包括JWT的概念、数据格式和交互流程,之前是听说过,这次是能亲自实战,感觉收获满满。

收获五:整合SpringBoot+ElasticSearch完成商品搜索,以后有需要用到搜索引擎相关的东西,可以直接拿过来进行优化。

一、架构

在这里插入图片描述

在这里插入图片描述

二、项目搭建

前端技术:

在这里插入图片描述
后端技术:
在这里插入图片描述

创建父工程

创建统一的父工程:leyou,用来管理依赖及其版本,注意是创建project,而不是module

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

在这里插入图片描述

创建leyou-registry:服务的注册中心(EurekaServer)

我们的注册中心,起名为:leyou-registry

在这里插入图片描述
添加依赖
编写启动类

@SpringBootApplication@EnableEurekaServer

配置文件

server:  port: 10086spring:  application:    name: leyou-registryeureka:  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: 5000 # 每隔5秒钟,进行一次服务列表的清理

创建leyou-gateway:服务网关(Zuul)

这里我们需要添加Zuul和EurekaClient的依赖:

@SpringBootApplication@EnableDiscoveryClient@EnableZuulProxy
server:  port: 10010spring:  application:    name: api-gateway  datasource:    url: jdbc:mysql://localhost:3306/leyou    username: root    password: 123    hikari:      maximum-pool-size: 30      minimum-idle: 10eureka:  client:    serviceUrl:      defaultZone: http://127.0.0.1:10086/eureka    registryFetchIntervalSeconds: 10  instance:    preferIpAddress: true    ipAddress: 127.0.0.1    instanceId: ${spring.application.name}:${server.port}zuul:  prefix: /api # 添加路由前缀  retryable: true  routes:    item-service: /item/** # 将商品微服务映射到/item/**    search-service: /search/**    user-service: /user/**    sms-service: /sms/**    auth-service: /auth/**    cart-service: /cart/**    order-service: /order/**    upload-service:      path: /upload/**      serviceId: upload-service      strip-prefix: false  host:      socket-timeout-millis: 100000      connect-timeout-millis: 100000  add-host-header: true #携带请求本身的host头信息  sensitive-headers: #禁止使用头的信息,设置为null,否则set-cookie无效ribbon:  ConnectTimeout: 100000 # 连接超时时间(ms)  ReadTimeout: 100000 # 通信超时时间(ms)  MaxAutoRetriesNextServer: 0 # 同一服务不同实例的重试次数  MaxAutoRetries: 0 # 同一实例的重试次数hystrix:  command:    default:      execution:        isolation:          thread:            timeoutInMillisecond: 100000 # 熔断超时时长:5000msly:  jwt:    pubKeyPath: F:/IdeaProjects/HwjProjects/auth/rsa/rsa.pub # 公钥地址    cookieName: LY_TOKEN  filter:    allowPaths:      - /api/auth      - /api/search      - /api/user/register      - /api/user/check      - /api/user/code      - /api/item #临时放行,因为需要用登录      - /zuul/api/upload

创建leyou-item: 商品微服务

在这里插入图片描述

leyou-item是一个微服务,那么将来肯定会有其它系统需要来调用服务中提供的接口,获取的接口数据,也需要对应的实体类来封装,因此肯定也会使用到接口中关联的实体类。

因此这里我们需要使用聚合工程,将要提供的接口及相关实体类放到独立子工程中,以后别人引用的时候,只需要知道坐标即可。

  • leyou-item-interface:主要是对外暴露的接口及相关实体类
  • leyou-item-service:所有业务逻辑及内部使用接口

在这里插入图片描述

因为是聚合工程,所以把项目打包方式设置为pom

pom

在这里插入图片描述

  • leyou-item-service中添加依赖:
    这些依赖,我们在顶级父工程:leyou中已经添加好了。所以直接引入即可:
- Eureka客户端- web启动器- mybatis启动器- 通用mapper启动器- 分页助手启动器 
com.github.pagehelper
pagehelper-spring-boot-starter
- 连接池,我们用默认的Hykira- mysql驱动- 千万不能忘了,我们自己也需要ly-item-interface中的实体类- 工具类
org.springframework.boot
spring-boot-starter-actuator

在整个leyou-item工程中,只有leyou-item-service是需要启动的。因此在其中编写启动类即可:

全局属性文件:

@SpringBootApplication@EnableDiscoveryClient
server:  port: 8081spring:  application:    name: item-service  datasource:    url: jdbc:mysql:///leyou    username: root    password: 123    hikari:      max-lifetime: 28830000 # 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';)      maximum-pool-size: 9 # 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)eureka:  client:    service-url:      defaultZone: http://localhost:10086/eureka  instance:    lease-renewal-interval-in-seconds: 5 # 5秒钟发送一次心跳    lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期mybatis:  type-aliases-package: com.leyou.item.pojo

创建leyou-common: 通用工具

后台管理系统搭建

在这里插入图片描述

安装依赖
我们只需要打开终端,进入项目目录,输入:npm install命令,即可安装这些依赖。

运行测试

F:\workspaces\IdeaProjects\leyou-manage-web>

npm run start

在这里插入图片描述

访问:

页面布局(Layout组件是整个页面的布局组件)

环境配置

使用域名访问本地项目

主域名是:www.leyou.com,

管理系统域名:manage.leyou.com
网关域名:api.leyou.com

  • 本地域名解析

Windows下的hosts文件地址:C:/Windows/System32/drivers/etc/hosts

Linux下的hosts文件所在路径: /etc/hosts

127.0.0.1 api.leyou.com127.0.0.1 manage.leyou.com192.168.31.128 image.leyou.com127.0.0.1 www.leyou.com
  • 反向代理配置
启动:start nginx.exe停止:nginx.exe -s stop重新加载:nginx.exe -s reload

在这里插入图片描述

#ngnix.confworker_processes  1;events {
worker_connections 1024;}http {
include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server {
listen 80; server_name www.leyou.com; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location /item {
# 先找本地 root html; if (!-f $request_filename) {
#请求的文件不存在,就反向代理 proxy_pass http://127.0.0.1:8084; break; } } location / {
proxy_pass http://127.0.0.1:9002; proxy_connect_timeout 600; proxy_read_timeout 600; }} server {
listen 80; server_name manage.leyou.com; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; location / {
proxy_pass http://127.0.0.1:9001; proxy_connect_timeout 600; proxy_read_timeout 600; } } server {
listen 80; server_name api.leyou.com; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 上传路径的映射 location /api/upload {
proxy_pass http://127.0.0.1:8082; proxy_connect_timeout 600; proxy_read_timeout 600; rewrite "^/api/(.*)$" /$1 break; } location / {
proxy_pass http://127.0.0.1:10010; proxy_connect_timeout 600; proxy_read_timeout 600; } } }

搭建前台系统

leyou-portal

leyou-portal 前台页面

start nginx live-server --port=9002

live-server

没有webpack,我们就无法使用webpack-dev-server运行这个项目,实现热部署。

所以,这里我们使用另外一种热部署方式:live-server

安装,使用npm命令即可,这里建议全局安装,以后任意位置可用

npm install -g live-server

运行时,直接输入命令:

live-server --port=9002

在这里插入图片描述

转载地址:http://wtxab.baihongyu.com/

你可能感兴趣的文章
设计模式、框架和架构的联系
查看>>
安装VMware虚拟机
查看>>
常用的设计模式和代码
查看>>
桥接模式-通俗的理解(转)
查看>>
MXML 文件中的xmlns是什么意思?
查看>>
Flex Builder 中的工作空间、项目
查看>>
Flex 获得远程数据
查看>>
Flex 添加效果的两种方法
查看>>
Flash Builder 4字体设置
查看>>
Actionscript 3.0 笔记一
查看>>
图像处理库OpenCV参考网址
查看>>
dllimport与dllexport作用与区别
查看>>
OpenGL坐标系
查看>>
C++用new和不用new创建类对象区别
查看>>
C++ C# JAVA 创建对象
查看>>
齐次坐标的理解
查看>>
QT配置文件
查看>>
QT .pro配置文件2
查看>>
Qt 模态与非模态对话框
查看>>
Qt C++中的关键字explicit .
查看>>