0% found this document useful (0 votes)
14 views7 pages

Distribute Configuration With Consul

Uploaded by

Aditi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Distribute Configuration With Consul

Uploaded by

Aditi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Implementing a configuration store in 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.

Run the below command to download install the Dockerized Consul

docker run -d —name consul -p 8500:8500 consul

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
]

Use the below command to start the consul


docker start consul

Create Key/Value for the database configuration used by microservice


Click on Key/Value link to create a config/couseservice folder i.e the name of the folder should
match the spring application name of the spring microservice.

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}

Build and run the application

Integrating Spring Cloud Config Server with Consul for Distributed Configuration:

Spring Config Server:


● The Spring Config Server is responsible for fetching configurations from a
versioned repository (such as Git) based on the profiles specified.
● When a client (such as a microservice) requests configuration from the Config
Server, it includes the application's name and active profiles.
● The Config Server combines the application name, profiles, and any labels to
identify a specific set of configurations.
● The Config Server can also interact with the service registry (such as Eureka) to
discover client applications.
Consul:
● Consul is used as a backend data store for the configurations.
● The Config Server uses Consul as a source for configuration properties,
effectively fetching configuration data from Consul based on the application
name, profiles, and labels.
Microservice (Client):
● The microservice, which acts as a client, interacts with the Spring Config Server
to retrieve its configuration.
● It specifies its application name and active profiles when requesting
configuration.
● The Config Server uses these details to fetch the appropriate configuration data
from the Consul backend.
● The Config Server then provides the configuration data to the microservice.
In summary, the Spring Config Server is responsible for fetching configurations from the
versioned repository and consulting Consul for distributed configuration data. The microservice,
when requesting its configuration from the Config Server, indirectly triggers the Config Server to
retrieve the specific configuration data from Consul based on the application name, profiles, and
labels.

● 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

@EnableConfigServer : Enables the service as spring cloud config service.


The @EnableConfigServer annotation is a crucial part of setting up a Spring Cloud Config
Server in a Spring Boot application. It's used to enable the Spring Cloud Config Server
functionality, allowing your application to act as a central configuration server that provides
configuration properties to other microservices and applications.
Hence use this annotation along with @SpringBootApplication annotation in the main class of
the spring boot application as shown below:

@SpringBootApplication
@EnableConfigServer
public class DemoconfigserverApplication {

public static void main(String[] args) {


SpringApplication.run(DemoconfigserverApplication.class, args);
}

Built and start the application by typing the below command:

mvn spring-boot:run

Create a microservice application using spring initializer

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

Build and run the application as shown below:

mvn spring-boot:run

You might also like