Giter VIP home page Giter VIP logo

googlellp / supermarket Goto Github PK

View Code? Open in Web Editor NEW
2.0K 62.0 693.0 148.42 MB

设计精良的网上商城系统,包括前端、后端、数据库、负载均衡、数据库缓存、分库分表、读写分离、全文检索、消息队列等,使用SpringCloud框架,基于Java开发。该项目可部署到服务器上,不断完善中……

License: GNU Affero General Public License v3.0

Java 81.66% HTML 13.49% ASP.NET 0.29% Classic ASP 0.86% PHP 1.18% Dockerfile 0.13% Shell 0.72% Ruby 1.66%
spring mybatis jquery html5 springcloud springboot redis mysql springmvc mycat elasticsearch rabbitmq javacript docker dockerfile

supermarket's Introduction

网上商城

截图展示

商品页面


注册页面


登录页面


购物车


订单


检索


管理商品


新增商品


秒杀


声明

  • 未经许可不得将本项目商用,如需商用请联系作者[email protected]
  • 欢迎各位看官向本项目提交代码,对提交的分支我将进行合并。提交时请务必保证能编译能运行

关键技术

基于SpringCloud框架开发的商城系统,代码严格遵循MVC分层**,可部署到服务器上,设计精良,不断完善

包结构说明

  • com.supermarket.*.filter对应于微服务的过滤器
  • com.supermarket.*.listener对应于微服务的监听器
  • com.supermarket.*.controller对应于微服务的controller层
  • com.supermarket.*.aspect对应于微服务的切面类
  • com.supermarket.*.service对应于微服务的service层
  • com.supermarket.*.dao对应于微服务的持久层
  • com.supermarket.*.schedule对应于微服务的定时任务
  • com.supermarket.*.exception对应于微服务的自定义异常
  • com.supermarket.*.domain对应于微服务的JavaBean
  • com.supermarket.*.utils对应于微服务的工具类
  • com.supermarket.*.vo对应于微服务的ViewObject

系统架构

架构图

功能说明

用户微服务(com.supermarket.user)

  • 用户登录
  • 免验证码登录
  • 用户注册
  • 用户登出
  • 用户名可用性校验
  • 用户登录状态获取
  • 查询用户权限等级

商品微服务(com.supermarket.product)

  • 分页查询
  • 单个商品查询
  • 商品新增
  • 商品修改
  • 查询全部商品

公共资源微服务(com.supermarket.common)

  • 公共资源微服务同时也是SpringCloud的Eureka模块
  • 提供了常用的JavaBean, ViewObject, 工具类

zuul网关(com.supermarket.gateway)

  • SpringCloud的zuul网关模块
  • 对敏感API调用进行后端鉴权

图片微服务(com.supermarket.image)

  • 图片上传功能
  • 生成验证码功能
  • 清除验证码在redis中的缓存

购物车微服务(com.supermarket.cart)

  • 购物车查询
  • 购物车新增商品
  • 购物车删除商品
  • 购物车修改商品
  • 购物车价格查询

订单微服务(com.supermarket.order)

  • 新增订单
  • 删除订单
  • 查询订单

检索微服务(com.supermarket.search)

  • 分页检索
  • 新增商品
  • 删除商品
  • 建立索引

秒杀微服务(com.supermarket.instantbuy)

  • 全部秒杀商品查询
  • 单个秒杀商品查询
  • 发起秒杀

