Distribute Configuration With Consul
Distribute Configuration With Consul
In this example we will use HashiCorp’s Consul to externalize the database access details
(like url, username and password) that are required for the data access.
Spring Framework has built-in support and integration with Consul. Though Consul is a service
discovery tool, it has the capability to store the application configuration i.e properties.
We will use a Dockerized Consul instance for our example to make it simple. Follow the below
instructions to install and test the Consul
Prerequisite to install Docokerized Consul is to make sure the Docker application is installed
and is running in your machine/environment.
It will download and install Consul. Then Consul will be kick started as a single-node instance
and starts listening to the requests on post no 8500. We can verify the Consul Web interface by
following the below url:
http://localhost:8500/
[ Note: You can also verify whether the Consul image is running or not by executing the below
command
docker ps
It will give the list of docker containers that are running in your environment. One of them is the
Consul image container.
In order to stop the Consul image running, use the below command
docker stop consul
]
Click on the Create button. While creating the folder the name should end with ‘/’. For Key name
it is not required.
Create all the properties as shown above and click Save for each of the property
database.url
database.username
database.password
Database.driver
Create microservice using spring initializer and select spring consul as the dependency and
observe the below entry in the pom.xml file
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
Update the application.properties file with the below entries:
spring.config.import=consul:
spring.application.name=courseservice
spring.cloud.consul.config.enabled=true
spring.cloud.consul.config.name=courseservice
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.datasource.url=${database.url}
spring.datasource.driver-class-name=${database.driver}
spring.datasource.username=${database.username}
spring.datasource.password=${database.password}
Integrating Spring Cloud Config Server with Consul for Distributed Configuration:
● Create a Spring cloud config server as a string boot application , so that it can be
exposed as a config service.
● Create the application as shown in below screen using Spring Initializer
Update the application.yml with the below profile including the consul
spring:
application:
name: config-server
profiles:
active: native, git, consul
cloud:
config:
server:
native:
search-locations:
classpath: /config
git:
uri: <url of the github repository>
username: <youraccountid>
password: <password>
searchPaths: training
consul:
host: localhost
port: 8500
enabled: true
prefix: config
defaultContext: application
server:
port: 8071
@SpringBootApplication
@EnableConfigServer
public class DemoconfigserverApplication {
mvn spring-boot:run
Create the spring microservice as cloud client to fetch the configuration from spring cloud config
server
In the client give the application.yml as below:
spring:
application:
name: course-service
profiles:
active: consul
config:
import: optional:configserver:http://localhost:8071
mvn spring-boot:run