1. 程式人生 > >SSM實現秒殺系統案例

SSM實現秒殺系統案例

---------------------------------------------------------------------------------------------
[版權申明:本文系作者原創,轉載請註明出處] 
文章出處:http://blog.csdn.net/sdksdk0/article/details/52997034
作者:朱培      ID:sdksdk0     

--------------------------------------------------------------------------------------------

好久沒寫部落格了,因為一直在忙專案和其他工作中的事情,最近有空,剛好看到了一個秒殺系統的設計,感覺還是非常不錯的一個系統,於是在這裡分享一下。

秒殺場景主要兩個點:
1:流控系統,防止後端過載或不必要流量進入,因為慕課要求課程的長度和簡單性,沒有加。
2:減庫存競爭,減庫存的update必然涉及exclusive lock ,持有鎖的時間越短,併發性越高。

對於搶購系統來說,首先要有可搶購的活動,而且這些活動具有促銷性質,比如直降500元。其次要求可搶購的活動類目豐富,使用者才有充分的選擇性。馬上就雙十一了,使用者剁手期間增量促銷活動量非常多,可能某個活動力度特別大,大多使用者都在搶,必然對系統是一個考驗。這樣搶購系統具有秒殺特性,併發訪問量高,同時使用者也可選購多個限時搶商品,與普通商品一起進購物車結算。這種大型活動的負載可能是平時的幾十倍,所以通過增加硬體、優化瓶頸程式碼等手段是很難達到目標的,所以搶購系統得專門設計。

在這裡以秒殺單個功能點為例,以ssm框架+mysql+redis等技術來說明。

一、資料庫設計

使用mysql資料庫:這裡主要是兩個表,主要是一個商品表和一個購買明細表,在這裡使用者的購買資訊的登入註冊這裡就不做了,使用者購買時需要使用手機號碼來進行秒殺操作,購買成功使用的是商品表id和購買明細的使用者手機號碼做為雙主鍵。

CREATE  DATABASE seckill;
USE seckill;

CREATE TABLE seckill(
	seckill_id  BIGINT NOT NULL AUTO_INCREMENT COMMENT '商品庫存id',
	`name`  VARCHAR(120)  NOT NULL COMMENT '商品名稱',
	number INT NOT NULL COMMENT '庫存數量',
	start_time TIMESTAMP NOT NULL COMMENT '秒殺開啟時間',
	end_time TIMESTAMP NOT NULL COMMENT '秒殺結束時間',
	create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間',
	PRIMARY KEY (seckill_id),
	KEY idx_start_time(start_time),
	KEY idx_end_time(end_time),
	KEY idx_create_time(create_time)

)ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8 COMMENT '秒殺庫存表'


--初始化資料
INSERT INTO seckill(NAME,number,start_time,end_time)
VALUES
('4000元秒殺ipone7',300,'2016-11-5 00:00:00','2016-11-6 00:00:00'),
('3000元秒殺ipone6',200,'2016-11-5 00:00:00','2016-11-6 00:00:00'),
('2000元秒殺ipone5',100,'2016-11-5 00:00:00','2016-11-6 00:00:00'),
('1000元秒殺小米5',100,'2016-11-5 00:00:00','2016-11-6 00:00:00');

--秒殺成功明細表
--使用者登入認證相關的資訊
CREATE TABLE success_kill(
	seckill_id  BIGINT NOT NULL AUTO_INCREMENT COMMENT '秒殺商品id',
	user_phone  BIGINT NOT NULL COMMENT '使用者手機號',
	state  TINYINT NOT NULL DEFAULT-1 COMMENT '狀態標識,-1無效,0成功,1已付款',
	create_time TIMESTAMP NOT NULL COMMENT '建立時間',
	PRIMARY KEY(seckill_id,user_phone),
	KEY idx_create_time(create_time)
)ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8 COMMENT '秒殺成功明細表'

SELECT * FROM seckill;
SELECT * FROM success_kill;

這裡我們還用到了一個儲存過程,所以我們新建一個儲存過程來處理,對於近日產品公司來說儲存過程的使用還是比較多的,所以儲存過程也還是要會寫的。
DELIMITER $$

