edit-icon download-icon

Prerequisites

Last Updated: Jun 13, 2018

This topic explains how to use the ACM API with sample code.

Get the SDK

Enter the following POM configuration to establish API dependency on the SDK.

  1. <dependency>
  2. <groupId>com.alibaba.edas.acm</groupId>
  3. <artifactId>acm-sdk</artifactId>
  4. <version>1.0.8</version>
  5. </dependency>
  6. <!-- Remove the following if logging implementation is available. -->
  7. <dependency>
  8. <groupId>ch.qos.logback</groupId>
  9. <artifactId>logback-classic</artifactId>
  10. <version>1.1.7</version>
  11. </dependency>

Sample code

  1. import java.util.Properties;
  2. import com.alibaba.edas.acm.ConfigService;
  3. import com.alibaba.edas.acm.exception.ConfigException;
  4. import com.alibaba.edas.acm.listener.ConfigChangeListener;
  5. import com.alibaba.edas.acm.listener.PropertiesListener;
  6. // Sample code, for sample test only.
  7. public class ACMTest {
  8. // Property/Switch
  9. private static String config = "DefaultValue";
  10. private static Properties acmProperties = new Properties();
  11. public static void main(String[] args) {
  12. try {
  13. // Copy the corresponding values from the "Namespace Management" in the console.
  14. Properties properties = new Properties();
  15. properties.put("endpoint", "$endpoint");
  16. properties.put("namespace", "$namespace");
  17. // Access ACM with instance RAM role: https://www.alibabacloud.com/help/en/doc-detail/72013.html
  18. // properties.put("ramRoleName", "$ramRoleName");
  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. // Actively get the configuration.
  26. String content = ConfigService.getConfig("${dataId}", "${group}", 6000);
  27. System.out.println(content);
  28. // Add listeners to the configuration during initialization, which calls back a notification when the configuration is changed.
  29. ConfigService.addListener("${dataId}", "${group}", new ConfigChangeListener() {
  30. public void receiveConfigInfo(String configInfo) {
  31. // After the configuration is updated, the latest value is returned to the user via this callback function.
  32. // Remember not to make blocking operations in callback function. Otherwise the notification thread will be blocked.
  33. config = configInfo;
  34. System.out.println(configInfo);
  35. }
  36. });
  37. /**
  38. * The following listener can be used if the content of the configuration value is in properties (key=value) format. This allows you to manage multiple configuration items in one configuration.
  39. */
  40. /**
  41. ConfigService.addListener("${dataId}", "${group}", new PropertiesListener() {
  42. @Override
  43. public void innerReceive(Properties properties) {
  44. // TODO Auto-generated method stub
  45. acmProperties = properties;
  46. System.out.println(properties);
  47. }
  48. });
  49. **/
  50. } catch (ConfigException e) {
  51. e.printStackTrace();
  52. }
  53. // Keep the main thread alive throughout the test, because the configuration subscription runs in a daemon thread, which exits once the main thread exits. The following code is not required in a real environment.
  54. while (true) {
  55. try {
  56. Thread.sleep(1000);
  57. } catch (InterruptedException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. }
  62. // Expose the configuration value via the get configuration API for external use.
  63. public static String getConfig() {
  64. return config;
  65. }
  66. // Expose the configuration value via the get configuration API for external use.
  67. public static Object getPorpertiesValue(String key) {
  68. if (acmProperties != null) {
  69. return acmProperties.get(key);
  70. }
  71. return null;
  72. }
  73. }

Options of passing parameters

To get you started, the sample code initializes the parameters with code. However, the real production process may involve different environments (namely different accounts, regions, or namespaces), and the parameters vary with environments, so you must use variables to pass the parameters. To facilitate the input parameter configuration and reduce the configuration cost, ACM provides multiple options of passing parameters.

Note: EDAS automatically injects for you during the publishing, and therefore you don’t need to take any action.

Initialization parameters How to pass parameters
endpoint From highest priority to lowest priority:
  1. By JVM parameters: -Daddress.server.domain=xxx
  2. By environmental variables: address_server_domain=xxx
  3. By code: see the preceding sample code
namespace From highest priority to lowest priority:
  1. By JVM parameters: -Dtenant.id=xxx
  2. By code: see the preceding sample code
accessKey/secretKey From highest priority to lowest priority:
  1. By files: store accessKey and secretKey in the format of Properties (which meets the requirement of the public void java.io.Reader.Properties.load(Reader reader) method) in a file specified with -Dspas.identity. If not specified, the default file /home/admin/.spas_key/<ApplicationName> (the application name is specified with -Dproject.name) is used
  2. By environmental variables: spas_accessKey=xxx spas_secretKey=xxx
  3. By code: see the preceding sample code
Thank you! We've received your feedback.