Microservices with spring cloud config

How to set and use spring cloud config?

Mango
3 min readMar 31, 2020

This article is based on ‘Spring 5.0 Microservices - Second Edition’ Let’s learn about why we need and how to use centralized configuration

The reason that why do we have to manage config properties through git repository.

Usually, we create config properties files, such as, ‘bootstrap.properties’ on each services(If you don’t know this mean ‘services’, you’d better learn about microservices before read this article.). And these contain required data when running each services. However, This way has two problems as like those below.

  • Each services can’t be updated changed properties while running.
  • It’s too hard to grasp at a glance.

If we are using ‘spring cloud config’, how can it solve these above problems? The answer to these questions are explained is as below.

Spring cloud config consist of ‘config client’ and ‘config server’. Also, with config server you can access ‘config repository’. Let’s learn details with the picture below.

  • Actuator: It can help to refresh ‘config client ’.
  • Config Client: Each service has a ‘Config Client’ library. This library can send a request to the ‘Config Server’ and get It’s own properties data from the ‘Config Server’
  • Config Server: It will request config information to git repository when requested to request getting config information from each services.

First, we have to create a git repository, such as, ‘springconfig-repo’.

1. Config repository

springconfig-repo

→ search-service.properties

spring.application.name=search-service
orginairports.shutdown=NYC

Then we have to create a config server.

2. Config Server

pom.xml

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

bootstrap.properties

server.port=8888
spring.cloud.config.server.git.uri=<your git repo https url>

let’s add config client library to a service.

3. Config Client

pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

! As mentioned above, we must add actuator library. It is for refresh action.

bootstrap.properties

spring.application.name=search-service
server.port=8060
spring.cloud.config.uri=<spring config server url>
management.endpoints.web.exposure.include=refresh

SearchController.java

@RefreshScope
@CrossOrigin
@RestController
@RequestMapping("/search")
class SearchRestController {
private static final Logger logger = LoggerFactory.getLogger(SearchComponent.class);
TPMCounter tpm = new TPMCounter();

private SearchComponent searchComponent;

@Value("${orginairports.shutdown}")
private String originAirportShutdownList;

GaugeService gaugeService;


@Autowired
public SearchRestController(SearchComponent searchComponent, GaugeService gaugeService) {
this.gaugeService = gaugeService;
this.searchComponent = searchComponent;
}

@PostMapping("/get")
List<Flight> search(@RequestBody SearchQuery query) {
logger.info("Input : " + query);
if (Arrays.asList(originAirportShutdownList.split(",")).contains(query.getOrigin())) {
logger.info("The origin airport is in shutdown state");
return new ArrayList<Flight>();
}
tpm.increment();
gaugeService.submit("tpm", tpm.count.intValue());

return searchComponent.search(query);
}
}

! Add @RefreshScope using property value.

! @Value(“${orginairports.shutdown}”) is a code to get a property value of ‘.properties’ file.

Conclusions

We’ve studied about spring cloud config infrastructure. And It is the basics of spring cloud config. If your services have a big infrastructure, You have to need multiple config servers and load balancing. Or maybe you need spring cloud bus. But these are difficult and complex. Therefore, I only focused on understanding Spring Cloud Config.

If this article contains incorrect information and words, please contact me by email(sattlub123@gmail.com). Thank you.

--

--

Mango
Mango

Written by Mango

Wanna be a good programmer

No responses yet