Skip to content

Commit

Permalink
Merge pull request crossoverJie#29 from crossoverJie/dev-seconds-kill
Browse files Browse the repository at this point in the history
Dev seconds kill
  • Loading branch information
crossoverJie authored May 7, 2018
2 parents 34af19d + 709832e commit 4cc5e16
Show file tree
Hide file tree
Showing 78 changed files with 6,469 additions and 4 deletions.
6 changes: 6 additions & 0 deletions SSM-SECONDS-KILL/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1. 简单的 web 层,service 层( `dubbo` 通信),数据库不做限制,出现超卖现象。
2. 数据库采用乐观锁更新,库存总数放入 Redis(并不会经常更新)
3. web 层,service 层分布式部署,提高吞吐量。
4. web 层做限流,每个 UID 限制每秒 N 次请求,多余的请求直接过滤(不能将过多的请求到 DB)。
5. service 层限流,先用单机限流,如令牌桶算法。再用 Redis 做全局限流。
6. 异步创建订单,将创建订单用 Kafka 解耦。
43 changes: 43 additions & 0 deletions SSM-SECONDS-KILL/SSM-SECONDS-KILL-API/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!--
- Copyright 1999-2011 Alibaba Group.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-->
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.crossoverJie</groupId>
<artifactId>SSM-SECONDS-KILL</artifactId>
<version>2.2.0-SNAPSHOT</version>
</parent>
<artifactId>SSM-SECONDS-KILL-API</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>


<dependencies>
<!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.9.0.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.crossoverJie.seconds.kill.api;

/**
* Function:
*
* @author crossoverJie
* Date: 01/05/2018 13:58
* @since JDK 1.8
*/
public interface OrderService {


/**
* 创建订单
* @param sid
* 库存ID
* @return
* 订单ID
*/
int createWrongOrder(int sid) throws Exception;


/**
* 创建订单 乐观锁
* @param sid
* @return
* @throws Exception
*/
int createOptimisticOrder(int sid) throws Exception;

/**
* 创建订单 乐观锁,库存查 Redis 减小 DB 压力。
* @param sid
* @return
* @throws Exception
*/
int createOptimisticOrderUseRedis(int sid) throws Exception ;

/**
* 创建订单 乐观锁,库存查 Redis 减小 DB 压力。
* 利用 Kafka 异步写订单
* @param sid
* @return
* @throws Exception
*/
void createOptimisticOrderUseRedisAndKafka(int sid) throws Exception ;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.crossoverJie.seconds.kill.api;

/**
* Function:
*
* @author crossoverJie
* Date: 15/04/2018 23:50
* @since JDK 1.8
*/
public interface StockService {

/**
* 获取当前库存
* @return
* @throws Exception
*/
Integer getCurrentCount() throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.crossoverJie.seconds.kill.api.constant;

/**
* Function:
*
* @author crossoverJie
* Date: 06/05/2018 01:03
* @since JDK 1.8
*/
public class RedisKeysConstant {

/**
* 库存
*/
public final static String STOCK_COUNT = "stock_count_";

/**
* 已售数量
*/
public final static String STOCK_SALE = "stock_sale_";

/**
* 版本
*/
public final static String STOCK_VERSION = "stock_version_";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.crossoverJie.seconds.kill.api.dto;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.kafka.common.errors.SerializationException;
import org.apache.kafka.common.serialization.Serializer;

import java.util.Map;


public class JsonSerializer<T> implements Serializer<T> {

public JsonSerializer() {

}


@Override
public void configure(Map<String, ?> configs, boolean isKey) {
// No-op
}

@Override
public byte[] serialize(String topic, T data) {
try {
byte[] result = null;
if (data != null) {

result = JSONObject.toJSONBytes(data,
SerializerFeature.UseISO8601DateFormat,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteClassName);
}
return result;
} catch (Exception ex) {
throw new SerializationException("Can't serialize data [" + data + "] for topic [" + topic + "]", ex);
}
}

@Override
public void close() {
// No-op
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.crossoverJie.seconds.kill.api.dto;

import java.io.Serializable;

/**
* @author crossoverJie
*/
public class Stock implements Serializable{
private static final long serialVersionUID = -8437012513227627973L;
private Integer id;

private String name;

private Integer count;

private Integer sale;

private Integer version;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}

public Integer getSale() {
return sale;
}

public void setSale(Integer sale) {
this.sale = sale;
}

public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}

@Override
public String toString() {
return "Stock{" +
"id=" + id +
", name='" + name + '\'' +
", count=" + count +
", sale=" + sale +
", version=" + version +
'}';
}
}
Loading

0 comments on commit 4cc5e16

Please sign in to comment.