Skip to content

Consul distributed configuration & service discovery with spring boot based apis

Notifications You must be signed in to change notification settings

haritkumar/boot-app-consul

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Consul distributed config and service discovery

  • Update application configuration without restarting app/pod/instance
  • Service discovery

alt text

Start consul server

docker build -t consul .
docker run -p 8080:8080 consul

alt text

  • Create a key/value
config/application/data

spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/asicdv?useSSL=false
    username: root
    password: rootchanged

alt text

We can use three way to load configuration from consul

  • key/value
  • yaml
  • file - Git

Configure application

  • 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

alt text

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();
    }
}

alt text

  • On updating properties on consul, changes will reflect in app without restart

alt text

About

Consul distributed configuration & service discovery with spring boot based apis

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published