简体   繁体   中英

Setup Spring Boot application to connect to different data source depending on environment parameter for the session

I am currently building an Spring Boot application which has a requirement to work with three different database environments and behave in an environment agnostic manner.

  • The "dev" environment will use a local sqlite database.

  • The "uat" environment will use a postgres database.

  • The "live" environment will use a sql database.

On load, my application checks for the existence of an environment parameter:

  • If none is set or the environment parameter is dev, then it will create a local sqlite database and establish a connection to it for the duration of the session.

  • If it is set to uat, then a connection will be established to a heroku postgres database.

  • If it is set to live, then a connection will be established to a mysql database.

Now I am currently struggling to conceptualise this on Java.

This is the code I have written so far, where I get the environment parameter. Beyond this I am not sure to do.

@SpringBootApplication
public class Launcher implements CommandLineRunner {

    public static void main(String args[]) {
        SpringApplication.run(Launcher.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        String currentEnvironment = System.getenv("CURRENT_ENV");

        // if current env is null or dev, set up sqlite database (if it doesnt already exist and use this for the remainder of the session

        // if uat connect to heroku postgres db

        // if live then connect to mysql db
    }
}

This is the purpose of creating profiles in spring, Spring Boot allows you to have profiles that will help your application run in different environments.

So in your case you have to create three properties files for example

  1. dev.properties
  2. uat.properties
  3. live.properties

In each properties files you have to set the necessary configuration for development. then just activate the profile you want to work.

spring.profiles.active=dev

For each one I would create a @Configuration class.

@Profile("dev")
@Configuration
public class DevConfiguration{
   ...
}

@Profile("uat")
@Configuration
public class UatConfiguration{
   ...
}

@Profile("live")
@Configuration
public class LiveConfiguration{
   ...
}

I would like to mention a nice book Pro Spring Boot by Felipe Gutierrez, it can teach you a lot.

I think the real question is : how do you specify your environment ?

With Spring boot you can use several application properties files. In Your case you can use 3 files :

  • application.properties (for the dev environnement)
  • application-uat.properties (for uat env)
  • application-live.properties (for live env).

you can also use YAML file to do this.

There is many way to let SpringBoot choose the right environment property file.

you have many way to choose the right environment file.

For exemple if are using tomcat for each environnement you can set the property spring.profiles.active For example JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=ENV_NAME" (in setclasspath.bat ( setclasspath.sh if you are on [l]unix) where ENV_NAME can be uat or live.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM