Welcome to another video about microservices. Today we will learn about Spring Cloud Configuration and how we can use it to manage the confi...
Welcome to another video about microservices. Today we will learn about Spring Cloud Configuration and how we can use it to manage the configuration of our services. As per Spring, Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. Basically, what it means is that we can store and versioned the contents of the application property file in GIT. We can also update the configuration at run-time and it is accessible via GIT URI.
For this demonstration, we will need to set up 2 projects, one we should be already familiar with.
- Terawarehouse-catalog - this is our catalog microservice
- Terawarehouse-config-server - configuration server that reads the configuration from a GIT store
Setting up the Terawarehouse-config-server
- Add the spring-cloud-config-server dependency.
- Annotate the SpringBootApplication class with @EnableConfigServer.
- Configure the application.yml file. For this example, we will point the spring.cloud.config.server.uri to a local git repository (make sure to set it up first).
- Run the config server.
- Access the configurations via URL: http://localhost:8000/{applicationName}/{profile}.
Setting up the Terawarehouse-catalog project
- Add spring-cloud-starter-config dependencies.
- Annotate the SpringBoothApplication class with @EnableDiscoveryClient.
- In the resource folder, remove the application.yml or properties file and add a bootstrap.properties. In this file, we will be defining the application name as well as the config server URI.
spring.application.name=catalog
spring.cloud.config.enabled=true
spring.cloud.config.uri=http://192.168.1.100:8000
spring.cloud.config.fail-fast=true - Create a new local git repository where the config server is pointing.
- Create a new file with filename equals to the spring.application.name of our microservice project. In this case catalog.
- Add all the required properties from the previous video we have. I already created a file named catalog.properties that you can download and paste in your local repository. Make sure to change the database configuration. Commit your changes.
- Our configuration should be accessible at http://localhost:8000/catalog/default.
- Let’s run the application. It should be accessible at port 8001.
- Stop the application.
- Let’s create a new profile “integration” with filename catalog-integration.properties and set the port to 8002.
- Run the application again and this time it should be available at port 8002.
References
Repositories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.application.name=catalog | |
spring.cloud.config.enabled=true | |
spring.cloud.config.uri=http://192.168.1.100:8000 | |
spring.cloud.config.fail-fast=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app | |
spring.application.name=catalog | |
server.port=8001 | |
spring.main.banner-mode=off | |
spring.main.allow-bean-definition-overriding=true | |
management.endpoints.web.exposure.include=* | |
# api | |
server.servlet.context-path=/api/v1/ | |
spring.data.rest.base-path=/api/spring/rest/ | |
# data source | |
spring.datasource.url=jdbc:postgresql://localhost:5432/tw_catalog | |
spring.datasource.username=kerri | |
spring.datasource.password=kerri | |
spring.datasource.driver-class-name=org.postgresql.Driver | |
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect | |
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true | |
#spring.jpa.hibernate.ddl-auto=create-drop | |
spring.liquibase.change-log=classpath:db/changelog/db.changelog-rebuild.xml | |
# basic auth credentials | |
spring.security.user.name=client | |
spring.security.user.password=client | |
# config to connect to admin server | |
spring.boot.admin.client.url=http://localhost:9990 | |
spring.boot.admin.client.username=admin | |
spring.boot.admin.client.password=admin | |
# config to send info to admin server | |
spring.boot.admin.client.instance.metadata.user.name=${spring.boot.admin.client.username} | |
spring.boot.admin.client.instance.metadata.user.password=${spring.boot.admin.client.username} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An Open Source Inventory and Sales Management System | |
* Copyright (C) 2019 Edward P. Legaspi (https://github.com/czetsuya) | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <https://www.gnu.org/licenses/>. | |
*/ | |
package com.terawarehouse; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.cloud.config.server.EnableConfigServer; | |
/** | |
* @author Edward P. Legaspi | czetsuya@gmail.com | |
*/ | |
@SpringBootApplication | |
@EnableConfigServer | |
public class TerawarehouseConfigServerApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(TerawarehouseConfigServerApplication.class, args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.2.0.RC1</version> | |
<relativePath /> <!-- lookup parent from repository --> | |
</parent> | |
<groupId>com.terawarehouse</groupId> | |
<artifactId>terawarehouse-config-server</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>terawarehouse-config-server</name> | |
<description>Terawarehouse Config Server</description> | |
<properties> | |
<java.version>11</java.version> | |
<maven.compiler.source>11</maven.compiler.source> | |
<maven.compiler.target>11</maven.compiler.target> | |
</properties> | |
<repositories> | |
<repository> | |
<id>jcenter-snapshots</id> | |
<name>jcenter</name> | |
<url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url> | |
</repository> | |
<repository> | |
<id>spring-milestones</id> | |
<name>Spring Milestones</name> | |
<url>https://repo.spring.io/milestone</url> | |
</repository> | |
</repositories> | |
<pluginRepositories> | |
<pluginRepository> | |
<id>spring-milestones</id> | |
<name>Spring Milestones</name> | |
<url>https://repo.spring.io/milestone</url> | |
</pluginRepository> | |
</pluginRepositories> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>com.terawarehouse</groupId> | |
<artifactId>terawarehouse-bom</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-config-server</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-actuator</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-starter-test</artifactId> | |
<scope>test</scope> | |
<exclusions> | |
<exclusion> | |
<groupId>org.junit.vintage</groupId> | |
<artifactId>junit-vintage-engine</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
</dependencies> | |
</project> |
For repositories use:
- Broodcamp Utils - Release version 0.0.1
- Broodcamp Commons - Release version 0.0.1
- Terawarehouse (catalog, config-server) - Tag 24-Microservice_Spring_Cloud_Configuration_Server_Using_a_GIT_Store
COMMENTS