acm java_ACM Java Native SDK 概述
示例代碼
添加依賴后,即可在程序中使用 ACM Java Native SDK 提供的接口。
說(shuō)明 請(qǐng)將代碼中的 $regionId、$endpoint、$namespace、$accessKey、$secretKey 分別替換為 ACM 控制臺(tái)上命名空間詳情對(duì)話框內(nèi)的地域 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;
// 示例代碼,僅用于示例測(cè)試
public class ACMTest {
// 屬性/開(kāi)關(guān)
private static String config = "DefaultValue";
private static Properties acmProperties = new Properties();
public static void main(String[] args) {
try {
// 從控制臺(tái)命名空間管理中拷貝對(duì)應(yīng)值
Properties properties = new Properties();
properties.put("endpoint", "$endpoint");
properties.put("namespace", "$namespace");
// 通過(guò) ECS 實(shí)例 RAM 角色訪問(wèn) ACM
// properties.put("ramRoleName", "$ramRoleName");
properties.put("accessKey", "$accessKey");
properties.put("secretKey", "$secretKey");
// 如果是加密配置,則添加下面兩行進(jìn)行自動(dòng)解密
//properties.put("openKMSFilter", true);
//properties.put("regionId", "$regionId");
ConfigService.init(properties);
// 主動(dòng)獲取配置
String content = ConfigService.getConfig("${dataId}", "${group}", 6000);
System.out.println(content);
// 初始化的時(shí)候,給配置添加監(jiān)聽(tīng),配置變更會(huì)回調(diào)通知
ConfigService.addListener("${dataId}", "${group}", new ConfigChangeListener() {
public void receiveConfigInfo(String configInfo) {
// 當(dāng)配置更新后,通過(guò)該回調(diào)函數(shù)將最新值返回給用戶。
// 注意回調(diào)函數(shù)中不要做阻塞操作,否則阻塞通知線程。
config = configInfo;
System.out.println(configInfo);
}
});
/**
* 如果配置值的內(nèi)容為properties格式(key=value),可使用下面監(jiān)聽(tīng)器。以便一個(gè)配置管理多個(gè)配置項(xiàng)
*/
/**
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();
}
// 測(cè)試讓主線程不退出,因?yàn)橛嗛喤渲檬鞘刈o(hù)線程,主線程退出守護(hù)線程就會(huì)退出。 正式代碼中無(wú)需下面代碼
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 通過(guò)get接口把配置值暴露出去使用
public static String getConfig() {
return config;
}
// 通過(guò)get接口把配置值暴露出去使用
public static Object getPorpertiesValue(String key) {
if (acmProperties != null) {
return acmProperties.get(key);
}
return null;
}
}
總結(jié)
以上是生活随笔為你收集整理的acm java_ACM Java Native SDK 概述的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python从命令行获取参数_pytho
- 下一篇: 获取局域网内服务器信息,使用Java代码