config-server-lib is a spring boot integration lib for config-server.
It bootstraps your application by fetching config from the config-server automatically on startup. It also provides your application with a /refresh callback endpoint that config-server can use to remotely trigger a config refresh and restart of your spring boot application.
Application.java - example
package com.ahirajustice.example;
import com.ahirajustice.lib.configserver.ConfigServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({
"com.ahirajustice.example",
"com.ahirajustice.lib.configserver"
})
public class Application {
public static void main(String[] args) {
ConfigServer.getConfig();
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
ConfigServer.configureRestart(Application.class, context);
}
}
Integration with config server requires setting up the Spring application.properties or application.yml file to work with spring-dotenv.
application.yml - Spring datasource config example
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: ${env.DATABASE_URL}
username: ${env.DATABASE_USER}
password: ${env.DATABASE_PASSWORD}
There are four environment variables that config-server-lib looks for on startup.
CONFIG_SERVER_CLIENT_ID=example_client
CONFIG_SERVER_CLIENT_SECRET=example_client_secret
CONFIG_SERVER_BASE_URL=https://configserver.example.com
CONFIG_ENVIRONMENT=DEVELOPMENT
CONFIG_SERVER_CLIENT_ID and CONFIG_SERVER_CLIENT_SECRET are credentials configured on config-server on client registration.
CONFIG_SERVER_BASE_URL is the base url for the config-server deployment.
CONFIG_ENVIRONMENT is the config scope for your application. CONFIG_ENVIRONMENT is one of [DEVELOPMENT, STAGING, PRODUCTION] and configs are fetched from config-server under one of these environments.
To disable config-server-lib from fetching configs on startup, simply omit setting these environment variables, or set them to empty/blank values.
Add config-server-lib as a dependency
pom.xml
<dependency>
<groupId>com.ahirajustice</groupId>
<artifactId>config-server-lib</artifactId>
<version>0.0.3</version>
</dependency>
build.gradle
implementation 'com.ahirajustice:config-server-lib:0.0.3'
For more options with different build tools, check out https://search.maven.org/artifact/com.ahirajustice/config-server-lib/0.0.3/jar