edit-icon download-icon

Simplify Spring Cloud micro-services environment configuration management with ACM

Last Updated: May 02, 2018

This topic explains how to use Alibaba Cloud Application Configuration Management (ACM) in combination with Spring Cloud to simplify environment configuration management in a micro-service architecture, by assuming that in the testing and production environments, different databases are connected, and different data source (including connection pool) parameters are configured.

Environment attributes of configuration

During the continuous delivery of the system, the diversity and complexity of the system’s final running environment undoubtedly make it harder for us to manage configurations. Sometimes, we can draw a safe conclusion that configurations are born because of environments.

Eugen Paraschiv briefly discussed this in his post Configuration Must Be Environment Specific. It is also elaborated in depth in the “Containerization, scheduling, and configuration management” section in Configuration management challenges in modern application architectures.

So, what exactly prevented us from keeping the artifacts of our applications in consistency in different environments? Sometimes Docker doesn’t deliver the “Build Once, Run Anywhere” experience with ease as advertised, which is usually attributed to the difference in environment configuration. Here are some simple examples to help you better understand.

  • The logLevel is set to DEBUG in the development environment, INFO in the staging environment, and WARNING in the production environment.
  • The database runs on a 4-core 8 GB-RAM machine in the development environment, but on a 32-core 96 GB-RAM machine in the production environment.
  • The maximum thread number in the execution thread pool of the daily environment should be set to 15, while this number should be larger in the production environment, which is 150 by default.
  • In the online environment, application data sources in the Central Data Center need to connect to Database A, while application data sources in Shenzhen Data Center should connect to Database B due to the proximity.
  • Two-way synchronization switch should be switched off only in Mini Taobao environment.
  • We made so many changes in this version that the new features should be made available only in the online Hangzhou Unit environment rather than other unit environments.

This topic briefly explains how to replace Spring Cloud Config with Alibaba Cloud ACM in Spring Cloud to simplify environment configuration management, and helps you understand the ACM-based solutions of simplifying micro-service environment configuration management. In addition, the pros and cons of ACM and Spring Cloud Config are compared.

User stories

In daily engineering practice, we often picture a simple scenario with a user story to facilitate the elaboration and communication. This is an illustration used for preaching in the early stages:

acm_pic5_config_service

Taking Movie Service as an example. Assuming that we need to retrieve a list of all movies from the relational database MySQL(RDS). Top-performance machines are only required for the production database, and we need different databases in the testing, staging, and production environments. Therefore, our applications need to have different data source configuration, connection pool configuration, database security configuration, and so on in different environments.

This topic explains how to map different environments with ACM Namespace, and set different data source configurations for Movie Service in different running environments.

As shown in the following figure:

Scenario

Create micro-service: movie service

  • Create Spring Boot Starter micro-service: movie service

    Movie service’s business logic is very straightforward: to list all movies in MySQL(RDS):

    spring_micro_movie_sample_3

    Here we created a standard JPA application (similar to the sample project on Spring official website Accessing data with MySQL). The project structure is as shown in the following figure:

    spring_micro_movie_sample_1

  • Introduce JPA, MySQL, connection pool HikariCP and WEB dependencies

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-web</artifactId>
    4. </dependency>
    5. <dependency>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-data-jpa</artifactId>
    8. </dependency>
    9. <dependency>
    10. <groupId>mysql</groupId>
    11. <artifactId>mysql-connector-java</artifactId>
    12. <scope>runtime</scope>
    13. </dependency>
    14. <dependency>
    15. <groupId>com.zaxxer</groupId>
    16. <artifactId>HikariCP</artifactId>
    17. <version>2.7.6</version>
    18. </dependency>
  • Create MySQL(RDS) database and users

    1. mysql> create database db_example; -- Create the new database
    2. mysql> create user 'springuser'@'localhost' identified by 'ThePassword'; -- Creates the user
    3. mysql> grant all on db_example.* to 'springuser'@'localhost'; -- Gives all the privileges to the new user on the newly created database

    For detailed steps, see the “Create the database” section in Accessing data with MySQL.

  • Create WEB Controller

    1. package com.alibaba.demo.microsvc.controller;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.ResponseBody;
    5. import org.springframework.web.bind.annotation.RestController;
    6. import com.alibaba.demo.microsvc.dao.MovieRepository;
    7. import com.alibaba.demo.microsvc.model.Movie;
    8. @RestController
    9. public class MovieController {
    10. @Autowired
    11. MovieRepository movieRepository;
    12. @RequestMapping("/list-movies")
    13. public @ResponseBody Iterable<Movie> listMovies() {
    14. return movieRepository.findAll();
    15. }
    16. }

Use Namespace in ACM to create isolated environment configuration

Note: To use ACM on Alibaba Cloud, you must activate this service. For instructions on how to activate this service, see Getting Started with ACM. Once you activate the service and log on, you can create namespaces and configurations in the ACM console.

  • Create three environments in ACM: dev, stage, and prod

    Create Namespaces

  • Create configuration respectively for dev, stage, and prod environments

    Create Configuration

Wrap-up: What have we accomplished so far?

