Building Microservices with Spring Cloud Consul: A Comprehensive Demo
Seamlessly integrate your microservices with the Consul service mesh using Spring Cloud Consul
Objective
Spring Cloud Consul" project, which is a part of the Spring Cloud ecosystem and provides integration with HashiCorp Consul, a popular service mesh and service discovery tool.
Spring Cloud Consul enables Spring Boot applications to register themselves with Consul and discover other services that are registered with Consul. It also provides features such as distributed configuration, service routing, and health checking.
Spring consul installation
Download the package from any of the OS package managers. For Instance below are the package manager for mac OS
brew tap hashicorp/tap
brew install hashicorp/tap/consul
Verify that Consul is installed correctly by running the following command in your terminal or command prompt
consul --version
Adding Spring consul to the project
following dependencies were required to integrate the existing project with
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Optionally we can also add a spring-boot starter dependency to test the changes
Once the dependencies are added make sure to add the configuration details to application.yml
spring:
application:
name: app01
cloud:
consul:
config:
fail-fast: true
host: 'remote server host name'
port: remote server port '
config:
import: 'consul:' # spring config is mandatory for versions more than spring-boot 2.4
Adding key values to the remote server
Open the Consul web UI by visiting
http://<consul-host>:<consul-port>/ui
in your web browser.Create a folder in structure in such a way that it config/<<application-name>>
Navigate to the "Key/Value" tab on the left sidebar.
Click on the "+" icon next to the "Keys" header to create a new key.
Enter the key name and value in the form that appears, and click the "Create" button.
Testing
To test/consume the props from remote server, make a Configuration class and annotate with @ConfigurationalProperties
@ConfigurationProperties(prefix = "my") @Data public class MyConfig { private String userName; private String password; }
And auto-wire MyConfig bean in RestController class. Create a GET mapping annotation and return the MyConfig instance
@RestController public class TestController { @Autowired MyConfig myConfig; @GetMapping("/getConfigData") public MyConfig getConfig(){ return myConfig; } }
navigate the http://localhost:8080/getConfigData, application should start successfully and verify the changes
Code Reference
Please email to venothanand91@gmail.com for any queries to support.
Thanks