新版本部署教程

  1. 环境准备
    1. 开发环境准备:首先你需要有一台电脑、以及一个虚拟机(或服务器)。内存要大,不然跑不起来。假设你的虚拟机的ip地址是192.168.137.147在你的电脑中把hosts追加两行。
      192.168.137.147 www.supermarket.com
      192.168.137.147 image.supermarket.com
      然后添加环境变量
      DOCKER_HOST=tcp://192.168.137.147:2375
    2. 部署环境准备:虚拟机装上centos7,再装上docker,然后依次拉取镜像
      docker pull zongxr/redis:3.2.11
      docker pull zongxr/mycat:1.15.1
      docker pull zongxr/mysql:5.7.37
      docker pull zongxr/elasticsearch:6.8.6
      docker pull zongxr/supermarket-cart:1.0-SNAPSHOT
      docker pull zongxr/supermarket-user:1.0-SNAPSHOT
      docker pull zongxr/supermarket-gateway:1.0-SNAPSHOT
      docker pull zongxr/supermarket-eureka:1.0-SNAPSHOT
      docker pull zongxr/supermarket-image:1.0-SNAPSHOT
      docker pull zongxr/supermarket-search:1.0-SNAPSHOT
      docker pull zongxr/supermarket-instantbuy:1.0-SNAPSHOT
      docker pull zongxr/supermarket-product:1.0-SNAPSHOT
      docker pull zongxr/supermarket-order:1.0-SNAPSHOT
      然后把start_up目录拷贝到/home目录下
  2. 部署数据库
    1. 参照数据库启动脚本逐行执行,或直接运行该脚本。
    2. 由于官方的mysql镜像没有默认开启主从配置,也没设定UTF-8字符集,因此使用本项目专有镜像zongxr/mysql:5.7.37,可自动开启bin-log,并设定UTF-8字符集
    3. 该脚本制作了双向主从高可用的数据分片,节点分别为dn1_host1, dn1_host2, dn2_host1, dn2_host2。并且通过mycat实现了读写分离以及分库分表,最后灌入数据。如果上一步执行成功了,你将能够通过以下的JDBC连接上数据库 jdbc:mysql://192.168.137.147:3306/supermarket连接dn1_host1
      jdbc:mysql://192.168.137.147:3307/supermarket连接dn1_host2
      jdbc:mysql://192.168.137.147:3308/supermarket连接dn2_host1
      jdbc:mysql://192.168.137.147:3309/supermarket连接dn2_host2
      jdbc:mysql://192.168.137.147:8066/supermarket连接mycat
  3. 部署redis集群
    1. 参照redis启动脚本逐行执行,或者直接运行该脚本
    2. 该脚本搭建了一个6节点的redis集群,从redis-1到redis-6。由于3.2.11版本的redis官方镜像没有预置ruby环境,而创建集群需要使用ruby环境。因此使用该项目的专用redis镜像zongxr/redis:3.2.11,该镜像已经集成了ruby环境可直接使用。
    3. 如果上一步redis集群部署成功了,那么可以通过6379端口连接该集群
  4. 部署elasticsearch集群
    1. 参照elasticsearch启动脚本逐行执行,或直接运行该脚本
    2. 该脚本搭建了3节点的elasticsearch集群,分别是es-1, es-2, es-3。如果部署成功了,可以使用head插件连接http://192.168.137.147:9200/地址,集群状态应为green
    3. 官方默认的elasticsearch镜像不带JDBC连接功能,该项目通过技术手段集成了JDBC连接功能,可以像操作关系型数据库一样写SQL去操作elasticsearch。使用的driver是org.elasticsearch.xpack.sql.jdbc.EsDriver
      jdbc:es://192.168.137.147:9201连接es-1
      jdbc:es://192.168.137.147:9202连接es-2
      jdbc:es://192.168.137.147:9200连接es-3
  5. 部署微服务
    1. 参照微服务启动脚本逐行执行,或直接运行该脚本
    2. 每个微服务做了一个镜像,名为zongxr/supermarket-xxx:tag。如果所有微服务都启动成功了,那么将能够在注册中心看到所有注册的微服务,并且在http://192.168.137.147:APP_PORT/swagger-ui/网址能够看到该微服务对应的接口文档
    3. instant-buy微服务用到了消息队列rabbitmq。如果消息队列启动成功,将能够在Web UI管理界面看到传输的消息,用户名密码均为guest
  6. 至此,所有服务都已经启动完成了。访问www.supermarket.com进行访问即可。

旧版本部署教程

交流群

点击链接加入群聊【Supermarket项目交流群】:https://jq.qq.com/?_wv=1027&k=wlpUy5jo
交流群二维码

版本迭代

版本号 更新内容 上传时间
0.1
  • 初步完成项目方案确定
  • 完成主页及注册页面的静态资源编写(包括js,css)
  • 完成主页及注册页面的动态资源编写(包括jsp,servlet)
  • 通过jdbc链接MySQL数据库
  • 使用c3p0连接池
2020年8月3日
0.1.1
  • 可以选择使用哪个连接池,并且在web.xml中指定,现在可以使用DBCP连接池
  • 通过反射机制设置如果指定连接池调用失败,再调用哪个连接池
  • 使用Ajax技术完成前端用户名校验
  • 更改img标签的src属性,使浏览器异步发送GET请求,刷新验证码
2020年8月7日
0.1.2
  • 使用JDBC.properties进行全局JDBC配置
  • 去掉JDBC的反射调用
  • 使用cookie完成记住用户名功能
  • 新增登录功能:使用session完成会话级别的数据传递
  • 新增注销功能:通过ajax请求销毁session
  • 修改静态页面,使其实现对logo.png的单独依赖
