Spring cloud management

Using GIT and libraries Spring client, Spring server

Nikitha Gullapalli
5 min readMay 12, 2023

Developing a distributed system with multiple micro-services can often. The number of micro-services and their instances (during scale up and scale down) can be a lot. Configuring each of them is too much work. Using a centeralized cloud management service makes it easy to have all the configurations in one place. This makes operations easy and if any configuration needs to be changed, we can change them in one place.

To achieve this we will need a spring cloud config server application connected to git and then connect all micro services(config client) to the spring cloud config server application.

1. Spring cloud server: Steps to create a cloud management server and connect it to GIT

Github: https://github.com/nikitha2/SpringCloudConfigServer101.git

1.a) Create a spring initilizer project with config server dependency as shown below. Generate it and import into eclipse as maven project.

1.b)Create a git repository and connect your project to it.

Refer to this article for steps to create git repository and connect eclipse project to

1.c) I want my cloud-config server to run on port 8888 and application name to be spring-cloud-config-server. Add configuration in applications.properties

spring.application.name=spring-cloud-config-server
server.port=8888

1.d) Now that I have a git repository for my project. Lets connect spring-cloud-config-server to the by adding the repository url in applications.properties as shown in the image.

spring.cloud.config.server.git.uri= file://Users/nikitha/git/SpringCloudConfigServer101

You can get this location by going to windows-> show view-> git Repository. In the git repository window right click on your repository -> copy path to Clipboard.

1.e) To enable this application as a config server. Add @EnableConfigServer annotation as shown below.

1.f) Lastly add the configuration file you want your config-servers to read. Here my file is limit-service.properties.

Config server can support different configurations for different environments (also called profiles). To do that simply add files for each environment. E.g: limit-service-dev.properties is a config file for dev profile.

1.g) Now run your config-server by right click on *Application.java class -> run as-> java application. This should start the applicationon port 8888.

Right click on Goto http://localhost:8888/limits-service/dev and confirm you are getting the configuration values from limit-service-dev.properties file. Note: You will also get the values for profile=default, but as we mentioned profile=dev, that will have priority.

Congratulations! you have successfully connected your config-server to git repository. Now lets connect our micro services (cloud-client) to the cloud-server

2. Create Spring cloud client and connect it to config-server

Github:https://github.com/nikitha2/LimitMicroService_BackendWithSpringBoot.git

2.a) Create a spring initilizer project with config client dependency as shown below. Generate it and import into eclipse as maven project.

Make sure to have dependencies — springWeb(This allows us to create a rest web service) and config client(enables us to connect application to cloud-server)

2.b) As we have configClient dependency. Project build will fail unless you define spring.config.import to connect with cloud-server. If the server is option at this moment you can mention optional as shown below. This will pick cloud-server if it exists.

2.c) Define profile and what config file to read in config-server as shown below.

# properties file we want to read from in cloud-config-server. 
# Should have the same name as config-server file name
spring.cloud.config.name = limits-service

#profile- can we qa, dev, prod
#spring.profiles.active= qa
spring.cloud.config.profile=dev

2.d) Create a configurations class and add all the configuration parameters you want to read from config-server.

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("limits-service"). //
public class Configuration {
private int minimum;
private int maximum;


public int getMinimum() {
return minimum;
}

public void setMinimum(int minimum) {
this.minimum = minimum;
}

public int getMaximum() {
return maximum;
}

public void setMaximum(int maximum) {
this.maximum = maximum;
}
}

2.e) Now create a rest service and return config values.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.microservices.limitservice.beans.Limits;
import com.springboot.microservices.limitservice.configuration.Configuration;

@RestController
public class LimitsController {

@Autowired
private Configuration configuration;


/** URL: http://localhost:8080/limits will fetch data
* from http://localhost:8888/limits-service/default spring-cloud configuration server
* */
@GetMapping("/limits")
public Limits retrieveLimits() {
return new Limits(configuration.getMinimum(),configuration.getMaximum());
}
}

Note: Make sure all the classes created should be under the package your application class is in, else your application class wont pick it up.

2.f) Right click on the appliation class -> run as->java application. This should start the application.

Now goto http://localhost:8080/limits and as you set the profile=dev. You should see the configuration values in limits-service-dev.properties in config-server.

Also, in console you can see that it is retriving info from http://localhost:8888/limits-service/dev.

Congratulations! your webservice is connected to the config-server. Which in turn is connected to git. So you have an end-to-end connection. You can connect as many webservices you want to the config-server. Thus making it centralized.

--

--