Skip to content

Commit

Permalink
完全模拟用户抢的流程,单线程
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrric committed May 18, 2021
1 parent ea7a7d3 commit 8d2af95
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/main/java/com/github/lyrric/service/HttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,14 @@ public class HttpService {
* @throws IOException
* @throws BusinessException
*/
public String secKill(String seckillId, String vaccineIndex, String linkmanId, String idCard) throws IOException, BusinessException {
public String secKill(String seckillId, String vaccineIndex, String linkmanId, String idCard, String st) throws IOException, BusinessException {
String path = baseUrl+"/seckill/seckill/subscribe.do";
Map<String, String> params = new HashMap<>();
params.put("seckillId", seckillId);
params.put("vaccineIndex", vaccineIndex);
params.put("linkmanId", linkmanId);
params.put("idCardNo", idCard);
//后面替换成接口返回的st
//目前发现接口返回的st就是当前时间,后面可能会固定为一个加密参数
long st = System.currentTimeMillis();
//加密参数
Header header = new BasicHeader("ecc-hs", eccHs(seckillId, st));
return get(path, params, header);
}
Expand Down Expand Up @@ -155,7 +153,7 @@ private String get(String path, Map<String, String> params, Header extHeader) th
return headers;
}

private String eccHs(String seckillId, Long st){
private String eccHs(String seckillId, String st){
String salt = "ux$ad70*b";
final Integer memberId = Config.memberId;
String md5 = DigestUtils.md5Hex(seckillId + st + memberId);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/github/lyrric/service/SecKillService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public void startSecKill(Integer vaccineId, String startDateStr, MainFrame mainF

AtomicBoolean success = new AtomicBoolean(false);
long now = System.currentTimeMillis();
if(now + 1000 < startDate){
if(now + 500 < startDate){
logger.info("还未到开始时间,等待中......");
Thread.sleep(startDate-now-2000);
Thread.sleep(startDate - now - 500);
}
String orderId = null;
do {
Expand All @@ -57,14 +57,15 @@ public void startSecKill(Integer vaccineId, String startDateStr, MainFrame mainF
logger.info("Thread ID:{},发送请求", id);
//log接口,不知道有何作用,也许调用这个接口后,服务端做了什么处理也未可知
httpService.log(vaccineId.toString());
orderId = httpService.secKill(vaccineId.toString(), "1", Config.memberId.toString(), Config.idCard);
String st = httpService.getSt(vaccineId.toString());
orderId = httpService.secKill(vaccineId.toString(), "1", Config.memberId.toString(), Config.idCard, st);
success.set(true);
logger.info("Thread ID:{},抢购成功", id);
break;
} catch (BusinessException e) {
logger.info("Thread ID: {}, 抢购失败: {}",Thread.currentThread().getId(), e.getErrMsg());
//如果离开始时间120秒后,或者已经成功抢到则不再继续
if(System.currentTimeMillis() > startDate+1000*60*2 || success.get()){
if (System.currentTimeMillis() > startDate + 1000 * 60 * 2 || success.get()) {
return;
}
} catch (Exception e) {
Expand Down

0 comments on commit 8d2af95

Please sign in to comment.