2020年8月9日
0.2
  • 使用EL表达式替换jsp页面中的脚本表达式
  • 修改静态资源,主页增加右下角浮动项以及向上返回功能
  • 新增错误页面提示
  • 重构为JavaEE三层经典架构
2020年8月20日
0.2.1
  • 使用过滤器完成全局请求参数拦截处理,包括全局中文乱码处理、全局password请求参数加密
  • 通过过滤器实现全局连接池选定
  • 通过过滤器实现自动登录功能
  • 通过装饰者模式扩展request对象
2020年8月22日
0.2.2
  • 使用log4j框架进行日志记录
  • 修复重启服务器自动登录失效的bug
  • 加入网页标题图标
2020年8月24日
0.2.3
  • 将JDBC初始化功能交由监听器实现,并移除对应的过滤器
  • 使用过滤器实现JDBC反注册,避免内存泄漏
  • 使用注解替代web.xml完成servlet和监听器的注册
  • 重写前端校验,对于支持html5的浏览器使用其自带校验
2020年8月25日
0.3
  • 使用Spring、SpringMVC、MyBatis重构该web应用
  • 使用User类的装饰者模式完成密码的md5加密功能
  • 使用SpringMVC拦截器记录用户的访问请求
  • 使用切面记录错误日志,并统计响应时间
  • 升级log4j为log4j2
  • 使用html5及jQuery相结合的方式进行前端表单校验
2020年9月25日
0.3.1
  • 新增UserDao的SpringJDBC模板类实现
  • 注册用户功能开启事务,修复多个用户同时注册时可能造成用户名重复的bug
2020年9月29日
0.3.2
  • 新增备选的DBCP连接池配置
2020年10月1日
0.4
  • 使用Maven重构依赖
  • 使用SpringCloud重构项目
  • 重构数据库
  • 增加common微服务: 存放共享资源(如JavaBean, 工具类, ViewObject)
  • 增加商品微服务:新增、修改、查询功能
  • 增加用户微服务:用户名校验、注册
  • 增加图片微服务:图片上传
  • 增加nginx:负载均衡、动态静态资源分离
2020年11月10日
0.4.1
  • 新增redis配置,通过redis解决微服务之间数据共享问题
  • 完善用户微服务:实现登录、登出、登录状态获取,修复注册未校验密码一致性的Bug
  • 改进redis数据结构:将key-value改进为hash,从而实现单点登录
  • 使用AOP切面实现登录时长的自动延长
2020年11月12日
0.4.2
  • 调整前端css,增加注册、登录的错误提示
  • 新增注册成功页面
  • 其他前端页面样式的调整
2020年11月13日
0.4.3
  • 完善图片微服务:新增功能生成验证码、验证码缓存清理(避免前端连续请求验证码爆掉redis)
  • 完善用户微服务:登录、注册需要验证码,登录或注册成功后清除验证码缓存(节省redis内存)
  • 完善商品微服务:商品新增、查询、编辑触发被动缓存
2020年11月14日
0.4.4
  • 新增购物车微服务:实现商品查询、商品新增、商品修改、商品删除功能
  • 通过事务实现购买数量后端校验:防止购买量大于库存
  • 在前端实现购买数量校验校验:防止购买量大于库存
2020年11月15日
0.4.5
  • 完善图片微服务:解决redis中验证码缓存无法清理的BUG
  • 完善用户微服务:新增记住用户名、自动登录功能
2020年11月18日
0.4.6
  • 修复自动登录与单点登录功能冲突的bug。
  • 替换网页logo,避免不必要的责任纠纷
2020年11月19日
0.4.7
  • 新增订单微服务:实现订单新增、订单查询、订单删除功能
  • 完善购物车微服务:新增总价格查询功能
  • 数据库迁移至服务器,并通过MyCat管理分库分表
  • 每个数据分片通过双向主从实现MySQL高可用,并开启读写分离
2020年11月24日
0.4.8
  • 新增检索微服务:分页检索、增加文档、删除文档、修改文档
  • 完善商品微服务:查询全部商品
  • 完善前端:进入后台权限校验
2020年11月30日
0.4.9
  • 新增秒杀微服务:全部秒杀商品查询、单个秒杀商品查询、发起秒杀(尚未联调)
  • 修改部分前端页面
2020年12月6日
0.5
  • 完善秒杀微服务:修复若干bug
  • 完善zuul网关:对敏感API调用进行后端鉴权
  • 完善用户微服务:新增权限查询功能
