HikariCP restart with Spring Cloud Config

孤街浪徒 提交于 2020-01-06 06:17:04

问题


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

  • Spring Boot - 2.1.1.RELEASE
  • Spring Cloud Dependencies - Greenwich.RC2

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.

I am refreshing my applications using this job that calls refresh() method on the 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());
    }
}

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.

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. Does this restart of HikariCP cause issues with the load to the application is increased?

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


回答1:


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

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

Here is the example to configure in yaml

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

where ConfigProperties is the class annotated with @RefreshScope.



来源:https://stackoverflow.com/questions/54223822/hikaricp-restart-with-spring-cloud-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!