乐优商城(四十二)秒杀

一、创建秒杀服务

1.1 创建module

1.2 pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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>leyou</artifactId>
        <groupId>com.leyou.parent</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.leyou.seckill</groupId>
    <artifactId>leyou-secskill</artifactId>

    <dependencies>
        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--商品微服务-->
        <dependency>
            <groupId>com.leyou.item.interface</groupId>
            <artifactId>leyou-item-interface</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

1.3 application.yml

server:
  port: 8090
spring:
  application:
    name: secskill-service
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 192.168.19.121:9300
  rabbitmq:
    virtual-host: /leyou
    username: /leyou
    password: leyou
    host: 192.168.19.121
  jackson:
    default-property-inclusion: non_null # 配置json处理时忽略空值
  resources:
    add-mappings: true
    chain:
      enabled: true
      gzipped: true
      html-application-cache: true
    cache:
      period: 3600m
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
    registry-fetch-interval-seconds: 5
  instance:
    instance-id: ${spring.application.name}:${server.port}
    prefer-ip-address: true  #当你获取host时,返回的不是主机名,而是ip
    ip-address: 127.0.0.1
    lease-expiration-duration-in-seconds: 10 #10秒不发送九过期
    lease-renewal-interval-in-seconds: 5 #每隔5秒发一次心跳

二、页面分析

2.1 页面效果

秒杀商品展示页面:

秒杀商品详情页面:

2.2 需要的数据

2.2.1 秒杀商品列表页面

image、title、price,seckill_stock

2.2.2 秒杀商品详情页面

和普通商品详情页面一样

2.2.3 最终数据结构

package com.leyou.seckill.vo;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.util.Date;

/**
 * @Author: 98050
 * @Time: 2018-11-10 11:48
 * @Feature: 秒杀商品对象
 */
@Table(name = "tb_seckill_spu")
public class SeckillGoods {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private Long spuId;
    private Date startTime;
    private Date endTime;
    private Double seckillPrice;
    private String title;
    private String subTitle;
    private String image;
    
    @Transient
    private Integer stock;

}

三、后台开发

3.1 设置秒杀商品

3.1.1 功能需求

根据传入的商品id数组,将其设置为可以秒杀的商品。

3.1.2 Controller

 

3.1.3 Mapper

3.1.4 Service

 

3.2 秒杀商品查询