CREATE PROCEDURE seckill.execute_seckill
  (IN v_seckill_id BIGINT, IN v_phone BIGINT,
   IN v_kill_time TIMESTAMP, OUT r_result INT)
  BEGIN
    DECLARE insert_count INT DEFAULT 0;
    START TRANSACTION;
    INSERT IGNORE INTO success_kill(seckill_id,user_phone,create_time,state)
        VALUE(v_seckill_id,v_phone,v_kill_time,0);
    SELECT ROW_COUNT() INTO insert_count;
    IF(insert_count = 0) THEN
       ROLLBACK;
       SET r_result = -1;
    ELSEIF(insert_count < 0) THEN
       ROLLBACK;
       SET r_result = -2;
    ELSE
       UPDATE seckill
       SET number = number - 1
       WHERE seckill_id = v_seckill_id
         AND end_time > v_kill_time
         AND start_time < v_kill_time
         AND number > 0;
       SELECT ROW_COUNT() INTO insert_count;
       IF(insert_count = 0) THEN
         ROLLBACK;
         SET r_result = 0;
       ELSEIF (insert_count < 0) THEN
          ROLLBACK;
          SET r_result = -2;
        ELSE 
          COMMIT;
          SET r_result = 1;
        END IF; 
    END IF;
   END;
$$

DELIMITER ;

SET @r_result = -3;
CALL execute_seckill(1000,13813813822,NOW(),@r_result);
SELECT @r_result;

先可以看一下頁面的展示情況:


二、實體類

因為我們有兩個表,所以自然建兩個實體bean啦!新建一個Seckill.java

	private long seckillId;
	private String name;
	private int number;
	private Date startTime;
	private Date endTime;
	private Date createTime;

實現其getter/setter方法。

再新建一個SuccessKill。

private long seckillId;
	private long userPhone;
	private short state;
	private Date createTime;
	
	private Seckill seckill;

實現其getter/setter方法。

三、DAO介面層

介面我們也是來兩個:SeckillDao.java和SuccessKillDao.java

內容分別為:

public interface SeckillDao {
	
	//減庫存
	int reduceNumber(@Param("seckillId")long seckillId,@Param("killTime")Date killTime);
	
	Seckill queryById(long seckilled);
	
	List<Seckill>  queryAll(@Param("offset") int offset,@Param("limit") int limit);
	 public void seckillByProcedure(Map<String, Object> paramMap);
}

public interface SuccessKillDao {
	
	/**
	 * 插入購買明細
	 * 
	 * @param seckillId
	 * @param userPhone
	 * @return
	 */
	int insertSuccessKill(@Param("seckillId")long seckillId,@Param("userPhone")long userPhone);
	
	/**
	 * 根據id查詢
	 * 
	 * @param seckill
	 * @return
	 */
	SuccessKill  queryByIdWithSeckill(@Param("seckillId")long seckillId,@Param("userPhone")long userPhone);
	

}

四、mapper處理

在mybatis中對上面的介面進行實現,這裡可以通過mybatis來實現。

 <mapper  namespace="cn.tf.seckill.dao.SeckillDao">
  		<update id="reduceNumber"  >
  		update  seckill set number=number-1  where seckill_id=#{seckillId}
  		and start_time <![CDATA[<=]]>#{killTime}
  		and end_time>=#{killTime}
  		and number >0
  	</update>
  	
  	<select id="queryById"  resultType="Seckill"  parameterType="long">
  			select  seckill_id,name,number,start_time,end_time,create_time
  			from seckill
  			where seckill_id =#{seckillId}
  	</select>
  	
  	<select id="queryAll"  resultType="Seckill">
  		select  seckill_id,name,number,start_time,end_time,create_time
  			from seckill
  			order by create_time desc
  			limit #{offset},#{limit}
  	</select>
  	
  <select id="seckillByProcedure" statementType="CALLABLE">
        call execute_seckill(
           #{seckillId,jdbcType=BIGINT,mode=IN},
           #{phone,jdbcType=BIGINT,mode=IN},
           #{killTime,jdbcType=TIMESTAMP,mode=IN},
           #{result,jdbcType=INTEGER,mode=OUT}
        )
    </select>
  
  </mapper>

