You've already forked FrameTour-BE
refactor(integration): 重构集成服务的降级机制
-移除各服务自定义的降级服务类,统一降级逻辑 - 新增 IntegrationFallbackService作为通用降级服务 - 更新设备和景区服务的降级处理方式 - 优化降级缓存管理,增加统计信息和批量清理功能 - 调整 API 接口,移除扁平化批量更新等相关方法
This commit is contained in:
@@ -13,4 +13,12 @@ public class BatchUpdateResponse {
|
||||
|
||||
@JsonProperty("message")
|
||||
private String message;
|
||||
|
||||
public Integer getSuccess() {
|
||||
return (updatedCount != null ? updatedCount : 0) + (createdCount != null ? createdCount : 0);
|
||||
}
|
||||
|
||||
public Integer getFailed() {
|
||||
return 0; // 当前响应格式不包含失败计数,返回0作为默认值
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.ycwl.basic.integration.scenic.example;
|
||||
|
||||
import com.ycwl.basic.integration.scenic.dto.config.CreateConfigRequest;
|
||||
import com.ycwl.basic.integration.scenic.dto.filter.FilterCondition;
|
||||
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterRequest;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.CreateScenicRequest;
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.scenic.dto.config.*;
|
||||
import com.ycwl.basic.integration.scenic.dto.filter.*;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.*;
|
||||
import com.ycwl.basic.integration.scenic.service.ScenicConfigIntegrationService;
|
||||
import com.ycwl.basic.integration.scenic.service.ScenicIntegrationService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -11,10 +11,12 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ZT-Scenic集成服务使用示例
|
||||
* 仅供参考,实际使用时根据业务需要调用相应的服务方法
|
||||
* 景区集成示例(包含降级机制)
|
||||
* 演示景区集成和失败降级策略的使用
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -23,6 +25,9 @@ public class ScenicIntegrationExample {
|
||||
|
||||
private final ScenicIntegrationService scenicIntegrationService;
|
||||
private final ScenicConfigIntegrationService scenicConfigIntegrationService;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
private static final String SERVICE_NAME = "zt-scenic";
|
||||
|
||||
/**
|
||||
* 示例:创建景区并设置配置
|
||||
@@ -75,4 +80,105 @@ public class ScenicIntegrationExample {
|
||||
log.error("筛选景区失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示基础景区操作的降级机制
|
||||
*/
|
||||
public void basicScenicOperationsExample() {
|
||||
log.info("=== 基础景区操作示例(含降级机制) ===");
|
||||
|
||||
Long scenicId = 2001L;
|
||||
|
||||
try {
|
||||
// 获取景区信息 - 自动降级
|
||||
ScenicV2DTO scenic = scenicIntegrationService.getScenic(scenicId);
|
||||
log.info("获取景区成功: {}", scenic.getName());
|
||||
|
||||
// 获取景区配置信息 - 自动降级
|
||||
ScenicV2WithConfigDTO scenicWithConfig = scenicIntegrationService.getScenicWithConfig(scenicId);
|
||||
log.info("获取景区配置成功,配置数量: {}", scenicWithConfig.getConfig().size());
|
||||
|
||||
// 获取扁平化配置 - 自动降级
|
||||
Map<String, Object> flatConfig = scenicIntegrationService.getScenicFlatConfig(scenicId);
|
||||
log.info("获取扁平化配置成功,配置项数量: {}", flatConfig.size());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("景区操作降级失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示景区配置管理的降级机制
|
||||
*/
|
||||
public void scenicConfigManagementFallbackExample() {
|
||||
log.info("=== 景区配置管理示例(含降级机制) ===");
|
||||
|
||||
Long scenicId = 2001L;
|
||||
|
||||
try {
|
||||
// 获取扁平化配置 - 自动降级
|
||||
Map<String, Object> flatConfigs = scenicConfigIntegrationService.getFlatConfigs(scenicId);
|
||||
log.info("获取扁平化配置成功,配置项数量: {}", flatConfigs.size());
|
||||
|
||||
// 批量更新配置 - 直接操作,失败时抛出异常
|
||||
Map<String, Object> updates = new HashMap<>();
|
||||
updates.put("max_visitors", "5000");
|
||||
updates.put("opening_hours", "08:00-18:00");
|
||||
|
||||
BatchUpdateResponse result = scenicConfigIntegrationService.batchFlatUpdateConfigs(scenicId, updates);
|
||||
log.info("批量更新配置完成: 成功 {}, 失败 {}", result.getSuccess(), result.getFailed());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("景区配置管理操作失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示降级缓存管理
|
||||
*/
|
||||
public void fallbackCacheManagementExample() {
|
||||
log.info("=== 景区降级缓存管理示例 ===");
|
||||
|
||||
String scenicCacheKey = "scenic:2001";
|
||||
String configCacheKey = "scenic:flat:configs:2001";
|
||||
|
||||
// 检查降级缓存状态
|
||||
boolean hasScenicCache = fallbackService.hasFallbackCache(SERVICE_NAME, scenicCacheKey);
|
||||
boolean hasConfigCache = fallbackService.hasFallbackCache(SERVICE_NAME, configCacheKey);
|
||||
|
||||
log.info("景区降级缓存存在: {}", hasScenicCache);
|
||||
log.info("配置降级缓存存在: {}", hasConfigCache);
|
||||
|
||||
// 获取降级缓存统计信息
|
||||
IntegrationFallbackService.FallbackCacheStats stats = fallbackService.getFallbackCacheStats(SERVICE_NAME);
|
||||
log.info("景区服务降级缓存统计: 缓存数量={}, TTL={}天",
|
||||
stats.getTotalCacheCount(), stats.getFallbackTtlDays());
|
||||
|
||||
// 清理特定的降级缓存
|
||||
if (hasScenicCache) {
|
||||
fallbackService.clearFallbackCache(SERVICE_NAME, scenicCacheKey);
|
||||
log.info("已清理景区降级缓存");
|
||||
}
|
||||
|
||||
// 如果缓存过多,批量清理
|
||||
if (stats.getTotalCacheCount() > 50) {
|
||||
fallbackService.clearAllFallbackCache(SERVICE_NAME);
|
||||
log.info("已批量清理所有景区降级缓存");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行所有示例
|
||||
*/
|
||||
public void runAllExamples() {
|
||||
log.info("开始运行景区集成示例(包含降级机制)...");
|
||||
|
||||
createScenicWithConfig();
|
||||
filterScenics();
|
||||
basicScenicOperationsExample();
|
||||
scenicConfigManagementFallbackExample();
|
||||
fallbackCacheManagementExample();
|
||||
|
||||
log.info("景区集成示例运行完成");
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.ycwl.basic.integration.scenic.service;
|
||||
|
||||
import com.ycwl.basic.integration.common.exception.IntegrationException;
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.scenic.client.ScenicConfigV2Client;
|
||||
import com.ycwl.basic.integration.scenic.dto.config.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -17,6 +18,9 @@ import java.util.Map;
|
||||
public class ScenicConfigIntegrationService {
|
||||
|
||||
private final ScenicConfigV2Client scenicConfigV2Client;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
private static final String SERVICE_NAME = "zt-scenic";
|
||||
|
||||
public List<ScenicConfigV2DTO> listConfigs(Long scenicId) {
|
||||
log.info("获取景区配置列表, scenicId: {}", scenicId);
|
||||
@@ -32,8 +36,15 @@ public class ScenicConfigIntegrationService {
|
||||
|
||||
public Map<String, Object> getFlatConfigs(Long scenicId) {
|
||||
log.info("获取景区扁平化配置, scenicId: {}", scenicId);
|
||||
CommonResponse<Map<String, Object>> response = scenicConfigV2Client.getFlatConfigs(scenicId);
|
||||
return handleResponse(response, "获取景区扁平化配置失败");
|
||||
return fallbackService.executeWithFallback(
|
||||
SERVICE_NAME,
|
||||
"scenic:flat:configs:" + scenicId,
|
||||
() -> {
|
||||
CommonResponse<Map<String, Object>> response = scenicConfigV2Client.getFlatConfigs(scenicId);
|
||||
return handleResponse(response, "获取景区扁平化配置失败");
|
||||
},
|
||||
Map.class
|
||||
);
|
||||
}
|
||||
|
||||
public ScenicConfigV2DTO createConfig(Long scenicId, CreateConfigRequest request) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.ycwl.basic.integration.scenic.service;
|
||||
|
||||
import com.ycwl.basic.integration.common.exception.IntegrationException;
|
||||
import com.ycwl.basic.integration.common.response.CommonResponse;
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.scenic.client.ScenicConfigV2Client;
|
||||
import com.ycwl.basic.integration.scenic.client.ScenicV2Client;
|
||||
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterPageResponse;
|
||||
@@ -25,23 +26,47 @@ public class ScenicIntegrationService {
|
||||
|
||||
private final ScenicV2Client scenicV2Client;
|
||||
private final ScenicConfigV2Client scenicConfigV2Client;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
private static final String SERVICE_NAME = "zt-scenic";
|
||||
|
||||
public ScenicV2DTO getScenic(Long scenicId) {
|
||||
log.info("获取景区信息, scenicId: {}", scenicId);
|
||||
CommonResponse<ScenicV2DTO> response = scenicV2Client.getScenic(scenicId);
|
||||
return handleResponse(response, "获取景区信息失败");
|
||||
return fallbackService.executeWithFallback(
|
||||
SERVICE_NAME,
|
||||
"scenic:" + scenicId,
|
||||
() -> {
|
||||
CommonResponse<ScenicV2DTO> response = scenicV2Client.getScenic(scenicId);
|
||||
return handleResponse(response, "获取景区信息失败");
|
||||
},
|
||||
ScenicV2DTO.class
|
||||
);
|
||||
}
|
||||
|
||||
public ScenicV2WithConfigDTO getScenicWithConfig(Long scenicId) {
|
||||
log.info("获取景区配置信息, scenicId: {}", scenicId);
|
||||
CommonResponse<ScenicV2WithConfigDTO> response = scenicV2Client.getScenicWithConfig(scenicId);
|
||||
return handleResponse(response, "获取景区配置信息失败");
|
||||
return fallbackService.executeWithFallback(
|
||||
SERVICE_NAME,
|
||||
"scenic:config:" + scenicId,
|
||||
() -> {
|
||||
CommonResponse<ScenicV2WithConfigDTO> response = scenicV2Client.getScenicWithConfig(scenicId);
|
||||
return handleResponse(response, "获取景区配置信息失败");
|
||||
},
|
||||
ScenicV2WithConfigDTO.class
|
||||
);
|
||||
}
|
||||
|
||||
public Map<String, Object> getScenicFlatConfig(Long scenicId) {
|
||||
log.info("获取景区扁平化配置, scenicId: {}", scenicId);
|
||||
CommonResponse<Map<String, Object>> response = scenicConfigV2Client.getFlatConfigs(scenicId);
|
||||
return handleResponse(response, "获取景区扁平化配置失败");
|
||||
return fallbackService.executeWithFallback(
|
||||
SERVICE_NAME,
|
||||
"scenic:flat:config:" + scenicId,
|
||||
() -> {
|
||||
CommonResponse<Map<String, Object>> response = scenicConfigV2Client.getFlatConfigs(scenicId);
|
||||
return handleResponse(response, "获取景区扁平化配置失败");
|
||||
},
|
||||
Map.class
|
||||
);
|
||||
}
|
||||
|
||||
public ScenicV2DTO createScenic(CreateScenicRequest request) {
|
||||
|
||||
Reference in New Issue
Block a user