In the previous step, we set different values for the same configuration item in different environments. Taking the spring.datasource.url configuration item as an example, we connect different databases to different environments by setting different URLs, and only enables SSL (useSSL=true) in the production environment.

  1. dev:
  2. spring.datasource.url=jdbc:mysql://localhost:3306/db_example?useSSL=false
  3. prod:
  4. spring.datasource.url=jdbc:mysql://30.5.101.169:3306/db_example?useSSL=true

In addition, we have set a larger database connection pool and a smaller timeout value for the prod environment.

  1. dev:
  2. spring.datasource.hikari.connection-timeout=60000
  3. spring.datasource.hikari.maximum-pool-size=10
  4. prod:
  5. spring.datasource.hikari.connection-timeout=15000
  6. spring.datasource.hikari.maximum-pool-size=200

To make it easier to debug, we only enables SQL Trace in the dev environment.

  1. dev:
  2. spring.jpa.show-sql=true

Integrate Movie Service with ACM

Next, we integrate Movie Service with ACM to obtain the corresponding environment configuration from ACM. For instructions on how to use ACM in Spring Cloud, see Prerequisites.

  • Introduce ACM dependencies to Movie Service

    1. <dependency>
    2. <groupId>com.alibaba.cloud</groupId>
    3. <artifactId>spring-cloud-starter-acm</artifactId>
    4. <version>1.0.1</version>
    5. </dependency>
  • Configure ACM connection information, namespace, accessKey, secretKey, and other information in application.properties

    1. spring.application.name=movie-service
    2. spring.application.group=com.alibaba.cloud.acm
    3. alibaba.acm.endpoint=acm.aliyun.com
    4. alibaba.acm.namespace=<your_namespace_id>
    5. alibaba.acm.accessKey=<your_ak>
    6. alibaba.acm.secretKey=<your_sk>

Note:You can find your namespace_id, accessKey, secretKey, and other information in “namespace details” or “code example of configuration”.

Namespace Details

Access Movie Service from your browser

spring_micro_movie_sample_7

View pushing and refreshing history of ACM configurations

If spring-boot-starter-actuator dependency has been introduced to Movie Service, and management.security.enabled=false is set in application.properties, you can view the application’s configuration consumption and refreshing history from endpoint http://<<ip:port>>/acm.

spring_micro_movie_sample_8

You can also view the configuration’s push track, configuration version, and other information from the ACM console. For instructions, see ACM documentation.

Brief comparison between ACM and Spring Cloud Config

What’s compared Spring Cloud Config Alibaba Cloud ACM
Spring Cloud seamless integration Supported Supported
Source code distribution method Open source To be open-sourced soon
Billing method Free Free
Large scale (over 100,000 configuration items) production verification No publicly available cases of large scale production verification Verified with Alibaba data center’s production environment featuring millions of configuration items, with over 100 million configuration changes pushed every day, and the Double 11 shopping spree and many other demanding scenarios
Configuration management UI console No console, dependent on IDE, GIT, and other third-party tools Professional configuration management UI console
Multi-language support Mainly supports Java ecosystem, without any native clients for other languages Supports Node.js, C++ and other native multi-language clients
Multi-data center, Local active-active disaster recovery, multi-zone, and other architectures Dependent on support of GIT, ZooKeeper, and so on, without an explicit official statement Supported
Configuration change push Dependent on RabbitMQ/KAFKA Built-in push mechanism, without external dependency
Timeliness of large scale configuration push Dependent on SLA and Webhooks such as GIT Webhooks and so on. Enterprise level large scale production capability is yet to be verified. Industrial level production in milliseconds
Audit capability for configuration changes Week Built-in audit mechanism (with an audit capability conforming to National Graded Protection of Information Security - Level 3)
Push track Unable to view the real-time monitoring of configurations pushed to the client Provides configuration change push tracks for monitoring configuration change push status
Data isolation Supports application, profile, label, git repo, and other isolation policies In addition to isolation policies provided by Spring Cloud, ACM supports multi-tenant, app, data_id, group and other multi-level isolation policies
Production and O&M cost High (must have sufficient GIT/RabbitMQ knowledge and talent reserve) Low (no third-party component dependencies)
High availability N/A (customers must assume all risks) 99.99% (Alibaba Cloud assumes the risks)
Secure communication Supports SSL Supports SSL
Disaster tolerance 2 levels (storage, server cache) 3 levels + client local disaster tolerance

Summary

This topic explains how to use Alibaba Cloud Application Configuration Management (ACM) in combination with Spring Cloud to simplify environment configuration management in a micro-service architecture, by assuming that in the testing and production environments, different databases are connected, and different data source (including connection pool) parameters are configured.

Download project

Sample project used in this article can be downloaded from movie-service.tar.gz.

This project has passed the test in the following environments:

  • Spring Cloud Edgware.RELEASE
  • Spring Boot 1.5.9.RELEASE
  • HikariCP 2.7.6
  • MySQL 5.7.11
  • ACM 4.2.0
  • ACM Spring Cloud SDK 1.0.1

Note: Before running this project locally, make sure to set your own ACM accessKey and secretKey in application.properties.

Thank you! We've received your feedback.