<mapper namespace="cn.tf.seckill.dao.SuccessKillDao">
  	<insert id="insertSuccessKill">
  			insert ignore into success_kill(seckill_id,user_phone,state)
  			values (#{seckillId},#{userPhone},0)
  	</insert>
  
  <select id="queryByIdWithSeckill"  resultType="SuccessKill">
  		select 
  			sk.seckill_id,
  			sk.user_phone,
  			sk.create_time,
  			sk.state,
  			s.seckill_id   "seckill.seckill_id",
  			s.name   "seckill.name",
  			s.number  "seckill.number",
  			s.start_time  "seckill.start_time",
  			s.end_time  "seckill.end_time",
  			s.create_time  "seckill.create_time"	
  		from success_kill sk
  		inner join seckill s on sk.seckill_id=s.seckill_id
  		where sk.seckill_id=#{seckillId} and sk.user_phone=#{userPhone} 
  
  </select>
  	
  	
</mapper>

五、redis快取處理

在這裡我們說的庫存不是真正意義上的庫存,其實是該促銷可以搶購的數量,真正的庫存在基礎庫存服務。使用者點選『提交訂單』按鈕後,在搶購系統中獲取了資格後才去基礎庫存服務中扣減真正的庫存;而搶購系統控制的就是資格/剩餘數。傳統方案利用資料庫行鎖,但是在促銷高峰資料庫壓力過大導致服務不可用,目前採用redis叢集(16分片)快取促銷資訊,例如促銷id、促銷剩餘數、搶次數等,搶的過程中按照促銷id雜湊到對應分片,實時扣減剩餘數。當剩餘數為0或促銷刪除,價格恢復原價。

這裡使用的是redis來進行處理。這裡使用的是序列化工具RuntimeSchema。

在pom.xml中配置如下:

<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.7.2</version>
		</dependency>

		<dependency>
			<groupId>com.dyuproject.protostuff</groupId>
			<artifactId>protostuff-api</artifactId>
			<version>1.0.8</version>
		</dependency>
		<dependency>
			<groupId>com.dyuproject.protostuff</groupId>
			<artifactId>protostuff-core</artifactId>
			<version>1.0.8</version>
		</dependency>
		<dependency>
			<groupId>com.dyuproject.protostuff</groupId>
			<artifactId>protostuff-runtime</artifactId>
			<version>1.0.8</version>
		</dependency>


然後我們引入之後,直接在這個dao中進行處理即可,就是把資料從redis中讀取出來以及把資料存到redis中,如果redis中有這個資料就直接讀,如果沒有就存進去。
public class RedisDao {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private JedisPool jedisPool;
	private int port;
	private String ip;

	public RedisDao(String ip, int port) {
		this.port = port;
		this.ip = ip;
	}

	//Serialize function
	private RuntimeSchema<Seckill> schema = RuntimeSchema.createFrom(Seckill.class);

	public Seckill getSeckill(long seckillId) {
		jedisPool = new JedisPool(ip, port);
		//redis operate
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "seckill:" + seckillId;
				//由於redis內部沒有實現序列化方法,而且jdk自帶的implaments Serializable比較慢,會影響併發,因此需要使用第三方序列化方法.
				byte[] bytes = jedis.get(key.getBytes());
				if(null != bytes){
					Seckill seckill = schema.newMessage();
					ProtostuffIOUtil.mergeFrom(bytes,seckill,schema);
					//reSerialize
					return seckill;
				}
			} finally {
				jedisPool.close();
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}

		return null;
	}

	public String putSeckill(Seckill seckill) {
		jedisPool = new JedisPool(ip, port);
		//set Object(seckill) ->Serialize -> byte[]
		try{
			Jedis jedis = jedisPool.getResource();
			try{
				String key = "seckill:"+seckill.getSeckillId();
				byte[] bytes = ProtostuffIOUtil.toByteArray(seckill, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
				//time out  cache
				int timeout = 60*60;
				String result = jedis.setex(key.getBytes(),timeout,bytes);
				return result;
			}finally {
				jedisPool.close();
			}
		}catch (Exception e){
			logger.error(e.getMessage(),e);
		}
		return null;
	}
}

還需要在spring中進行配置:我這裡的地址使用的是我伺服器的地址。
	<bean id="redisDao" class="cn.tf.seckill.dao.cache.RedisDao">
		<constructor-arg index="0" value="115.28.16.234"></constructor-arg>
        <constructor-arg index="1" value="6379"></constructor-arg>
	</bean> 



六、service介面及其實現

接下來就是service的處理了。這裡主要是由兩個重要的業務介面。

1、暴露秒殺 和 執行秒殺 是兩個不同業務,互不影響  2、暴露秒殺 的邏輯可能會有更多變化,現在是時間上達到要求才能暴露,說不定下次加個別的條件才能暴露,基於業務耦合度考慮,分開比較好。3、重新更改暴露秒殺介面業務時,不會去影響執行秒殺介面,對於測試都是有好處的。。。另外 不好的地方是前端需要呼叫兩個接口才能執行秒殺。

//從使用者角度設計介面,方法定義粒度,引數,返回型別
public interface SeckillService {
	
	List<Seckill>  getSeckillList();
	
	Seckill getById(long seckillId);
	//輸出秒殺開啟介面地址
	Exposer  exportSeckillUrl(long seckillId);

	/**
	 * 執行描述操作
	 * 
	 * @param seckillId
	 * @param userPhone
	 * @param md5
	 */
	SeckillExecution executeSeckill(long seckillId,long userPhone,String md5)  throws SeckillCloseException,RepeatKillException,SeckillException;
	  /**
     * 通過儲存過程執行秒殺
     * @param seckillId
     * @param userPhone
     * @param md5 
     */
    SeckillExecution executeSeckillByProcedure(long seckillId, long userPhone, String md5); 

    
}  
    


實現的過程就比較複雜了,這裡加入了前面所說的儲存過程還有redis快取。這裡做了一些異常的處理,以及資料字典的處理。

@Service
public class SeckillServiceImpl implements SeckillService{

	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SeckillDao  seckillDao;
	@Autowired
	private SuccessKillDao successKillDao;
	 @Autowired
	private RedisDao redisDao;
	
	//加鹽處理
	private final String slat="xvzbnxsd^&&*)(*()kfmv4165323DGHSBJ";


	public List<Seckill> getSeckillList() {
		return seckillDao.queryAll(0, 4);
	}

	public Seckill getById(long seckillId) {
		return seckillDao.queryById(seckillId);
	}


	public Exposer exportSeckillUrl(long seckillId) {

        //優化點:快取優化

        Seckill seckill = redisDao.getSeckill(seckillId);
        if (seckill == null) {
            //訪問資料庫
            seckill = seckillDao.queryById(seckillId);
            if (seckill == null) {
                return new Exposer(false, seckillId);
            } else {
                //放入redis
                redisDao.putSeckill(seckill);
            }
        }


        Date startTime = seckill.getStartTime();
        Date endTime = seckill.getEndTime();
        //當前系統時間
        Date nowTime = new Date();
        if (nowTime.getTime() < startTime.getTime()
                || nowTime.getTime() > endTime.getTime()) {
            return new Exposer(false, seckillId, nowTime.getTime(), startTime.getTime(), endTime.getTime());
        }
        //轉換特定字串的過程,不可逆
        String md5 = getMD5(seckillId);
        return new Exposer(true, md5, seckillId);
    }


    @Transactional
    public SeckillExecution executeSeckill(long seckillId, long userPhone, String md5)
            throws SeckillException, RepeatKillException, SeckillCloseException {
        if (md5 == null || (!md5.equals(getMD5(seckillId)))) {
            throw new SeckillException("Seckill data rewrite");
        }
        //執行秒殺邏輯:減庫存,記錄購買行為
        Date nowTime = new Date();
        try {

            //記錄購買行為
            int insertCount = successKillDao.insertSuccessKill(seckillId, userPhone);
            if (insertCount <= 0) {
                //重複秒殺
                throw new RepeatKillException("Seckill repeated");
            } else {
                //減庫存
                int updateCount = seckillDao.reduceNumber(seckillId, nowTime);
                if (updateCount <= 0) {
                    //沒有更新到記錄,秒殺結束
                    throw new SeckillCloseException("Seckill is closed");
                } else {
                    //秒殺成功
                    SuccessKill successKilled = successKillDao.queryByIdWithSeckill(seckillId, userPhone);
                    return new SeckillExecution(seckillId, SeckillStatEnum.SUCCESS, successKilled);

                }
            }


        } catch (SeckillCloseException e1) {
            throw e1;
        } catch (RepeatKillException e2) {
            throw e2;
        } catch (Exception e) {
            logger.error(e.getMessage());
            //所有編譯期異常轉換為執行時異常
            throw new SeckillException("Seckill inner error" + e.getMessage());
        }

    }

    /**
     * @param seckillId
     * @param userPhone
     * @param md5
     * @return
     * @throws SeckillException
     * @throws RepeteKillException
     * @throws SeckillCloseException
     */
    public SeckillExecution executeSeckillByProcedure(long seckillId, long userPhone, String md5) {
        if (md5 == null || (!md5.equals(getMD5(seckillId)))) {
            throw new SeckillException("Seckill data rewrite");
        }
        Date killTime = new Date();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("seckillId", seckillId);
        map.put("phone", userPhone);
        map.put("killTime", killTime);
        map.put("result", null);
        //執行儲存過程,result被賦值
        try {
            seckillDao.seckillByProcedure(map);
            //獲取result
            int result = MapUtils.getInteger(map, "result", -2);
            if (result == 1) {
                SuccessKill successKilled = successKillDao.queryByIdWithSeckill(seckillId, userPhone);
                return new SeckillExecution(seckillId, SeckillStatEnum.SUCCESS, successKilled);
            } else {
                return new SeckillExecution(seckillId, SeckillStatEnum.stateOf(result));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new SeckillExecution(seckillId, SeckillStatEnum.INNER_ERROR);
        }
    }

    private String getMD5(long seckillId) {
        String base = seckillId + "/" + slat;
        String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
        return md5;
    }
}


七、Controller層處理

在springMVC中,是基於restful風格來對訪問地址進行處理,所以我們在控制層也這樣進行處理。

@Controller
@RequestMapping("/seckill")
public class SeckillController {
	
	private final Logger logger=LoggerFactory.getLogger(this.getClass());
	
	
	@Autowired
	private SeckillService seckillService;
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(Model model){
		
		List<Seckill> list = seckillService.getSeckillList();
		model.addAttribute("list",list);
		return "list";
	}
	
	 @RequestMapping(value = "/{seckillId}/detail", method = RequestMethod.GET)
	    public String detail(@PathVariable("seckillId") Long seckillId, Model model){
	        if(seckillId == null){
	            return "redirect:/seckill/list";
	        }
	        Seckill seckill = seckillService.getById(seckillId);
	        if(seckill == null){
	            return "redirect:/seckill/list";
	        }
	        model.addAttribute("seckill", seckill);
	        return "detail";
	    }
	    
	    @RequestMapping(value = "/{seckillId}/exposer", 
	                    method = RequestMethod.POST,
	                    produces = {"application/json;charset=UTF-8"})
	    @ResponseBody
	    public SeckillResult<Exposer> exposer(@PathVariable("seckillId") Long seckillId){
	        SeckillResult<Exposer> result;
	        try {
	            Exposer exposer = seckillService.exportSeckillUrl(seckillId);
	            result = new SeckillResult<Exposer>(true,exposer);
	        } catch (Exception e) {
	            result = new SeckillResult<Exposer>(false, e.getMessage());
	        }
	        return result;
	    }
	    
	    @RequestMapping(value = "/{seckillId}/{md5}/execution",
	                    method = RequestMethod.POST,
	                    produces = {"application/json;charset=UTF-8"})
	    @ResponseBody
	    public SeckillResult<SeckillExecution> execute(@PathVariable("seckillId")Long seckillId,
	                                                   @PathVariable("md5")String md5, 
	                                                   @CookieValue(value = "killPhone", required = false)Long phone){
	        if(phone == null){
	            return new SeckillResult<>(false, "未註冊");
	        }
	        
	        try {
	        	 SeckillExecution execution = seckillService.executeSeckillByProcedure(seckillId, phone, md5);
	            return new SeckillResult<SeckillExecution>(true, execution);
	        } catch (SeckillCloseException e) {
	            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.REPEAT_KILL);
	            return new SeckillResult<SeckillExecution>(false, execution);
	        } catch (RepeatKillException e) {
	            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.END);
	            return new SeckillResult<SeckillExecution>(false, execution);
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.INNER_ERROR);
	            return new SeckillResult<SeckillExecution>(false, execution);
	        }
	    }
	    
	    @RequestMapping(value = "/time/now", method = RequestMethod.GET)
	    @ResponseBody
	    public SeckillResult<Long> time(){
	        Date now = new Date();
	        return new SeckillResult<>(true, now.getTime());
	    }
}


