2

我正在试验 K8s 和 spring boot liveness and readiness probes。我似乎无法理解和正确修复的一件事是我的 Spring Boot 应用程序应该如何从失败状态中恢复?我有一个简单的场景 - 我的应用程序正在连接到 S3 存储桶并尝试下载内容。而且我想确保当存储桶不可用时,我的就绪状态会自动更改为REFUSING_TRAFFIC. 但是,当存储桶启动并再次可用时,我希望我的就绪状态再次更新为ACCEPTING_TRAFIC.

我怎么做?

这是我所拥有的:

@Slf4j
public class AcpS3EnvironmentRepository extends AbstractScmEnvironmentRepository
        implements EnvironmentRepository, SearchPathLocator, InitializingBean {
    
    private static final String DEFAULT_CONFIG_VERSION = "latest";
    private final AcpS3TransferManager transferManager;
    private final ApplicationEventPublisher eventPublisher;
    
    public AcpS3EnvironmentRepository(ConfigurableEnvironment environment,
            AcpS3RepositoryProperties properties,
            AcpS3TransferManager transferManager,
                                      ApplicationEventPublisher publisher) {
        super(environment, properties);
        this.transferManager = transferManager;
        this.eventPublisher = publisher;
    }
    
    @Override
    public synchronized void afterPropertiesSet() {
        Assert.state(getUri() != null, "You need to configure a uri for the aws s3 bucket");
    }
    
    @Override
    public synchronized Locations getLocations(String application, String profile, String label) {
        try {            
            transferManager.downloadBucket(new AmazonS3URI(getUri()).getBucket(), getBasedir().toPath());
        } catch (Exception ex) {
            log.error("Could not load data from bucket " + getUri(), ex);
            AvailabilityChangeEvent.publish(eventPublisher, this, ReadinessState.REFUSING_TRAFFIC);
            throw new AcpS3BucketIllegalException("Could not load data from bucket " + getUri());
        }
        return new Locations(
                application,
                profile,
                label,
                DEFAULT_CONFIG_VERSION,
                getSearchLocations(
                        getWorkingDirectory(),
                        application,
                        profile,
                        label
                )
        );
    }
}

我目前观察到的:

  1. 使 s3 不工作
  2. 准备是 refusing_traffic
  3. s3 又开始工作了
  4. readiness 仍然是 refusing_traffic (但这次它应该接受流量)。

所以我的问题是如何以最好的方式实现这一目标?

4

0 回答 0