Consul distributed config and service discovery
Update application configuration without restarting app/pod/instance
Service discovery
docker build -t consul .
docker run -p 8080:8080 consul
config/application/data
spring :
datasource :
driverClassName : com.mysql.jdbc.Driver
url : jdbc:mysql://localhost:3306/asicdv?useSSL=false
username : root
password : rootchanged
We can use three way to load configuration from consul
key/value
yaml
file - Git
Add additional jars to pom.xml
<org .springframework.cloud>2.0.0.RELEASE</org .springframework.cloud>
<dependency >
<groupId >org.springframework.cloud</groupId >
<artifactId >spring-cloud-starter-consul-config</artifactId >
<version >${org.springframework.cloud}</version >
</dependency >
<dependency >
<groupId >org.springframework.cloud</groupId >
<artifactId >spring-cloud-starter-consul-discovery</artifactId >
<version >${org.springframework.cloud}</version >
</dependency >
<dependency >
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-starter-actuator</artifactId >
</dependency >
create bootstrap.yml with following config
spring :
datasource :
driverClassName : com.mysql.jdbc.Driver
url : jdbc:mysql://localhost:3306/asicdv?useSSL=false
username : root
password : root
jpa :
properties :
hibernate :
dialect : org.hibernate.dialect.MySQL5InnoDBDialect
hibernate :
jpa.generate-ddl : true
ddl-auto : update
server :
port : 8081
User @RefreshScope annotation to update config on runtime
@ SpringBootApplication
@ RefreshScope
@ EntityScan ("com.boot.entity" )
public class BootAppApplication {
public static void main (String [] args ) {
SpringApplication .run (BootAppApplication .class , args );
}
@ Bean
@ ConfigurationProperties ("spring.datasource" )
@ Primary
public DataSourceProperties dataSourceProperties () {
return new DataSourceProperties ();
}
@ Bean
@ ConfigurationProperties ("spring.datasource" )
@ Primary
public HikariDataSource dataSource (DataSourceProperties properties ) {
return properties .initializeDataSourceBuilder ().type (HikariDataSource .class )
.build ();
}
}
On updating properties on consul, changes will reflect in app without restart