feat(用户服务): 添加故事服务Feign客户端熔断机制
All checks were successful
test/timeline-server/pipeline/head This commit looks good
All checks were successful
test/timeline-server/pipeline/head This commit looks good
添加StoryServiceClientFallback和StoryServiceClientFallbackFactory实现类,用于在故事服务不可用时提供默认值 配置feign.circuitbreaker.enabled=true启用熔断功能
This commit is contained in:
@@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
* Story Service Feign Client
|
||||
* 用于调用 timeline-story-service 的接口
|
||||
*/
|
||||
@FeignClient(name = "timeline-story-service", path = "/story")
|
||||
@FeignClient(name = "timeline-story-service", path = "/story", fallbackFactory = StoryServiceClientFallbackFactory.class)
|
||||
public interface StoryServiceClient {
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.timeline.user.feign;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* StoryServiceClient Fallback
|
||||
* 当 story-service 不可用时返回默认值
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class StoryServiceClientFallback implements StoryServiceClient {
|
||||
|
||||
@Override
|
||||
public Long countStoriesByUserId(String userId) {
|
||||
log.warn("StoryService 不可用,返回默认故事数量 0, userId={}", userId);
|
||||
return 0L;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long countItemsByUserId(String userId) {
|
||||
log.warn("StoryService 不可用,返回默认照片/视频数量 0, userId={}", userId);
|
||||
return 0L;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getTotalStorageByUserId(String userId) {
|
||||
log.warn("StoryService 不可用,返回默认存储使用量 0, userId={}", userId);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.timeline.user.feign;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* StoryServiceClient Fallback Factory
|
||||
* 用于创建 Fallback 实例并记录异常原因
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class StoryServiceClientFallbackFactory implements FallbackFactory<StoryServiceClient> {
|
||||
|
||||
private final StoryServiceClientFallback fallback;
|
||||
|
||||
public StoryServiceClientFallbackFactory(StoryServiceClientFallback fallback) {
|
||||
this.fallback = fallback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StoryServiceClient create(Throwable cause) {
|
||||
log.error("StoryServiceClient 调用失败,使用 Fallback 处理", cause);
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
@@ -53,10 +53,13 @@ server.servlet.encoding.force=true
|
||||
spring.datasource.hikari.maximum-pool-size=10
|
||||
spring.datasource.hikari.minimum-idle=2
|
||||
# 5 minutes
|
||||
spring.datasource.hikari.idle-timeout=300000
|
||||
spring.datasource.hikari.idle-timeout=300000
|
||||
# 30 minutes, below MySQL wait_timeout
|
||||
spring.datasource.hikari.max-lifetime=1800000
|
||||
spring.datasource.hikari.max-lifetime=1800000
|
||||
# wait up to 30s for a connection
|
||||
spring.datasource.hikari.connection-timeout=30000
|
||||
spring.datasource.hikari.keepalive-time=0
|
||||
spring.datasource.hikari.validation-timeout=5000
|
||||
spring.datasource.hikari.connection-timeout=30000
|
||||
spring.datasource.hikari.keepalive-time=0
|
||||
spring.datasource.hikari.validation-timeout=5000
|
||||
|
||||
# Feign Client Configuration
|
||||
feign.circuitbreaker.enabled=true
|
||||
Reference in New Issue
Block a user