本文介绍了HikariCP使用Spring Cloud Config重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将我的应用程序配置为将Spring Cloud Config与Github一起用作配置存储库.

I have recently configured my application to use Spring Cloud Config with Github as a configuration repository.

我的应用程序几乎使用了所有现成的东西.我刚刚在application.yml中配置了数据库,并且让HikariCP自动配置在后台发挥了作用.

My application is using pretty much everything out of the box. I have just configured the database in application.yml and I have HikariCP autoconfigurations doing the magic in the background.

我正在使用在 RefreshEndpoint .

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.endpoint.RefreshEndpoint;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@EnableScheduling
@Component
public class ConfigRefreshJob {

    private static final Logger LOG = LoggerFactory.getLogger(ConfigRefreshJob.class);

    private static final int ONE_MINUTE = 60 * 1000;

    private final RefreshEndpoint refreshEndpoint;

    @Autowired
    public ConfigRefreshJob(final RefreshEndpoint refreshEndpoint) {
        this.refreshEndpoint = refreshEndpoint;
    }

    @Scheduled(fixedDelay = ONE_MINUTE)
    public void refreshConfigs() {
        LOG.info("Refreshing Configurations - {}", refreshEndpoint.refresh());
    }
}

一切似乎都正常,但是每次刷新配置时都会看到以下日志.这些日志显示,每次刷新时,HikariCP池都会关闭并启动.

Everything seems be working good, but I see following logs every time I refresh the configurations. These logs say HikariCP pool is shutdown and started everytime I refresh.

2019-01-16 18:54:55.817  INFO 14 --- [taskScheduler-9] o.s.b.SpringApplication       : Started application in 0.155 seconds (JVM running for 144.646)
2019-01-16 18:54:55.828  INFO 14 --- [taskScheduler-9] c.z.h.HikariDataSource        : HikariPool-1555 - Shutdown initiated...
2019-01-16 18:54:55.828  INFO 14 --- [taskScheduler-9] c.z.h.HikariDataSource        : HikariPool-1555 - Shutdown completed.
2019-01-16 18:54:55.828  INFO 14 --- [taskScheduler-9] c.d.ConfigRefreshJob          : Refreshing Configurations - []
2019-01-16 18:55:03.094  INFO 14 --- [  XNIO-1 task-5] c.z.h.HikariDataSource        : HikariPool-1556 - Starting...
2019-01-16 18:55:03.123  INFO 14 --- [  XNIO-1 task-5] c.z.h.HikariDataSource        : HikariPool-1556 - Start completed.

如果我查看这些日志的时间,大约需要8秒钟才能重新配置HikariCP.

If I look at the times of these logs, it takes around 8 seconds for the HikariCP to be configured again.

由于应用程序上的负载现在还不多,所以到目前为止我的应用程序中还没有发现任何问题,但这是我要解决的几个问题.

I haven't found any issues in my application as of now since the load on the application is not that much right now, but here are couple of questions that I have.

  1. 重新启动HikariCP是否会导致应用程序负载增加的问题?

  1. Does this restart of HikariCP cause issues with the load to the application is increased?

如果重新启动会导致问题,是否可以不刷新HikariCP?

If the restarting can cause issues, is there a way to not refresh the HikariCP?

推荐答案

默认情况下,HikariCP可刷新,因为对其进行的更改会在池启动后密封配置.

HikariCP is made refreshable by default because a change made to it that seals the configuration once the pool is started.

因此,请禁用此功能,将spring.cloud.refresh.refreshable设置为空集.

So disable this, set spring.cloud.refresh.refreshable to an empty set.

以下是在Yaml中配置的示例

Here is the example to configure in yaml

spring:
  cloud:
    refresh:
      refreshable:
      - com.example.app.config.ConfigProperties

其中ConfigProperties是用@RefreshScope注释的类.

这篇关于HikariCP使用Spring Cloud Config重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 01:01