acm java_ACM Java Native SDK 概述
示例代碼
添加依賴后,即可在程序中使用 ACM Java Native SDK 提供的接口。
說明 請將代碼中的 $regionId、$endpoint、$namespace、$accessKey、$secretKey 分別替換為 ACM 控制臺上命名空間詳情對話框內的地域 ID、End Point、命名空間 ID、AccessKey、SecretKey。
import java.util.Properties;
import com.alibaba.edas.acm.ConfigService;
import com.alibaba.edas.acm.exception.ConfigException;
import com.alibaba.edas.acm.listener.ConfigChangeListener;
import com.alibaba.edas.acm.listener.PropertiesListener;
// 示例代碼,僅用于示例測試
public class ACMTest {
// 屬性/開關
private static String config = "DefaultValue";
private static Properties acmProperties = new Properties();
public static void main(String[] args) {
try {
// 從控制臺命名空間管理中拷貝對應值
Properties properties = new Properties();
properties.put("endpoint", "$endpoint");
properties.put("namespace", "$namespace");
// 通過 ECS 實例 RAM 角色訪問 ACM
// properties.put("ramRoleName", "$ramRoleName");
properties.put("accessKey", "$accessKey");
properties.put("secretKey", "$secretKey");
// 如果是加密配置,則添加下面兩行進行自動解密
//properties.put("openKMSFilter", true);
//properties.put("regionId", "$regionId");
ConfigService.init(properties);
// 主動獲取配置
String content = ConfigService.getConfig("${dataId}", "${group}", 6000);
System.out.println(content);
// 初始化的時候,給配置添加監聽,配置變更會回調通知
ConfigService.addListener("${dataId}", "${group}", new ConfigChangeListener() {
public void receiveConfigInfo(String configInfo) {
// 當配置更新后,通過該回調函數將最新值返回給用戶。
// 注意回調函數中不要做阻塞操作,否則阻塞通知線程。
config = configInfo;
System.out.println(configInfo);
}
});
/**
* 如果配置值的內容為properties格式(key=value),可使用下面監聽器。以便一個配置管理多個配置項
*/
/**
ConfigService.addListener("${dataId}", "${group}", new PropertiesListener() {
@Override
public void innerReceive(Properties properties) {
// TODO Auto-generated method stub
acmProperties = properties;
System.out.println(properties);
}
});
**/
} catch (ConfigException e) {
e.printStackTrace();
}
// 測試讓主線程不退出,因為訂閱配置是守護線程,主線程退出守護線程就會退出。 正式代碼中無需下面代碼
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 通過get接口把配置值暴露出去使用
public static String getConfig() {
return config;
}
// 通過get接口把配置值暴露出去使用
public static Object getPorpertiesValue(String key) {
if (acmProperties != null) {
return acmProperties.get(key);
}
return null;
}
}
總結
以上是生活随笔為你收集整理的acm java_ACM Java Native SDK 概述的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python从命令行获取参数_pytho
- 下一篇: 获取局域网内服务器信息,使用Java代码