Abstract: In this article, we will make a simple example on how to use alicloud ACM to replace Spring Cloud Config in Spring Cloud to help simplify the configuration management of the environment. This will help you understand the scheme to simplify the configuration management of microservice environment based on ACM. The advantages and disadvantages of ACM and Spring Cloud Config schemes will be briefly compared.

Click here to view the original text:click.aliyun.com/m/41595/

Configured environment properties

There is no doubt that the diversity and complexity of the environments in which systems end up being delivered adds to the burden of configuration management. Sometimes, even literally, configuration is created by the environment.

This was explained briefly in Eugen Paraschiv’s post Configuration Must Be Environment Specific, and in depth in my post on containerization, scheduling, and Configuration Management, challenges of Configuration Management in Modern Application Architectures.

If asked, what causes the artifact of our application to not stay the same across environments, sometimes Docker can’t easily achieve Build Once, Run Anywhere! The answer is often a difference in environment configuration. To help you understand, here are some simple examples:

Set logLevel to DEBUG in the development environment, INFO in the pre-delivery environment, and WARNING in the production environment. Use a 4-core 8G machine to run the database in the development environment. Using 32 nuclear 96 g machine run in production database In the daily environment to the maximum number of threads executing thread pool should be set to 15, and this value should be A bit bigger on production environment, the default is set to 150 online environment, center machine room, library, application data source need connection A room, and shenzhen application should connect to the nearest library B Only in a small taobao environment, This change is a bit big and the new feature is only available online in hangzhou unit. In this article, we will make a simple example on how to use alicloud ACM to replace Spring Cloud Config in Spring Cloud to help simplify the configuration management of the environment, and help you understand the scheme to simplify the configuration management of microservice environment based on ACM. The advantages and disadvantages of ACM and Spring Cloud Config schemes will be briefly compared.

In order to help understand requirements and scenarios, in daily engineering practice, we usually use User stories to preset a simple scenario for explanation and communication. Those familiar with the history of micro-services must be familiar with the following early sermon:



In this article, let’s take the Movie Service as an example. Suppose we need to retrieve all the Movie information lists from the relational database MySQL(RDS), but we need to use different databases in our test, pre-release, and production environments because only the production library needs the top-loaded machine. In this way, our application needs to configure different data source configuration, connection pool configuration, database security configuration and so on in different environments. We will introduce how to set different data source configuration for Movie Service in different operating environments based on the Namespace mapping ability of Alicloud ACM.

As shown below:

The business logic of the Movie Service is simple. The business logic of the Movie Service is simple. The business logic of the Movie Service is simple.



Here we have created a standard JPA application (similar to the sample project on the Spring website)Accessing data with MySQL, our engineering structure is shown in the figure below:

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>2.7.6</version>
</dependency>Copy the code

Create MySQL(RDS) database and user

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

For details, see ‘Create the database’ in Accessing Data with MySQL

Create a WEB Controller

package com.alibaba.demo.microsvc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.demo.microsvc.dao.MovieRepository;
import com.alibaba.demo.microsvc.model.Movie;

@RestController
public class MovieController {

  @Autowired
  MovieRepository movieRepository;

  @RequestMapping("/list-movies")
  public @ResponseBody Iterable<Movie> listMovies() {
        returnmovieRepository.findAll(); }}Copy the code

You can use the Namespace in the ACM to create an isolated environment configuration

Note: The prerequisite for using ACM on Alicloud is to enable the ACM service. For details about how to enable the ACM service, see ACM Quick Start. After enabling the ACM service and logging in, you can access the ACM console to create namespaces and configure namespaces

Create 3 environments in ACM (Dev, Stage, PROd)



Create configurations for dev,stage, and Prod environments

Notice what we’ve accomplished?

In the previous step, we set different values for the same configuration item for different environments. For example, for the ‘spring.datasource. Url’ configuration item, we set different urls to connect to different databases for each environment and enable SSL only in production environment (useSSL=true).

dev: spring.datasource.url=jdbc:mysql://localhost:3306/db_example? useSSL=false> prod: spring. The datasource. Url = JDBC: mysql: / / 30.5.101.169:3306 / db_example? useSSL=true
>    Copy the code