2020年12月9日
0.5.1
  • 完善秒杀微服务:修复发起秒杀功能中的线程安全问题
  • 新增全系统架构图
2020年12月10日
0.5.2
  • 完善秒杀微服务:修复发起秒杀功能中的线程安全问题
  • 完善zuul网关:过滤器实现更细粒度鉴权
  • 完善检索微服务:修复新增和编辑商品时无法同步到elasticsearch的Bug
2020年12月11日
0.5.3
  • 微服务添加熔断器,避免单个故障导致集群崩溃
  • 使用Feign替代Ribbon进行微服务间调用
  • 完善检索微服务:修复微服务启动时创建索引失败的bug
  • 新增log4j2日志框架支持
2020年12月12日
1.0.0
  • 所有服务及组件迁移至docker,使用容器进行部署
  • 修复若干BUG
2022年3月26日
1.0.1.0
  • 完善商品微服务接口文档
  • 修复若干BUG
2022年4月1日
1.0.2.0
  • 完善检索微服务接口
  • 修正检索微服务和商品微服务调用异常的BUG
2022年4月1日
1.0.3.0
  • 完善抢购微服务接口
  • 修复若干BUG
2022年4月4日
1.0.4.0
  • 完善图片微服务
  • 修复若干BUG
2022年4月5日

配置情况

遇到的bug锦集(作为记录,博君一笑)

  • HttpServletRequest的装饰者类HttpServletRequestDecorator中的getParameterMap重写方法中,不能在原地进行参数字符集修改,否则如果调用两次及以上该方法时会编码多次,造成乱码
  • 对请求参数的拦截处理,如果用到装饰者模式,最好在一处完成。如果在多处完成会造成请求参数的重复处理,从而导致乱码或者拿到非预期的参数值
  • 用户的登录信息最好保存到session域中,在0.2.1中的自动登录成功后误把user对象塞入到了request域,造成了重启服务器自动登录失效的bug。
  • 使用过滤器将密码加密,不仅要将参数为password的值加密,还要将参数为password2的值加密,否则永远确认密码不一致
  • 使用SpringMVC处理请求参数,如果自动封bean作为方法参数传入,那么该bean所携带的请求参数是不经过过滤器的!原因未知
  • 自动登录最好使用过滤器实现,因为SpringMVC拦截器无法拦截index.jsp。而因为使用过滤器的时候Spring容器还未初始化,所以过滤器的属性需要手工注入
  • 即便上一次响应设置了cookie的path,下一次请求携带的cookie的path也会是null。因此如果要修改或删除cookie,一定要严格满足name, path都设置,否则可能会无效
  • 如果间接依赖了spring-cloud-starter-netflix-eureka-server,一定要把jackson-dataformat-xml排除掉,因为如果不排除掉会导致返回给浏览器的对象是xml格式而非json格式
  • MyBatis如果使用bean传参,不要使用<if>标签内的test属性名应该对应bean属性名而非表的列名。否则会报There is no getter for property named 'xx' in 'class xxx
  • 在Springboot项目中,如果要在application.properties中写自定义配置项,若该配置项为路径字符串,一定要写全路径,从盘符写起,否则会自动在前面拼接tomcat的临时路径
  • html中onsubmit事件要放在form标签内,放在<input type="submit" />标签内无效
  • 启动的时候需要先启动redis再启动微服务,否则会报RedisConnectionFailureException
  • redis作为缓存使用时,建议捕获RedisConnectionFailureException异常,从而在redis崩掉的情况下能够直接访问数据库
  • 基于SpringCloud的Ribbon模块在微服务之间相互调用时,不经过zuul网关
  • MySQL主从高可用时,如果因为错误数据导致挂接失败,需要先取消挂接(stop slave),手动删除错误数据,再重新挂接(start slave)
  • MyCat1.5.1版本还不支持复杂SQL,比如update...select..., insert ...select...等
  • insert的时候加上列名是一个好习惯,因为MyCat1.5.1不支持省略列名的写法
  • 直接使用RedisTemplate模板类,键和值会出现类似\xac\xed\x00\x05t\x00的东西,此时要么不去手动查看redis输出,要么换用StringRedisTemplate
  • 用nginx也能做到外部调用内部api的拦截,只需要将提供给前端的api路径和提供给后端的api路径区分开。比如提供给前端的api路径是/manage/query,提供给后端的api是/backend/query,因为nginx拼接了"/manage",所以自然找不到/backend/query
  • @RequestParam用于接收单个query请求参数;@ModelAttribute用于接收Content-Type为application/x-www-form-urlencoded的多个query请求参数后封装成bean;@RequestBody用于接收Content-Type为application/json的封装成body的请求参数
    单个基本请求参数多个请求参数组成的一个bean
    query@RequestParam@ModelAttribute
    body@RequestBody
  • @ExceptionHandler注解修饰的方法别忘了加@ResponseBody注解,否则捕获不到异常

