视频055~(雪花算法)

master
Administrator 3 years ago
parent 8c3887567d
commit 432d5464cb

@ -25,4 +25,5 @@ public interface BeaconCacheClient {
@GetMapping("/cache/smember/{key}") @GetMapping("/cache/smember/{key}")
Set<Map> smember(@PathVariable(value = "key")String key); Set<Map> smember(@PathVariable(value = "key")String key);
} }

@ -1,8 +1,14 @@
package com.mashibing.api.filter.impl; package com.mashibing.api.filter.impl;
import com.mashibing.api.client.BeaconCacheClient;
import com.mashibing.api.filter.CheckFilter; import com.mashibing.api.filter.CheckFilter;
import com.mashibing.common.constant.ApiConstant;
import com.mashibing.common.constant.CacheConstant;
import com.mashibing.common.enums.ExceptionEnums;
import com.mashibing.common.exception.ApiException;
import com.mashibing.common.model.StandardSubmit; import com.mashibing.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
@ -13,9 +19,49 @@ import org.springframework.stereotype.Service;
@Slf4j @Slf4j
public class FeeCheckFilter implements CheckFilter { public class FeeCheckFilter implements CheckFilter {
@Autowired
private BeaconCacheClient cacheClient;
/**
* 70
*/
private final int MAX_LENGTH = 70;
/**
* 7067/
*/
private final int LOOP_LENGTH = 67;
private final String BALANCE = "balance";
@Override @Override
public void check(StandardSubmit submit) { public void check(StandardSubmit submit) {
log.info("【接口模块-校验客户余额】 校验ing…………"); log.info("【接口模块-校验客户余额】 校验ing…………");
//1、从submit中获取到短信内容
int length = submit.getText().length();
//2、判断短信内容的长度如果小于等于70算作一条如果大于70字按照67字/条,算出来当前短信的费用
if(length <= MAX_LENGTH){
// 当前短信内容是一条
submit.setFee(ApiConstant.SINGLE_FEE);
}else{
int strip = length % LOOP_LENGTH == 0 ? length / LOOP_LENGTH : length / LOOP_LENGTH + 1;
submit.setFee(ApiConstant.SINGLE_FEE * strip);
}
//3、从Redis中查询出客户剩余的金额
Long balance = ((Integer) cacheClient.hget(CacheConstant.CLIENT_BALANCE + submit.getClientId(), BALANCE)).longValue();
//4、判断金额是否满足当前短信费用\
if(balance >= submit.getFee()){
log.info("【接口模块-校验客户余额】 用户金额充足!!");
return;
}
//5、不满足就抛出异常
log.info("【接口模块-校验客户余额】 客户余额不足");
throw new ApiException(ExceptionEnums.BALANCE_NOT_ENOUGH);
} }
} }

@ -16,6 +16,11 @@
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -15,4 +15,9 @@ public interface ApiConstant {
*/ */
String SIGN_SUFFIX = "】"; String SIGN_SUFFIX = "】";
/**
* 50
*/
Long SINGLE_FEE = 50L;
} }

@ -14,8 +14,9 @@ public enum ExceptionEnums {
ERROR_SIGN(-3,"无可用签名"), ERROR_SIGN(-3,"无可用签名"),
ERROR_TEMPLATE(-4,"无可用模板"), ERROR_TEMPLATE(-4,"无可用模板"),
ERROR_MOBILE(-5,"手机号格式不正确"), ERROR_MOBILE(-5,"手机号格式不正确"),
BALANCE_NOT_ENOUGH(-6,"客户余额不足"), BALANCE_NOT_ENOUGH(-6,"客户余额不足"),
PARAMETER_ERROR(-10,"参数不合法!"), PARAMETER_ERROR(-10,"参数不合法!"),
SNOWFLAKE_OUT_OF_RANGE(-11,"雪花算法的机器id或服务id超出最大范围")
; ;
private Integer code; private Integer code;

@ -58,7 +58,7 @@ public class StandardSubmit {
private LocalDateTime sendTime; private LocalDateTime sendTime;
/** /**
* 7067 * 7067,
*/ */
private Long fee; private Long fee;

@ -0,0 +1,113 @@
package com.mashibing.common.util;
import com.mashibing.common.enums.ExceptionEnums;
import com.mashibing.common.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
/**
* ID
* 64bitlong
* 1bit0.
* 41bit
* 5bitid
* 5bitid
* 12bit
* @author zjw
* @description
*/
@Component
@Slf4j
public class SnowFlakeUtil {
/**
* 41bit069.7
* 使19702039
* 2022-11-1141bit使2092~~
*/
private long timeStart = 1668096000000L;
/**
* id
*/
@Value("${snowflake.machineId:0}")
private long machineId;
/**
* id
*/
@Value("${snowflake.serviceId:0}")
private long serviceId;
/**
*
*/
private long sequence;
/**
* idbit
*/
private long machineIdBits = 5L;
/**
* idbit
*/
private long serviceIdBits = 5L;
/**
* bit
*/
private long sequenceBits = 12L;
/**
* id
*/
private long maxMachineId = -1 ^ (-1 << machineIdBits);
/**
* id
*/
private long maxServiceId = -1 ^ (-1 << serviceIdBits);
@PostConstruct
public void init(){
if(machineId > maxMachineId || serviceId > maxServiceId){
System.out.println("机器ID或服务ID超过最大范围值");
throw new ApiException(ExceptionEnums.SNOWFLAKE_OUT_OF_RANGE);
}
}
/**
* id
*/
private long serviceIdShift = sequenceBits;
/**
* id
*/
private long machineIdShift = sequenceBits + serviceIdBits;
/**
*
*/
private long timestampShift = sequenceBits + serviceIdBits + maxMachineId;
}

@ -1,5 +1,6 @@
package com.mashibing.test.mapper; package com.mashibing.test.mapper;
import com.mashibing.test.entity.ClientBalance;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
@ -9,7 +10,7 @@ import org.apache.ibatis.annotations.Select;
*/ */
public interface ClientBalanceMapper { public interface ClientBalanceMapper {
@Select("select balance from client_balance where client_id = #{clientId}") @Select("select * from client_balance where client_id = #{clientId}")
Long findByClientId(@Param("clientId")Long clientId); ClientBalance findByClientId(@Param("clientId")Long clientId);
} }

@ -1,13 +1,16 @@
package com.mashibing.test.mapper; package com.mashibing.test.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mashibing.test.client.CacheClient; import com.mashibing.test.client.CacheClient;
import com.mashibing.test.entity.ClientBalance;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*; import java.util.Map;
@SpringBootTest @SpringBootTest
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ -20,10 +23,11 @@ public class ClientBalanceMapperTest {
private CacheClient cacheClient; private CacheClient cacheClient;
@Test @Test
public void findByClientId() { public void findByClientId() throws JsonProcessingException {
Long balance = mapper.findByClientId(1L); ClientBalance clientBalance = mapper.findByClientId(1L);
System.out.println(balance); ObjectMapper objectMapper = new ObjectMapper();
Map map = objectMapper.readValue(objectMapper.writeValueAsString(clientBalance), Map.class);
cacheClient.set("client_balance:1",balance); cacheClient.hmset("client_balance:1",map);
} }
} }
Loading…
Cancel
Save