优化日志请求id
This commit is contained in:
parent
ae040421ce
commit
88bdf71ceb
@ -0,0 +1,5 @@
|
||||
package com.qiuguo.iot.base.constans;
|
||||
|
||||
public class Log4Constans {
|
||||
public static String PRINT_LOG_ID = "logid";
|
||||
}
|
||||
@ -21,7 +21,7 @@ public class RedisConstans {
|
||||
/*
|
||||
* 10年
|
||||
* */
|
||||
public static Long TEN_YEAR = ONE_DAY * 365 * 10;
|
||||
public static Long TEN_YEAR = 365L * 10;
|
||||
public static String DEVICE_INFO = "device::info::";
|
||||
|
||||
public static String IOT_TOKEN = "iot_token:";
|
||||
|
||||
@ -2,10 +2,13 @@ package com.qiuguo.iot.base.utils;
|
||||
|
||||
import cn.hutool.core.math.Money;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.qiuguo.iot.base.constans.Log4Constans;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.util.context.Context;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@ -43,11 +46,11 @@ public class WebClientUtils {
|
||||
return Mono.just(JSONObject.parseObject(s));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Mono<JSONObject> post(String url, JSONObject body, Map<String, String> headers) {
|
||||
log.info("POST WebClient URL:{} body:{} headers:{}", url, body, headers);
|
||||
//String logId = MDC.get(Log4Constans.PRINT_LOG_ID);
|
||||
if(headers == null || headers.size() == 0) {
|
||||
return webClient.post().uri(url).bodyValue(body.toString()).retrieve().bodyToMono(String.class).flatMap(s->
|
||||
{
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.qiuguo.iot.admin.http.api.filter;
|
||||
|
||||
import com.qiuguo.iot.base.constans.Log4Constans;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.reactivestreams.Subscription;
|
||||
import org.slf4j.MDC;
|
||||
@ -21,7 +22,7 @@ import javax.annotation.PreDestroy;
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class LogMdcConfiguration {
|
||||
public static String PRINT_LOG_ID = "logid";
|
||||
public static String PRINT_LOG_ID = Log4Constans.PRINT_LOG_ID;
|
||||
@PostConstruct
|
||||
public void contextOperatorHook() {
|
||||
Hooks.onEachOperator(PRINT_LOG_ID, Operators.lift((r, c) ->{
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.qiuguo.iot.user.api.filter;
|
||||
|
||||
import com.qiuguo.iot.base.constans.Log4Constans;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.reactivestreams.Subscription;
|
||||
import org.slf4j.MDC;
|
||||
@ -21,7 +22,7 @@ import javax.annotation.PreDestroy;
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class LogMdcConfiguration {
|
||||
public static String PRINT_LOG_ID = "logid";
|
||||
public static String PRINT_LOG_ID = Log4Constans.PRINT_LOG_ID;
|
||||
@PostConstruct
|
||||
public void contextOperatorHook() {
|
||||
Hooks.onEachOperator(PRINT_LOG_ID, Operators.lift((r, c) ->{
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.qiuguo.iot.box.websocket.api.filter;
|
||||
|
||||
import com.qiuguo.iot.base.constans.Log4Constans;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.reactivestreams.Subscription;
|
||||
import org.slf4j.MDC;
|
||||
@ -20,7 +21,7 @@ import javax.annotation.PreDestroy;
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class LogMdcConfiguration {
|
||||
public static String PRINT_LOG_ID = "logid";
|
||||
public static String PRINT_LOG_ID = Log4Constans.PRINT_LOG_ID;
|
||||
@PostConstruct
|
||||
public void contextOperatorHook() {
|
||||
Hooks.onEachOperator(PRINT_LOG_ID, Operators.lift((r, c) ->{
|
||||
|
||||
@ -29,6 +29,7 @@ import org.springframework.web.reactive.socket.*;
|
||||
import reactor.core.publisher.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.Duration;
|
||||
|
||||
@Component
|
||||
@WebSocketMapping("/websocket/box")
|
||||
@ -115,14 +116,13 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock
|
||||
//通知用户端设备绑定成功
|
||||
sendNoticeToUser(boxSession.getUserId(), "设备离线,设备序列号:" + boxSession.getSn(), AskTypeEnum.BOX_OFF_LINE.getCode());
|
||||
deviceInfoService.setOnLineStatus(sn, YesNo.NO.getCode()).subscribe();
|
||||
ReactiveValueOperations<String, String> operations = reactiveStringRedisTemplate.opsForValue();
|
||||
UserDeviceInfoModel userDeviceInfoModel = new UserDeviceInfoModel();
|
||||
userDeviceInfoModel.setStatus(YesNo.NO.getCode());
|
||||
userDeviceInfoModel.setUserId(userId);
|
||||
userDeviceInfoModel.setSn(sn);
|
||||
operations.set(RedisConstans.USER_BOX_INFO + userId,
|
||||
reactiveStringRedisTemplate.opsForValue().set(RedisConstans.USER_BOX_INFO + userId,
|
||||
JSONObject.toJSONString(userDeviceInfoModel),
|
||||
RedisConstans.TEN_YEAR).subscribe();
|
||||
Duration.ofDays(RedisConstans.TEN_YEAR)).subscribe();
|
||||
return;
|
||||
}
|
||||
log.info("被踢下线断开连接:{}", boxSession.getSn());
|
||||
@ -137,34 +137,35 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock
|
||||
}
|
||||
}
|
||||
|
||||
private void errorLogin(BaseSession boxSession, ReactiveValueOperations<String, String> operations, String sn){
|
||||
private void errorLogin(BaseSession boxSession, String sn){
|
||||
//清除异常redis
|
||||
operations.delete(RedisConstans.DEVICE_INFO + sn).subscribe();//不需要时间
|
||||
reactiveStringRedisTemplate.opsForValue().delete(RedisConstans.DEVICE_INFO + sn).subscribe();//不需要时间
|
||||
closeSendMsg(boxSession, "异常,请重新登录", AskTypeEnum.TTS.getCode());
|
||||
}
|
||||
|
||||
private void checkToken(BoxSession boxSession, String sn, Long linkTime, String signature, Long userId){
|
||||
ReactiveValueOperations<String, String> operations = reactiveStringRedisTemplate.opsForValue();
|
||||
operations.get(RedisConstans.DEVICE_INFO + sn).defaultIfEmpty("").flatMap(s -> {
|
||||
if(com.qiuguo.iot.base.utils.StringUtils.isNotBlank(s)){
|
||||
reactiveStringRedisTemplate.opsForValue().get(RedisConstans.DEVICE_INFO + sn).defaultIfEmpty("").flatMap(s -> {
|
||||
if(com.qiuguo.iot.base.utils.StringUtils.isNotBlank(s) && s.length() < 1000){
|
||||
try{
|
||||
DeviceInfoEntity dv = JSONObject.parseObject(s, DeviceInfoEntity.class);
|
||||
if(dv.getId() == null){
|
||||
log.info("redis设备缓存异常,清楚");
|
||||
errorLogin(boxSession, operations, sn);
|
||||
errorLogin(boxSession, sn);
|
||||
}
|
||||
return Mono.just(dv);
|
||||
}catch (Exception e){
|
||||
log.info("转换异常,清除redis。下次连接成功{}", e);
|
||||
|
||||
errorLogin(boxSession, operations, sn);
|
||||
errorLogin(boxSession, sn);
|
||||
}
|
||||
}
|
||||
DeviceInfoRequest request = new DeviceInfoRequest();
|
||||
request.setSn(sn);
|
||||
return deviceInfoService.selectDeviceInfoByRequest(request).defaultIfEmpty(new DeviceInfoEntity()).map(dv -> {
|
||||
if(dv.getId() != null){
|
||||
operations.set(RedisConstans.DEVICE_INFO + dv.getSn(), JSONObject.toJSONString(dv), RedisConstans.ONE_HOUR).subscribe();//直接提交订阅
|
||||
String redis = JSONObject.toJSONString(dv);
|
||||
|
||||
reactiveStringRedisTemplate.opsForValue().set(RedisConstans.DEVICE_INFO + dv.getSn(), redis, Duration.ofHours(1)).subscribe();//直接提交订阅
|
||||
}
|
||||
|
||||
return dv;
|
||||
@ -227,14 +228,13 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock
|
||||
//下面所有的以前未主设备改成非主设备
|
||||
//通知用户端设备绑定成功
|
||||
sendNoticeToUser(userId, "设备绑定成功,设备序列号:" + dv.getSn(), AskTypeEnum.DEVICE_BIND.getCode());
|
||||
ReactiveValueOperations<String, String> operations = reactiveStringRedisTemplate.opsForValue();
|
||||
UserDeviceInfoModel userDeviceInfoModel = new UserDeviceInfoModel();
|
||||
userDeviceInfoModel.setStatus(YesNo.YES.getCode());
|
||||
userDeviceInfoModel.setUserId(userId);
|
||||
userDeviceInfoModel.setSn(entity.getOtherDeviceId());
|
||||
operations.set(RedisConstans.USER_BOX_INFO + userId,
|
||||
reactiveStringRedisTemplate.opsForValue().set(RedisConstans.USER_BOX_INFO + userId,
|
||||
JSONObject.toJSONString(userDeviceInfoModel),
|
||||
RedisConstans.TEN_YEAR).subscribe();
|
||||
Duration.ofDays(RedisConstans.TEN_YEAR)).subscribe();
|
||||
|
||||
return Mono.empty();
|
||||
}).subscribe();
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.qiuguo.iot.customer.http.api.filter;
|
||||
|
||||
import com.qiuguo.iot.base.constans.Log4Constans;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.reactivestreams.Subscription;
|
||||
import org.slf4j.MDC;
|
||||
@ -21,7 +22,7 @@ import javax.annotation.PreDestroy;
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class LogMdcConfiguration {
|
||||
public static String PRINT_LOG_ID = "logid";
|
||||
public static String PRINT_LOG_ID = Log4Constans.PRINT_LOG_ID;
|
||||
@PostConstruct
|
||||
public void contextOperatorHook() {
|
||||
Hooks.onEachOperator(PRINT_LOG_ID, Operators.lift((r, c) ->{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user