使Spring Boot配置具有容错性

huangapple go评论63阅读模式
英文:

Making a Spring Boot Configuration Fault Tolerant

问题

我有一个依赖于各种服务(如Keycloak、Elastic Stack和PostgreSQL)的Spring Boot应用程序。该应用程序将与其他许多应用程序一起部署到Kubernetes集群中。是否可以使我的应用程序对可能不可用的服务具有容错性?想象一下如果在启动时PostgreSQL不可用会怎样。我是否可以强制应用程序在降级状态下启动,通知用户,记录错误,并通知任何调用该服务降级的应用程序?如果能够在一段时间后重试自动配置这些Bean,那将非常有用。

英文:

I have a Spring Boot Application that relies on various services like Keycloak, Elastic Stack, and PostgreSQL. This application will be deployed to an Kubernetes cluster along with a number of other applications. Is it possible to cause my application to be tolerant of services that may not be available? Imagine if PostgreSQL wasn't up at boot time. Can I force the application to start in a degraded state, notifying the user, log the error, and notifying any calling application of the service degradation? It would also be useful if it could retry auto configuring such beans at an interval.

答案1

得分: 1

你是不是在询问关于 @Retryable 和 @Recover?还有 @Lazy,但前两者可能正是你想要的。这是假设你的bean没有内置重试的情况下。也就是说,Kafka的bean会自动重试。如果你在谈论REST API,我可能会使用类似Eureka、熔断器等的东西。

英文:

Aren't you asking about @Retryable and @Recover? There's also @Lazy, but the first 2 are probably exactly what you want. That's assuming that your beans don't have retry built in. I.e. the Kafka beans retry automatically. If you are talking rest apis, I'd probably use something like Eureka, Circuit Breaker, etc.

答案2

得分: 0

这不会完全回答您的问题。但对于数据库,可以在数据库不可用的情况下启动Spring Boot。如果您使用Hikari,则可以配置连接池,以便在启动时不尝试连接到数据库。

@Bean
public DataSource xxxDataSource() {
    HikariConfig config = new HikariConfig();
    config.setDriverClassName("org.postgresql.Driver");
    config.setUsername("user");
    config.setPassword("password");
    config.setJdbcUrl("jdbc-url");
    config.setMaximumPoolSize(5);
    config.setInitializationFailTimeout(-1);  // <-- 这一行是您需要的

    return new HikariDataSource(config);
}

您不需要重新创建bean。每次尝试使用数据源时,Hikari都会尝试创建连接。如果数据库仍然关闭,将引发异常。如果它重新启动,那么就会成功。

英文:

This won't answer your question completely. But for the database it's possible to startup spring boot with the database unavailable. If you use Hikari then you can configure the connection pool so that it won't try to connect to the database on startup.

@Bean
public DataSource xxxDataSource() {
    HikariConfig config = new HikariConfig();
    config.setDriverClassName(&quot;org.postgresql.Driver&quot;);
    config.setUsername(&quot;user&quot;);
    config.setPassword(&quot;password&quot;);
    config.setJdbcUrl(&quot;jdbc-url&quot;);
    config.setMaximumPoolSize(5);
    config.setInitializationFailTimeout(-1);  // &lt;-- This line here is what you need

    return new HikariDataSource(config);
}

You won't need to recreate the bean. Every time you try to use the datasource Hikari will try to create a connection. If the database is still down an exception will be thrown. If it's back up then you succeed.

答案3

得分: -1

你的问题含糊不清,范围太广。如果你正在使用Spring Data,这是不可能的,因为Spring Data会在启动时尝试连接到数据库,如果连接不上就会失败。你可以自己进行连接,但之后需要考虑连接断开、重试、客户端体验等问题。

如果你想要深入分析,请参考我的博客文章;虽然是针对Couchbase数据库编写的,但涉及到与你的情况相同的用例。https://blog.asarkar.com/technical/couchbase-kubernetes/

英文:

Your question is vague and too wide in scope. This is not possible if you're using Spring Data, because Spring data will attempt to connect to the DB at startup, and fail if it can't. You can connect yourself, but you'll have to think about connection dropping later, retries, client experience, things like that.

If you want an in-depth analysis, refer to my blog post; it's written for Couchbase database, but addresses the same use case as yours. https://blog.asarkar.com/technical/couchbase-kubernetes/

huangapple
  • 本文由 发表于 2020年8月11日 03:50:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63347062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定