supermarket's People

Contributors

annaxchun avatar zongxr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

supermarket's Issues

[security vulnerability] Arbitrary Order Deletion Vulnerability

Recently, our team found an arbitrary order deletion vulnerability in the latest version of the project.

The vulnerability logic is present in the file: https://github.com/GoogleLLP/SuperMarket/blob/master/order/src/main/java/com/supermarket/order/controller/OrderController.java#L74. Unauthorized access to the /manage/delete/{orderId} API enables attackers to manipulate the path variable orderId and delete orders belonging to other users.
image

To address this vulnerability, we strongly recommend that developers implement access control policies to ensure that only privileged users or the order owner are authorized to perform the delete operation.

[security vulnerability] Arbitrary Cart Addition Vulnerability

Recently, our team found an arbitrary cart addition vulnerability in the latest version of the project.

The vulnerability logic is present in the file: https://github.com/GoogleLLP/SuperMarket/blob/master/cart/src/main/java/com/supermarket/cart/controller/CartController.java#L50. Access to the /manage/save API is unauthorized, allowing attackers to add carts for any user via a crafted cart object.

image

To address this vulnerability, we recommend that developers implement access control policies to ensure that users can only add items to their own cart.

[security vulnerability] Arbitrary Cart Details Access Vulnerability

Recently, our team found an arbitrary cart details access vulnerability in the latest version of the project.

The vulnerability logic is present in the file: https://github.com/GoogleLLP/SuperMarket/blob/master/cart/src/main/java/com/supermarket/cart/controller/CartController.java#L35.
image

Access to the /manage/query?userId= API is unauthorized, allowing attackers to manipulate the userId request parameter and access other users' cart details, potentially compromising user privacy data.

To address this vulnerability, we strongly recommend that developers implement access control policies to ensure that only privileged users or cart owners can access the information.

页面请求失败

大佬我按照容器化部署后服务都能启动,但页面上所有操作都弹窗请求失败,是什么问题

[security vulnerability] Arbitrary Order Detail Access Vulnerability

Recently, our team found an arbitrary order detail access vulnerability in the latest version of the project.
The vulnerability logic is present in the file: https://github.com/GoogleLLP/SuperMarket/blob/master/order/src/main/java/com/supermarket/order/controller/OrderController.java#L59. Access to the /manage/query/{userId} API is unauthorized, allowing attackers to manipulate the path variable userId and access other users' order details, potentially compromising user privacy data.

image

To address this vulnerability, we strongly recommend that developers implement access control policies to ensure that only privileged users or the owner can access the order information.

[security vulnerability] Arbitrary Order Addition Vulnerability

Recently, our team found an arbitrary order addition vulnerability in the latest version of the project.

The vulnerability logic is present in the file: https://github.com/GoogleLLP/SuperMarket/blob/master/order/src/main/java/com/supermarket/order/controller/OrderController.java#L36. Access to the /manage/save API is unauthorized, allowing attackers to add orders as any user via a crafted order object.

image

To address this vulnerability, we strongly recommend that developers implement access control policies to limit the order addition operation.

[security vulnerability] Arbitrary Cart Deletion Vulnerability

Recently, our team found an arbitrary cart deletion vulnerability in the latest version of the project.

The vulnerability logic is present in the file: https://github.com/GoogleLLP/SuperMarket/blob/master/cart/src/main/java/com/supermarket/cart/controller/CartController.java#L96. Access to the /manage/delete API is unauthorized, allowing attackers to delete arbitrary cart via a crafted cart object.

image

To address this vulnerability, we recommend that developers implement access control policies to limit API access to privileged users and cart owners only.

[security vulnerability] Arbitrary Cart Update Vulnerability

Recently, our team found an arbitrary cart update vulnerability in the latest version of the project.

The vulnerability logic is present in the file: https://github.com/GoogleLLP/SuperMarket/blob/master/cart/src/main/java/com/supermarket/cart/controller/CartController.java#L73. Access to the /manage/update API is unauthorized, allowing attackers to update the information of arbitrary cart via a crafted cart object.

image

To address this vulnerability, we recommend that developers implement access control policies to limit API access to privileged users and cart owners only.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.