优化
This commit is contained in:
@@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* 跨服务和跨区域传输使用的不可变事件信封。
|
||||
* 统一测试环境中跨业务服务传输使用的不可变事件信封。
|
||||
*
|
||||
* <p>租户 ID 和聚合 ID 使用字符串,避免跨语言传输时发生大整数精度丢失。
|
||||
* {@code payloadHash} 是原始规范化负载字节的 SHA-256 摘要。</p>
|
||||
@@ -14,8 +14,6 @@ import java.time.Instant;
|
||||
* @param eventType 事件类型,不包含版本号
|
||||
* @param eventVersion 事件契约版本,从 1 开始
|
||||
* @param occurredAt 业务事实实际发生时间,使用 UTC 时间点
|
||||
* @param sourceRegion 事件来源业务单元
|
||||
* @param targetRegion 事件目标业务单元;国内一期允许与来源区域相同
|
||||
* @param tenantId 事件所属客户租户 ID
|
||||
* @param aggregateType 聚合类型
|
||||
* @param aggregateId 聚合 ID
|
||||
@@ -32,8 +30,6 @@ public record EventEnvelope(
|
||||
String eventType,
|
||||
int eventVersion,
|
||||
Instant occurredAt,
|
||||
RegionCode sourceRegion,
|
||||
RegionCode targetRegion,
|
||||
String tenantId,
|
||||
String aggregateType,
|
||||
String aggregateId,
|
||||
|
||||
-6
@@ -37,12 +37,6 @@ public final class EventEnvelopeValidator {
|
||||
if (envelope.occurredAt() == null) {
|
||||
errors.add("occurredAt 不能为空");
|
||||
}
|
||||
if (envelope.sourceRegion() == null) {
|
||||
errors.add("sourceRegion 不能为空");
|
||||
}
|
||||
if (envelope.targetRegion() == null) {
|
||||
errors.add("targetRegion 不能为空");
|
||||
}
|
||||
requireText(errors, "tenantId", envelope.tenantId(), MAX_ID_LENGTH);
|
||||
if (envelope.aggregateType() == null
|
||||
|| !AGGREGATE_TYPE_PATTERN.matcher(envelope.aggregateType()).matches()) {
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baiyue.common.event;
|
||||
|
||||
/**
|
||||
* 事件来源与目标部署区域。
|
||||
*
|
||||
* <p>区域表示独立业务单元,不表示用户当前所在地。</p>
|
||||
*/
|
||||
public enum RegionCode {
|
||||
/** 国内业务单元。 */
|
||||
CN,
|
||||
/** 海外业务单元。 */
|
||||
OVERSEAS
|
||||
}
|
||||
@@ -8,8 +8,7 @@ import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
class DeliveryAckTest {
|
||||
private EventEnvelope event(String type) {
|
||||
return new EventEnvelope(UUID.randomUUID().toString(), type, 1, Instant.now(),
|
||||
RegionCode.CN, RegionCode.OVERSEAS, "0", "TEST", "1", 1, null,
|
||||
return new EventEnvelope(UUID.randomUUID().toString(), type, 1, Instant.now(), "0", "TEST", "1", 1, null,
|
||||
null, null, null, new ObjectMapper().createObjectNode(), "sha256:" + "a".repeat(64));
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.List;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DisplayName("跨区域事件信封契约")
|
||||
@DisplayName("跨服务事件信封契约")
|
||||
class EventEnvelopeTest {
|
||||
private final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
|
||||
|
||||
@@ -26,6 +26,8 @@ class EventEnvelopeTest {
|
||||
EventEnvelope restored = objectMapper.readValue(json, EventEnvelope.class);
|
||||
|
||||
assertThat(restored).isEqualTo(source);
|
||||
assertThat(objectMapper.readTree(json).has("sourceRegion")).isFalse();
|
||||
assertThat(objectMapper.readTree(json).has("targetRegion")).isFalse();
|
||||
assertThat(objectMapper.readTree(json).path("eventType").asText())
|
||||
.isEqualTo(EventTypes.FIRST_LEG_SHIPMENT_DEPARTED);
|
||||
}
|
||||
@@ -40,7 +42,7 @@ class EventEnvelopeTest {
|
||||
@Test
|
||||
void validatorReturnsAllBasicErrorsAtOnce() {
|
||||
EventEnvelope envelope = new EventEnvelope(
|
||||
"bad-id", "bad_type", 0, null, null, null, " ", "bad-type", " ",
|
||||
"bad-id", "bad_type", 0, null, " ", "bad-type", " ",
|
||||
-1, 0L, " ", " ", "bad-cause", null, "bad-hash");
|
||||
|
||||
List<String> errors = EventEnvelopeValidator.validate(envelope);
|
||||
@@ -90,8 +92,6 @@ class EventEnvelopeTest {
|
||||
EventTypes.FIRST_LEG_SHIPMENT_DEPARTED,
|
||||
1,
|
||||
Instant.parse("2026-07-29T08:30:00Z"),
|
||||
RegionCode.CN,
|
||||
RegionCode.OVERSEAS,
|
||||
"20001",
|
||||
"FIRST_LEG_SHIPMENT",
|
||||
"90001",
|
||||
|
||||
Reference in New Issue
Block a user