Commit f3d9030e authored by xieshaojun's avatar xieshaojun

更新代码

parent 4dc233b8
......@@ -145,11 +145,10 @@
<artifactId>spring-test</artifactId>
</dependency>
<!--jasypt加密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
<groupId>cn.yueshutong</groupId>
<artifactId>spring-boot-starter-current-limiting</artifactId>
<version>0.0.1.RELEASE</version>
</dependency>
</dependencies>
......
package vc.thinker.exception;
import cn.hutool.core.util.StrUtil;
import lombok.Getter;
/**
* 异常信息
*
* @author xfy
* @since 2022/1/18 9:56
**/
public class CurrentLimitException extends RuntimeException {
private static final long serialVersionUID = -6154899990522310326L;
@Getter
private final String code;
public CurrentLimitException(String message) {
super(message);
this.code = "500";
}
public CurrentLimitException(String message, String code) {
super(message);
this.code = code;
}
public static CurrentLimitException of(String message) {
return new CurrentLimitException(message);
}
public static CurrentLimitException of(String message, Object... argArray) {
return of(StrUtil.format(message, argArray));
}
}
package vc.thinker.exception;
import cn.yueshutong.springbootstartercurrentlimiting.annotation.CurrentLimiter;
import cn.yueshutong.springbootstartercurrentlimiting.handler.CurrentAspectHandler;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
/**
* @author : xieshaojun
* @date : 2023/1/4 18:13
*/
@Component
public class CurrentLimitHandler implements CurrentAspectHandler {
@Override
public Object around(ProceedingJoinPoint pjp, CurrentLimiter rateLimiter) {
//限流的返回数据可以自己根据需求场景设计
throw new CurrentLimitException("接口访问繁忙,休息一下");
}
}
\ No newline at end of file
......@@ -20,6 +20,11 @@ import java.util.List;
@RestControllerAdvice(annotations = RestController.class)
public class ExceptionAdvice {
/**
* 缺少参数
* @param e
* @return
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
public AbstractResponse handleBodyValidException(MethodArgumentNotValidException e) {
......@@ -28,4 +33,17 @@ public class ExceptionAdvice {
errorResponse.setErrorInfo(HttpStatus.BAD_REQUEST.value(), fieldErrors.get(0).getDefaultMessage());
return errorResponse;
}
/**
* 请求过多
* @param e
* @return
*/
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
@ExceptionHandler({CurrentLimitException.class})
public AbstractResponse currentLimitException(CurrentLimitException e) {
AbstractResponse errorResponse = new SimpleResponse();
errorResponse.setErrorInfo(HttpStatus.TOO_MANY_REQUESTS.value(), e.getMessage());
return errorResponse;
}
}
\ No newline at end of file
package vc.thinker.forwardthinker;
package vc.thinker.mqtt.forwardthinker;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;
import vc.thinker.absctacts.mqtt.protocol.AbstractNotSubscriptionMqttProtocol;
import vc.thinker.forwardthinker.dto.ThinkerReportDTO;
import vc.thinker.forwardthinker.enums.FoxGatewayTopicDefinition;
import vc.thinker.mqtt.forwardthinker.dto.ThinkerReportDTO;
import vc.thinker.mqtt.forwardthinker.enums.FoxGatewayTopicDefinition;
import vc.thinker.mqtt.config.ThinkerMqttConnectionProperties;
import java.util.List;
......
package vc.thinker.forwardthinker.actor.up;
package vc.thinker.mqtt.forwardthinker.actor.up;
import akka.actor.AbstractActor;
import akka.japi.pf.ReceiveBuilder;
......@@ -7,8 +7,8 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import vc.thinker.absctacts.mqtt.entity.MqttResponseBytes;
import vc.thinker.forwardthinker.ThinkerProdProtocolClient;
import vc.thinker.forwardthinker.enums.FoxGatewayTopicDefinition;
import vc.thinker.mqtt.forwardthinker.ThinkerProdProtocolClient;
import vc.thinker.mqtt.forwardthinker.enums.FoxGatewayTopicDefinition;
import vc.thinker.thirdpartyproxy.ProtocolType;
import javax.annotation.Resource;
......
package vc.thinker.forwardthinker.dto;
package vc.thinker.mqtt.forwardthinker.dto;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
......
package vc.thinker.forwardthinker.enums;
package vc.thinker.mqtt.forwardthinker.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import vc.thinker.absctacts.mqtt.topic.TopicDefinition;
import vc.thinker.absctacts.mqtt.utils.StringFormatUtils;
import vc.thinker.forwardthinker.actor.up.ThirdPartyReportDataActor;
import vc.thinker.mqtt.forwardthinker.actor.up.ThirdPartyReportDataActor;
import vc.thinker.utils.Resolve;
/**
......
......@@ -9,8 +9,8 @@ import org.springframework.util.ObjectUtils;
import vc.thinker.absctacts.protocolproxy.enums.IProtocolType;
import vc.thinker.absctacts.protocolproxy.protocol.AbstractThirdPartyProtocolSyncMeterInfo;
import vc.thinker.config.device.DeviceInfoConfig;
import vc.thinker.forwardthinker.ThinkerProdProtocolClient;
import vc.thinker.forwardthinker.dto.ThinkerReportDTO;
import vc.thinker.mqtt.forwardthinker.ThinkerProdProtocolClient;
import vc.thinker.mqtt.forwardthinker.dto.ThinkerReportDTO;
import vc.thinker.thirdpartyproxy.ProtocolType;
import vc.thinker.thirdpartyproxy.protocolimpls.centralAir.dto.DataReportInfoDTO;
import vc.thinker.thirdpartyproxy.protocolimpls.centralAir.dto.DataReportParamDTO;
......
package vc.thinker.web;
import cn.yueshutong.springbootstartercurrentlimiting.annotation.CurrentLimiter;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
......@@ -7,6 +8,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vc.thinker.response.SimpleResponse;
import vc.thinker.thirdpartyproxy.ProtocolType;
import vc.thinker.thirdpartyproxy.protocolimpls.centralAir.CentralAirProtocol;
import vc.thinker.thirdpartyproxy.protocolimpls.centralAir.dto.DataReportInfoDTO;
......@@ -33,7 +35,10 @@ public class CentralAirController {
@ApiOperation("设备数据上报接口")
@PostMapping("data-report")
public Boolean dataReport(@RequestBody @Valid TerminalDataReportVO reportVO) {
@CurrentLimiter(QPS = 1)
public SimpleResponse dataReport(@RequestBody @Valid TerminalDataReportVO reportVO) {
SimpleResponse simpleResponse = new SimpleResponse();
simpleResponse.setCode("200");
if (Objects.equals(reportVO.getProtocolCode(),ProtocolType.central_air_conditioning.getCode())){
DataReportInfoDTO dto = DataReportInfoDTO.builder()
.protocolCode(reportVO.getProtocolCode())
......@@ -42,9 +47,13 @@ public class CentralAirController {
.data(JSONObject.parseObject(JSONObject.toJSONString(reportVO.getData()), DataReportParamDTO.class))
.build();
this.centralAirProtocol.dataReport(dto);
return true;
simpleResponse.setSuccess(true);
simpleResponse.setMessage("处理成功");
return simpleResponse;
}
return false;
simpleResponse.setSuccess(false);
simpleResponse.setMessage("处理失败");
return simpleResponse;
}
}
......@@ -35,9 +35,17 @@ mqtt:
# 设备信息
device:
code: zykt-test1
jasypt:
encryptor:
# 加密算法
algorithm: PBEWITHHMACSHA512ANDAES_256
# 加密使用的盐
password: jaspyt_password
# 接口限制
current:
limiting:
#开启全局限流
enabled: false
#开启注解限流,可使注解失效
part-enabled: true
#每秒并发量 这里的qps是全局限流开启的时候的值,如果使用注解在注解里设置QPS值
qps: 1
#开启快速失败,可切换为阻塞
fail-fast: true
#系统启动保护时间为0
initial-delay: 0
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment