Spring Cloud Config Server with Example(2021) Hello World Example | CodeUsingJava






















Spring Cloud Config Server with Example

In this tutorial we will be implementing Spring Cloud Config Server with the help of example.
In an Enterprise Application, many microservices collaborate together so Just keeping our configuration stored in a enviornment sometimes does not prove to be a good option.
Here is where the Spring cloud config server comes to use. In the spring cloud config, there is a centralised server responsible where multiple services can be configured.
Spring Config Project
In this project we will have InfoManager which acts as a client, InfoByAuthorities class which acts as a information source and InfoConfiguration class which acts as a configuration server and we will configure our git server here.
The project flow can be described as follows-
Spring Config Project
The client instance will not directly communicate with the information source . Instead they will communicate with a config server which further communicate through github. The github will now communicate with InfoByAuthorities service.
The advantage is that any changes in the url of InfoByAuthorities would not directly affect the InfoManager. The github repository will take care of the changes and reflect automatically.
Firstly open your github and create a repository named InfoServer. Now create a file named application.properties in it.
	infoByAuthorities.infourl=http://localhost:8080/getallactors

InfoConfiguration

As we can see from the figure, this microservice will be linked with the github repository as well as the instance of the clients will be registered in this service. infoconfig
This will be the standard directory layout for maven project structure- We need to start by creating a Maven pom.xml(Project Object Model) file. The pom.xml file contains the project configuration details.
	<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
		<modelVersion>4.0.0</modelVersion>
	
		<groupId>com.codeusingjava</groupId>
		<artifactId>infoserver</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<packaging>jar</packaging>
	
		<name>Info Configuration</name>
		<description>info server</description>
	
		<parent>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-parent</artifactId>
			<version>2.0.1.RELEASE</version>
			<relativePath/> <!-- lookup parent from repository -->
		</parent>
	
		<properties>
			<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
			<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
			<java.version>1.8</java.version>
			<spring-cloud.version>Finchley.RC1</spring-cloud.version>
		</properties>
	
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-actuator</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-web</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-config-server</artifactId>
			</dependency>
	
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-devtools</artifactId>
				<scope>runtime</scope>
			</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>0</version>
					<type>pom</type>
					<scope>import</scope>
				</dependency>
			</dependencies>
		</dependencyManagement>
	
		<build>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
				</plugin>
			</plugins>
		</build>
	
		<repositories>
			<repository>
				<id>spring-milestones</id>
				<name>Spring Milestones</name>
				<url>https://repo.spring.io/milestone</url>
				<snapshots>
					<enabled>false</enabled>
				</snapshots>
			</repository>
		</repositories>
	
	
	</project>
	
Creating the configuration in application.properties as follows-
The uri of the git repository is defined where the application.properties is configured.
	spring.cloud.config.server.git.uri=https://github.com/your-github-account-name/InfoServer
	management.endpoints.web.exposure.include=*
	server.port=8888
Creating the main class for InfoConfiguration
With the help of @EnableConfigServer annotation, our application can act as a Configuration Server.
	package com.codeusingjava.infoserver;

	import org.springframework.boot.SpringApplication;
	import org.springframework.boot.autoconfigure.SpringBootApplication;
	import org.springframework.cloud.config.server.EnableConfigServer;
	
	@SpringBootApplication
	@EnableConfigServer
	public class InfoConfigurationApplication {
	
		public static void main(String[] args) {
			SpringApplication.run(InfoConfigurationApplication.class, args);
		}
	}
	

InfoByAuthorities

This class acts as a source of information. The clients will access the url present in this application with the hep of a github config server.
The project structure is as follows:-
infobyauthorities
We need to start by creating a Maven pom.xml(Project Object Model) file. The pom.xml file contains the project configuration details.
	<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
		<modelVersion>4.0.0</modelVersion>
	
		<groupId>com.codeusingjava</groupId>
		<artifactId>informationByAuthorities</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<packaging>jar</packaging>
	
		<name>infoByAuthorities</name>
		<description>information by authorities</description>
	
		<parent>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-parent</artifactId>
			<version>2.0.1.RELEASE</version>
			<relativePath/> <!-- lookup parent from repository -->
		</parent>
	
		<properties>
			<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
			<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
			<java.version>1.8</java.version>
		</properties>
	
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-web</artifactId>
			</dependency>
	
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-devtools</artifactId>
				<scope>runtime</scope>
			</dependency>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-test</artifactId>
				<scope>test</scope>
			</dependency>
		</dependencies>
	
		<build>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
				</plugin>
			</plugins>
		</build>
	
	
	</project>
		
Creating the main class for infobyAuthorities.
The url present in this class will be linked to the github config server.
	package com.codeusingjava.infobyAuthorities;

	import java.util.List;
	import java.util.stream.Collectors;
	import java.util.stream.Stream;
	
	import org.springframework.boot.SpringApplication;
	import org.springframework.boot.autoconfigure.SpringBootApplication;
	import org.springframework.web.bind.annotation.GetMapping;
	import org.springframework.web.bind.annotation.RestController;
	
	@SpringBootApplication
	@RestController
	public class InfoByAuthoritiesApplication {
	
		@GetMapping("/getallactors")
		public List<String> getPlans() {
			return Stream.of("Student", "Staff", "Principal").collect(Collectors.toList());
	
		}
	
		public static void main(String[] args) {
			SpringApplication.run(InfoByAuthoritiesApplication.class, args);
		}
	}
	

InfoManager

This class acts as a client service that is linked with the info configuration service. The user will interact with the url present in the controller class of this service. The project structure is as follows:-
infoManager
The standard pom.xml file for this class is as follows-
	<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
		<modelVersion>4.0.0</modelVersion>
	
		<groupId>com.codeusingjava</groupId>
		<artifactId>infoManager</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<packaging>jar</packaging>
	
		<name>infoManager</name>
		<description>Spring client class for config</description>
	
		<parent>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-parent</artifactId>
			<version>2.0.1.RELEASE</version>
			<relativePath/> <!-- lookup parent from repository -->
		</parent>
	
		<properties>
			<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
			<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
			<java.version>1.8</java.version>
			<spring-cloud.version>Finchley.RC1</spring-cloud.version>
		</properties>
	
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-web</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-starter-config</artifactId>
			</dependency>
	
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-devtools</artifactId>
				<scope>runtime</scope>
			</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>0</version>
					<type>pom</type>
					<scope>import</scope>
				</dependency>
			</dependencies>
		</dependencyManagement>
	
		<build>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
				</plugin>
			</plugins>
		</build>
	
		<repositories>
			<repository>
				<id>spring-milestones</id>
				<name>Spring Milestones</name>
				<url>https://repo.spring.io/milestone</url>
				<snapshots>
					<enabled>false</enabled>
				</snapshots>
			</repository>
		</repositories>
	
	
	</project>
	
Creating the configuration for application.properties
	spring.cloud.config.uri=http://localhost:8888
	server.port=9999
@RefreshScope helps in providing a new scope for defining the bean. It refreshes the property sources and the bean gets refreshed. Run the following Spring Boot class for starting our application.
The @Value indicates the value fetched by the properties file. We see that the value from the github application.properties file is to be fetched using the annotation.
	package com.codeusingjava.InfoManager;

	import java.util.List;
	
	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.beans.factory.annotation.Value;
	import org.springframework.boot.SpringApplication;
	import org.springframework.boot.autoconfigure.SpringBootApplication;
	import org.springframework.cloud.context.config.annotation.RefreshScope;
	import org.springframework.context.annotation.Bean;
	import org.springframework.context.annotation.Lazy;
	import org.springframework.web.bind.annotation.GetMapping;
	import org.springframework.web.bind.annotation.RestController;
	import org.springframework.web.client.RestTemplate;
	
	@SpringBootApplication
	@RestController
	@RefreshScope
	public class InfoManagerApplication {
	
		@Autowired
		@Lazy
		private RestTemplate template;
	
		@Value("")
		private String geturl;
	
		@GetMapping("/getcatagories")
		public List<String> getCatagories() {
			List<String> catagory = template.getForObject(geturl, List.class);
			return catagory;
		}
	
		public static void main(String[] args) {
			SpringApplication.run(InfoManagerApplication.class, args);
		}
	
		@Bean
		public RestTemplate template() {
			return new RestTemplate();
		}
	
	}
	

Running the application
First we run the InfoConfigurationApplication class as spring boot app.
Now we run InfoByAuthoritiesApplication as spring boot application.
Now we run InfoManagerApplication as spring boot application. Actuator Output
api Output

Downloads-

Spring Cloud + Config Hello World Example