flow调msg异步和调log异步

This commit is contained in:
2025-12-23 19:29:51 +08:00
parent 44f3b599a8
commit d4ed987f75
11 changed files with 69 additions and 28 deletions

View File

@@ -86,11 +86,11 @@ public class HomeycomClientUserTokenController {
// 3. 先从缓存中获取token
String accessToken = getTokenFromCache(redisKey);
// 4. 如果缓存中没有生成新的token
if (StringUtils.isEmpty(accessToken)) {
// if (StringUtils.isEmpty(accessToken)) {
accessToken = generateAndCacheToken(userId, tenantId, redisKey);
} else {
log.info("从缓存中获取用户tokenuserId: {}, tenantId: {}", userId, tenantId);
}
// } else {
// log.info("从缓存中获取用户tokenuserId: {}, tenantId: {}", userId, tenantId);
// }
ObtainTokenDTO obtainTokenDTO = new ObtainTokenDTO();
obtainTokenDTO.setAccess_token(accessToken);
return R.ok(obtainTokenDTO);

View File

@@ -6,6 +6,7 @@ import com.honeycombis.honeycom.common.swagger.annotation.EnableOpenApi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* @author honeycom archetype
@@ -17,6 +18,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@EnableHoneycomResourceServer
@SpringBootApplication
@EnableAsync
public class HoneycomFlowTaskApplication {
public static void main(String[] args) {

View File

@@ -39,6 +39,7 @@ import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -401,6 +402,7 @@ public class RemoteServiceImpl implements IRemoteService {
messageOpenApiDTO.setContent("收到一条" + process.getName() + "审批任务代办,请前往任务中心-代办任务-流程任务审批");
messageOpenApiDTO.setStaffCode(String.valueOf(processNodeRecordAssignUserParamDto.getUserId()));
messageOpenApiDTO.setTenantCode(String.valueOf(TenantContextHolder.getTenantId()));
messageOpenApiDTO.setTenantId(TenantContextHolder.getTenantId());
JSONObject jsonObject = new JSONObject();
jsonObject.put("flowId", processNodeRecordAssignUserParamDto.getFlowId());
@@ -410,7 +412,14 @@ public class RemoteServiceImpl implements IRemoteService {
jsonObject.put("title", process.getName());
messageOpenApiDTO.setBusinessId(JSON.toJSONString(jsonObject));
log.info("审批节点发送通知消息:{}", JSON.toJSONString(messageOpenApiDTO));
remoteMessageService.pushMessage(messageOpenApiDTO);
CompletableFuture.runAsync(() -> {
try {
log.info("开始异步调用Msg服务...");
remoteMessageService.pushMessage(messageOpenApiDTO, Long.valueOf(messageOpenApiDTO.getTenantCode()));
} catch (Exception e) {
log.error("异步调用Msg服务异常: {}", e.getMessage(), e);
}
});
return R.ok();
}

View File

@@ -20,4 +20,6 @@ public class MessageOpenApiDTO {
@Schema(description = "业务id")
private String businessId;
private Long tenantId;
}

View File

@@ -34,6 +34,6 @@ public interface RemoteMessageService {
@Operation(summary = "提供给外围系统发布消息通知", description = "提供给外围系统发布消息通知")
@PostMapping(value = "/openapi/push")
R<Object> pushMessage(@RequestBody MessageOpenApiDTO dto);
R<Object> pushMessage(@RequestBody MessageOpenApiDTO dto, @RequestHeader("TENANT-ID") Long tenantId);
}

View File

@@ -1,5 +1,6 @@
package com.honeycombis.honeycom.msg.controller;
import com.alibaba.fastjson.JSON;
import com.honeycombis.honeycom.common.core.exception.ErrorType;
import com.honeycombis.honeycom.common.core.exception.HoneycomException;
import com.honeycombis.honeycom.common.core.util.R;
@@ -37,6 +38,7 @@ public class MessageApiController {
@PostMapping(value = "/push")
@Inner(value = false)
public R<Object> addByConfigKey(@RequestBody MessageOpenApiDTO dto) {
log.info("[openapi/push] param:{}", JSON.toJSONString(dto));
if (StringUtils.isBlank(dto.getStaffCode())) {
log.error("用户ID不能为空");
throw new HoneycomException(ErrorType.PARAM_ERROR.getCode());

View File

@@ -71,6 +71,12 @@
<artifactId>honeycom-msg-api</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.38</version>
</dependency>
<!--必备: 操作数据源相关-->
<!-- <dependency>-->
<!-- <groupId>com.honeycombis</groupId>-->

View File

@@ -5,6 +5,7 @@ import com.honeycombis.honeycom.common.feign.annotation.EnableHoneycomFeignClien
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
@@ -16,6 +17,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@EnableDiscoveryClient
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class HoneycomSpdmApplication {
public static void main(String[] args) {
SpringApplication.run(HoneycomSpdmApplication.class, args);

View File

@@ -22,12 +22,12 @@ package com.honeycombis.honeycom.spdm.controller;
import cn.hutool.json.JSONUtil;
import com.honeycombis.honeycom.common.core.util.R;
import com.honeycombis.honeycom.spdm.dto.SysLogDto;
import com.honeycombis.honeycom.spdm.feign.SpdmServiceFeignClient;
import com.honeycombis.honeycom.spdm.service.SpdmService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -41,20 +41,17 @@ import org.springframework.web.bind.annotation.RestController;
@Slf4j
public class SpdmLogController {
@Resource
private SpdmServiceFeignClient spdmServiceFeignClient;
@Autowired
private SpdmService spdmService;
@Operation(summary = "记录日志")
@PostMapping(value = "/saveLog")
public R<Void> saveLog(@RequestBody SysLogDto sysLogDto) {
// R<Boolean> r = remoteLogServiceFeign.saveLog(messageDto, SecurityConstants.FROM_IN);
SysLogDto sysLog = new SysLogDto();
sysLog.setTitle("登录成功");
sysLog.setServiceId("simulation-system");
sysLog.setTenantId(sysLogDto.getTenantId());
sysLog.setCreateBy(sysLogDto.getCreateBy());
log.info("[SpdmLogController] sysLog param:{}", JSONUtil.toJsonStr(sysLog));
spdmServiceFeignClient.saveLog(sysLog);
log.info("[SpdmLogController] 接收到日志保存请求:{}", JSONUtil.toJsonStr(sysLogDto));
// 异步执行日志保存
spdmService.asyncSaveLog(sysLogDto);
return R.ok();
}

View File

@@ -1,4 +1,34 @@
package com.honeycombis.honeycom.spdm.service;
public interface SpdmService {
import cn.hutool.json.JSONUtil;
import com.honeycombis.honeycom.spdm.dto.SysLogDto;
import com.honeycombis.honeycom.spdm.feign.SpdmServiceFeignClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class SpdmService {
@Resource
private SpdmServiceFeignClient spdmServiceFeignClient;
@Async
public void asyncSaveLog(SysLogDto sysLogDto) {
try {
SysLogDto sysLog = new SysLogDto();
sysLog.setTitle("登录成功");
sysLog.setServiceId("simulation-system");
sysLog.setTenantId(sysLogDto.getTenantId());
sysLog.setCreateBy(sysLogDto.getCreateBy());
log.info("[asyncSaveLog] sysLog param:{}", JSONUtil.toJsonStr(sysLog));
spdmServiceFeignClient.saveLog(sysLog);
} catch (Exception e) {
log.error("异步保存日志失败: {}", e.getMessage(), e);
// 这里可以添加降级处理,比如保存到本地文件或数据库
}
}
}

View File

@@ -1,9 +0,0 @@
package com.honeycombis.honeycom.spdm.service.impl;
import com.honeycombis.honeycom.spdm.service.SpdmService;
import org.springframework.stereotype.Service;
@Service
public class SpdmServiceImpl implements SpdmService {
}