八、前臺處理

後臺資料處理完之後就是前臺了,對於頁面什麼的就直接使用bootstrap來處理了,直接呼叫bootstrap的cdn連結地址。

頁面的程式碼我就不貼出來了,可以到原始碼中進行檢視,都是非常經典的幾個頁面。值得一提的是這個js的分模組處理。

//存放主要互動邏輯的js程式碼
// javascript 模組化(package.類.方法)

var seckill = {

    //封裝秒殺相關ajax的url
    URL: {
        now: function () {
            return '/SecKill/seckill/time/now';
        },
        exposer: function (seckillId) {
            return '/SecKill/seckill/' + seckillId + '/exposer';
        },
        execution: function (seckillId, md5) {
            return '/SecKill/seckill/' + seckillId + '/' + md5 + '/execution';
        }
    },

    //驗證手機號
    validatePhone: function (phone) {
        if (phone && phone.length == 11 && !isNaN(phone)) {
            return true;//直接判斷物件會看物件是否為空,空就是undefine就是false; isNaN 非數字返回true
        } else {
            return false;
        }
    },

    //詳情頁秒殺邏輯
    detail: {
        //詳情頁初始化
        init: function (params) {
            //手機驗證和登入,計時互動
            //規劃我們的互動流程
            //在cookie中查詢手機號
            var killPhone = $.cookie('killPhone');
            //驗證手機號
            if (!seckill.validatePhone(killPhone)) {
                //繫結手機 控制輸出
                var killPhoneModal = $('#killPhoneModal');
                killPhoneModal.modal({
                    show: true,//顯示彈出層
                    backdrop: 'static',//禁止位置關閉
                    keyboard: false//關閉鍵盤事件
                });

                $('#killPhoneBtn').click(function () {
                    var inputPhone = $('#killPhoneKey').val();
                    console.log("inputPhone: " + inputPhone);
                    if (seckill.validatePhone(inputPhone)) {
                        //電話寫入cookie(7天過期)
                        $.cookie('killPhone', inputPhone, {expires: 7, path: '/SecKill'});
                        //驗證通過  重新整理頁面
                        window.location.reload();
                    } else {
                        //todo 錯誤文案資訊抽取到前端字典裡
                        $('#killPhoneMessage').hide().html('<label class="label label-danger">手機號錯誤!</label>').show(300);
                    }
                });
            }

            //已經登入
            //計時互動
            var startTime = params['startTime'];
            var endTime = params['endTime'];
            var seckillId = params['seckillId'];
            $.get(seckill.URL.now(), {}, function (result) {
                if (result && result['success']) {
                    var nowTime = result['data'];

                    //解決計時誤差
                    var userNowTime = new Date().getTime();
                    console.log('nowTime:' + nowTime);
                    console.log('userNowTime:' + userNowTime);

                    //計算使用者時間和系統時間的差,忽略中間網路傳輸的時間(本機測試大約為50-150毫秒)
                    var deviationTime = userNowTime - nowTime;
                    console.log('deviationTime:' + deviationTime);
                    //考慮到使用者時間可能和伺服器時間不一致,開始秒殺時間需要加上時間差
                    startTime = startTime + deviationTime;
                    //


                    //時間判斷 計時互動
                    seckill.countDown(seckillId, nowTime, startTime, endTime);
                } else {
                    console.log('result: ' + result);
                    alert('result: ' + result);
                }
            });
        }
    },

    handlerSeckill: function (seckillId, node) {
        //獲取秒殺地址,控制顯示器,執行秒殺
        node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn">開始秒殺</button>');

        $.post(seckill.URL.exposer(seckillId), {}, function (result) {
            //在回撥函式種執行互動流程
            if (result && result['success']) {
                var exposer = result['data'];
                if (exposer['exposed']) {
                    //開啟秒殺
                    //獲取秒殺地址
                    var md5 = exposer['md5'];
                    var killUrl = seckill.URL.execution(seckillId, md5);
                    console.log("killUrl: " + killUrl);
                    //繫結一次點選事件
                    $('#killBtn').one('click', function () {
                        //執行秒殺請求
                        //1.先禁用按鈕
                        $(this).addClass('disabled');//,<-$(this)===('#killBtn')->
                        //2.傳送秒殺請求執行秒殺
                        $.post(killUrl, {}, function (result) {
                            if (result && result['success']) {
                                var killResult = result['data'];
                                var state = killResult['state'];
                                var stateInfo = killResult['stateInfo'];
                                //顯示秒殺結果
                                node.html('<span class="label label-success">' + stateInfo + '</span>');
                            }
                        });
                    });
                    node.show();
                } else {
                    //未開啟秒殺(由於瀏覽器計時偏差,以為時間到了,結果時間並沒到,需要重新計時)
                    var now = exposer['now'];
                    var start = exposer['start'];
                    var end = exposer['end'];
                    var userNowTime = new Date().getTime();
                    var deviationTime = userNowTime - nowTime;
                    start = start + deviationTime;
                    seckill.countDown(seckillId, now, start, end);
                }
            } else {
                console.log('result: ' + result);
            }
        });

    },

    countDown: function (seckillId, nowTime, startTime, endTime) {
        console.log(seckillId + '_' + nowTime + '_' + startTime + '_' + endTime);
        var seckillBox = $('#seckill-box');
        if (nowTime > endTime) {
            //秒殺結束
            seckillBox.html('秒殺結束!');
        } else if (nowTime < startTime) {
            //秒殺未開始,計時事件繫結
            var killTime = new Date(startTime);//todo 防止時間偏移
            seckillBox.countdown(killTime, function (event) {
                //時間格式
                var format = event.strftime('秒殺倒計時: %D天 %H時 %M分 %S秒 ');
                seckillBox.html(format);
            }).on('finish.countdown', function () {
                //時間完成後回撥事件
                //獲取秒殺地址,控制現實邏輯,執行秒殺
                console.log('______fininsh.countdown');
                seckill.handlerSeckill(seckillId, seckillBox);
            });
        } else {
            //秒殺開始
            seckill.handlerSeckill(seckillId, seckillBox);
        }
    }

}
使用者秒殺之前需要先登記使用者的手機號碼,這個號碼會儲存在cookie中。

到了秒殺開始時間段,使用者就可以點選按鈕進行秒殺操作。


每個使用者只能秒殺一次,不能重複秒殺,如果重複執行,會顯示重複秒殺。


秒殺倒計時:


總結:其實在真實的秒殺系統中,我們是不直接對資料庫進行操作的,我們一般是會放到redis中進行處理,企業的秒殺目前應該考慮使用redis,而不是mysql。其實高併發是個偽命題,根據業務場景,資料規模,架構的變化而變化。開發高併發相關係統的基礎知識大概有:多執行緒,作業系統IO模型,分散式儲存,負載均衡和熔斷機制,訊息服務,甚至還包括硬體知識。每塊知識都需要一定的學習週期,需要幾年的時間總結和提煉。