Commit b2be902c by 王祖波

holo定时读配置初始化

parent c58de993
package com.gic.haoban.manage.service.dao.mapper.content.holo; package com.gic.haoban.manage.service.dao.mapper.content.holo;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.hologres.client.HoloClient; import com.alibaba.hologres.client.HoloClient;
import com.alibaba.hologres.client.HoloConfig; import com.alibaba.hologres.client.HoloConfig;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.Config;
...@@ -9,6 +14,11 @@ import org.springframework.beans.factory.DisposableBean; ...@@ -9,6 +14,11 @@ import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/** /**
* @Author MUSI * @Author MUSI
* @Date 2023/9/4 4:11 PM * @Date 2023/9/4 4:11 PM
...@@ -21,13 +31,23 @@ public class HoloDataSource implements InitializingBean, DisposableBean { ...@@ -21,13 +31,23 @@ public class HoloDataSource implements InitializingBean, DisposableBean {
HoloClient client; HoloClient client;
public synchronized void init() { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public synchronized void init() {
try { try {
Config appConfig = ConfigService.getAppConfig(); Config appConfig = ConfigService.getAppConfig();
String holoUrl = appConfig.getProperty("holo.url", ""); String holoUrl = appConfig.getProperty("holo.url", "");
String holoUserName = appConfig.getProperty("holo.username", ""); Pair<String, String> holoAuth = getHoloAuth();
String holoPassword = appConfig.getProperty("holo.password", ""); if (holoAuth == null) {
log.info("holo链接初始化获取授权信息失败");
return;
}
if (this.client != null) {
this.client.close();
}
String holoUserName = holoAuth.getKey();
String holoPassword = holoAuth.getValue();
HoloConfig holoConfig = new HoloConfig(); HoloConfig holoConfig = new HoloConfig();
holoConfig.setJdbcUrl(holoUrl); holoConfig.setJdbcUrl(holoUrl);
holoConfig.setUsername(holoUserName); holoConfig.setUsername(holoUserName);
...@@ -53,6 +73,7 @@ public class HoloDataSource implements InitializingBean, DisposableBean { ...@@ -53,6 +73,7 @@ public class HoloDataSource implements InitializingBean, DisposableBean {
if (client == null) { if (client == null) {
this.init(); this.init();
} }
scheduler.scheduleAtFixedRate(this::reinitialize, 1, 1, TimeUnit.HOURS);
} }
...@@ -62,4 +83,23 @@ public class HoloDataSource implements InitializingBean, DisposableBean { ...@@ -62,4 +83,23 @@ public class HoloDataSource implements InitializingBean, DisposableBean {
} }
return this.client; return this.client;
} }
// 重新初始化方法
public synchronized void reinitialize() {
this.init();
}
private Pair<String, String> getHoloAuth() {
//{"appId":"ops_key","cluster":"default","namespaceName":"application","configurations":{"aliyun-key":"{\n\"LTAI5tGn2YicKmgcyTiJDV59\":\"iOp7cICESPZIvIjJRtTmLnCd4gT1Qy\"\n}"},"releaseKey":"20240412153514-b260f04ffbe11f4e"}
String result = HttpUtil.get("http://proapollo.gicdev.com:7080/configs/ops_key/default/application?key=aliyun-key",10000);
JSON json = JSONUtil.parse(result);
String authInfo = json.getByPath("configurations.aliyun-key", String.class);
JSON authJson = JSONUtil.parseObj(authInfo);
Map<String, String> map = authJson.toBean(new TypeReference<Map<String, String>>() {});
for (Map.Entry<String, String> entry : map.entrySet()) {
return new Pair<>(entry.getKey(), entry.getValue());
}
return null;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment