1

I am trying to set a spring boot application, using Datastore as DB storage.

For that I use the spring-cloud-gcp starter dependency to auto-configure the Datastore instance for me.

Since I want to work with two spring profiles - one production and one dev/integration-tests profile, I am trying to set the dev profile to utilize the LocalDatastoreHelper (auto-configured in GcpDatastoreEmulatorAutoConfiguration), hence my application-dev.properties defines the following (as documented here)

spring.cloud.gcp.datastore.emulator.enabled=true
spring.cloud.gcp.datastore.emulator.store-on-disk=true
spring.cloud.gcp.datastore.emulator.port=8081

During the booting of the DEV application, I have several beans that depend on the Datastore to query/insert DEV data to it.

The problem comes from the fact that when GcpDatastoreAutoConfiguration detects the presence of emulator settings, it automatically directs traffic to the emulator, which is not started until the Application is running, causing an I/O error.

I looked up the source code of GcpDatastoreEmulatorAutoConfiguration and it seems like it's supposed to run before the GcpDatastoreAutoConfiguration (doc):

@AutoConfiguration
@ConditionalOnProperty("spring.cloud.gcp.datastore.emulator.enabled")
@AutoConfigureBefore(GcpDatastoreAutoConfiguration.class)
@EnableConfigurationProperties(GcpDatastoreProperties.class)
@ConditionalOnMissingBean(LocalDatastoreHelper.class)
public class GcpDatastoreEmulatorAutoConfiguration implements SmartLifecycle{
    
    //config code... 
}

My guess is that SmartLifecycle overrides the expected behavior, as the configuration is not started until the DEFAULT_PHASE which is Integer.MAX_INT. I am fairly unfamiliar with SmartLifecycle, but according to the javadoc, seems like it will pospone its initialization for the last phase of the Spring Context initialization.

Has anyone got an idea how to fix this? How can I make the Emulator run prior to the initialization of the Datastore bean?

0 Answers0