Get started with ACM

Last Updated: Apr 09, 2018

This topic provides a quick walkthrough of the most fundamental concepts and features of ACM through two Quick Start tasks:

  • Create and dynamically adjust configuration items
  • Manage configurations in different environments

The following figure shows the procedure from activating the ACM service through to performing basic tasks:

quickstart

Sign up for ACM service

To use ACM service, activate it first by performing the following steps:

  1. Log on to the Alibaba Cloud official website.

  2. Move your mouse onto Products > Middleware, then click Application Configuration Management ACM to enter the product homepage.

  3. On the product homepage, click Buy Now, and sign up for the service as instructed.

If you have already signed up for the service, log on to the ACM console directly.

Task 1: Create and dynamically adjust configuration values

Scenario

The business application “myapp.jar” is deployed onto two production servers. This application contains a configuration file “app.cfg”, which consists of two configuration items: thread pool size and log level. Now, you need to adjust the configuration of the application on these two servers simultaneously and refresh the state of the application dynamically.

The scenario is shown in the following figure:

quickstart-2

Configuration details are as follows:

  1. ## app.cfg ##
  2. threadPoolSize=5
  3. logLevel=WARN

Procedure

Step 1: Create configuration in ACM

  1. Log on to the ACM console.

  2. In the left-side navigation pane, select Configuration Management, and click Create Configuration in the upper-right corner.

  3. Enter the following on the Create Configuration page.

    • Data ID: com.acm.myapp.app.cfg
    • Group: myapp
    • Content:
      1. threadPoolSize=5
      2. logLevel=WARN

Step 2: Create a Maven project

  1. Create a Maven project or download the myapp.tar project.

    For how to install and use Maven, see Maven documentation.

    1. mvn archetype:generate -DgroupId=com.acm.sample -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    The preceding command creates the following project structure:

    1. myapp
    2. |-- pom.xml
    3. `-- src
    4. |-- main
    5. | `-- java
    6. | `-- com
    7. | `-- acm
    8. | `-- sample
    9. | `-- App.java
    10. `-- test
    11. `-- java
    12. `-- com
    13. `-- mycompany
    14. `-- app
    15. `-- AppTest.java
  2. Add the ACM Client Native API dependency in Pom.

    1. <dependencies>
    2. <dependency>
    3. <groupId>com.alibaba.edas.acm</groupId>
    4. <artifactId>acm-sdk</artifactId>
    5. <version>1.0.6</version>
    6. </dependency>
    7. <!-- Remove the following if logging implementation is available. -->
    8. <dependency>
    9. <groupId>ch.qos.logback</groupId>
    10. <artifactId>logback-classic</artifactId>
    11. <version>1.1.7</version>
    12. </dependency>
    13. </dependencies>
  3. Listen for configuration changes with an API.

    1. //-- App.java
    2. package com.acm.sample;
    3. import java.io.IOException;
    4. import java.io.StringReader;
    5. import java.util.Properties;
    6. import com.alibaba.edas.acm.listener.ConfigChangeListener;
    7. import com.alibaba.edas.acm.ConfigService;
    8. import com.alibaba.edas.acm.exception.ConfigException;
    9. public class App {
    10. private static Properties appCfg = new Properties();
    11. public static void initAndWatchConfig() {
    12. final String dataId = "com.acm.myapp.app.cfg";
    13. final String group = "myapp";
    14. final long timeoutInMills = 3000;
    15. // Copy the corresponding values from the "Namespace Management" in the console.
    16. Properties properties = new Properties();
    17. properties.put("endpoint", "$endpoint");
    18. properties.put("namespace", "$namespace");
    19. properties.put("accessKey", "$accessKey");
    20. properties.put("secretKey", "$secretKey");
    21. // If it is an encrypted configuration, then add the following two lines for automatic decryption.
    22. //properties.put("openKMSFilter", true);
    23. //properties.put("regionId", "$regionId");
    24. ConfigService.init(properties);
    25. // Start a use-once scenario to get the configuration values directly.
    26. try {
    27. String configInfo = ConfigService.getConfig(dataId, group, timeoutInMills);
    28. appCfg.load(new StringReader(configInfo));
    29. } catch (ConfigException e1) {
    30. e1.printStackTrace();
    31. } catch (IOException e) {
    32. e.printStackTrace();
    33. }
    34. // Listen for configuration changes to get the latest values.
    35. ConfigService.addListener(dataId, group, new ConfigChangeListener() {
    36. public void receiveConfigInfo(String configInfo) {
    37. try {
    38. appCfg.load(new StringReader(configInfo));
    39. } catch (Exception e) {
    40. // process exception
    41. }
    42. refreshApp();
    43. }
    44. });
    45. }
    46. public static void refreshApp() {
    47. System.out.println("current thread pool size: " + appCfg.getProperty("threadPoolSize"));
    48. System.out.println("current log level: " + appCfg.getProperty("logLevel"));
    49. System.out.println("");
    50. }
    51. public static void main(String[] args) {
    52. initAndWatchConfig();
    53. // Make sure the main thread does not exit.
    54. while (true) {
    55. try {
    56. Thread.sleep(1000);
    57. } catch (InterruptedException e) {
    58. }
    59. }
    60. }
    61. }

Step 3: Deploy and start the application

Copy the JAR package to one or two servers to deploy and then start the application.

The deployment can be performed by running the following command in shell:

  1. ${JAVA_HOME}/java -cp myapp.jar com.acm.sample.App

To run Java programs, install JDK on the server and set the JAVA_HOME environment variable.

Step 4: Search and change the configuration in the ACM console

  1. Log on to the ACM console.

  2. On the Configurations page, find the configuration created in Step 1.

  3. Click Details in the Actions column to view configuration details.

  4. Edit configuration content.

    Adjust the configuration items to:

    1. threadPoolSize=15
    2. logLevel=DEBUG
  5. Click Publish.

Step 5: Verify result

After the configuration is released, we can see that the configuration changes are received simultaneously on both servers on which the application is deployed, and the following information is printed.

  1. current thread pool size: 15
  2. current log level: DEBUG

Conclusion

In Quick Start Task 1, a configuration is created for the application “myapp” on ACM and the Native API of ACM is used in the program to listen for changes to this configuration. If any change is made to the configuration in the ACM console, the change is received on all the servers on which the application is deployed, and the state of the application is refreshed accordingly.

Task 2: Make different settings for a configuration in different environments

Scenario

In this task, we will use the “Namespace” function of ACM and set different values for one configuration in a test environment, a pre-production environment, and a production environment.

As shown in the figure:

quick_start_pi

Procedure

Step 1: Create a namespace

The following is an example of creating the namespace “Development”.

  1. Log on to the ACM console.

  2. In the left-side navigation pane, select Namespace, and click the Create Namespace button in the upper-right corner:

  3. In the Create Namespace dialog box, enter Development for the namespace name.

  4. Repeat Steps 1 through 3 to create namespaces “Testing” and “Staging”.

Step 2: Create a configuration under each namespace

  1. On the Configurations page, select the namespace Development.

  2. Repeat Step 1 in Task 1 to create a configuration with the same name.

Conclusion

Task 2 provides a typical example of managing a configuration in multiple environments. In real-world business scenarios, we often need to set different values for one configuration item based on different environments. You can easily do so with the Namespace feature of ACM.

Thank you! We've received your feedback.