We also set larger database connection pools and smaller connection timeouts for production environments (PRODs)

  dev:
        spring.datasource.hikari.connection-timeout=60000
        spring.datasource.hikari.maximum-pool-size=10
>        
    prod:
        spring.datasource.hikari.connection-timeout=15000
        spring.datasource.hikari.maximum-pool-size=200
>    Copy the code

In order to facilitate development and debugging, we only turn on SQL Trace in the development environment

    dev:
        spring.jpa.show-sql=trueCopy the code

Now we will integrate Movie Service and ACM to obtain the configuration of the corresponding environment from ACM. For details about how to use the ACM in the Spring Cloud, see the ACM official documentation Development Guide > SDK Reference > Spring Cloud ACM

Introduce ACM dependencies for the Movie Service

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-acm</artifactId> The < version > < / version 1.0.1 > < / dependency >Copy the code

On application.properties, configure ACM connection information, namespace, accessKey, and secretKey

spring.application.name=movie-service spring.application.group=com.alibaba.cloud.acm alibaba.acm.endpoint=acm.aliyun.com  alibaba.acm.namespace=<your_namespace_id> alibaba.acm.accessKey=<your_ak> alibaba.acm.secretKey=<your_sk>Copy the code

Note: you can find your namespace_id, accessKey, secretKey, etc. in the ACM ‘namespace Details’ or’ Config Example code ‘, as shown below:

Access the Movie Service in your browser

View the update information about ACM configuration push

If the movie service introduced spring – the boot – starter – physical dependence and in application. The properties set up management security, enabled = false, can through the endpoint http://<>/ ACM The following figure shows the configuration consumption and refresh of the application:

You can also view the configuration push track and configuration version information on the ACM console. For details about how to use the configuration, see the OFFICIAL ACM documentation.

A simple comparison between ACM and Spring Cloud Config





Extended thinking – why not put the configuration in the application’s own JAR package?

In my post entitledConfiguration management challenges in modern application architecturesThere’s a long profile in “.

How do you ensure that your tests are valid if test/production packages are different because of configuration?

Anyone who cares about microservices has read the microservices Bible

Sections 6.7 and 6.8 of the previous book have excellent descriptions of environments and configurations, which are quoted here

6.8 Service Configuration

The service requires some configuration. Ideally, the effort of these configurations should be minimal and limited to configuration differences between environments. If your configuration modifies the basic behavior of many services, or the configuration varies greatly from one environment to another, you may find a particular problem in only one environment, which can be extremely painful. So, if there are configuration differences between different environments, how do you handle them in the deployment process? One approach is to create a different build for each environment and build the configuration into that build. At first this approach seemed to make sense. The configuration is already built in, and it should work just fine with a simple deployment, right? This is problematic. Remember the concept of continuous delivery? We want to create a build as a release candidate and move it down the pipeline to finally confirm that it can be released into production. Imagine that I build a customer-service-test build and a customer-service-prod build. If the Customer-service-test build passes the Test, but the build I’m actually deploying is Customer-service-Prod, how do I verify that the software actually runs in production? There are other challenges. First, creating these constructs is time-consuming. Second, you need to know what environment exists when you build. How do you handle sensitive configuration data? I don’t want to commit production database passwords to the source code, but this is usually unavoidable when creating these builds. A better approach is to create just one build and manage its configuration separately. Formally, this could be a properties file for each environment, or some parameters passed into the installation process. Another popular approach for dealing with a large number of microservices is to use proprietary systems to provide configuration, a topic discussed in detail in Chapter 11.

Configuration drift After application deployment operation process, especially after deploying multiple servers, if you use the developers or operations staff manual maintenance configuration file, accumulate over a long period, produces what we call “configuration drift” problem, which is due to the application and rely on the version of the component changes of the differences, As well as different changes made by different teams or people at different points in time, the configuration of the same application on each machine in the data center can be more or less slightly different on each machine, which is often where bugs and major failures can be hidden.

In this article, we take a test and production connection to different databases, configure different data sources (including connection pool) parameters as an example, and introduce how to use Aliccloud configuration center ACM together with Spring Cloud to help you simplify your environment configuration management in microservices architecture.