|
|
|
@ -17,35 +17,27 @@
|
|
|
|
|
|
|
|
|
|
package cn.hippo4j.config.springboot.starter.refresher;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
import cn.hippo4j.common.toolkit.JSONUtil;
|
|
|
|
|
import cn.hippo4j.common.toolkit.StringUtil;
|
|
|
|
|
import io.etcd.jetcd.ByteSequence;
|
|
|
|
|
import io.etcd.jetcd.Client;
|
|
|
|
|
import io.etcd.jetcd.ClientBuilder;
|
|
|
|
|
import io.etcd.jetcd.KeyValue;
|
|
|
|
|
import io.etcd.jetcd.Watch;
|
|
|
|
|
import io.etcd.jetcd.*;
|
|
|
|
|
import io.etcd.jetcd.kv.GetResponse;
|
|
|
|
|
import io.etcd.jetcd.watch.WatchEvent;
|
|
|
|
|
import io.etcd.jetcd.watch.WatchResponse;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*@author : wh
|
|
|
|
|
*@date : 2022/8/30 17:59
|
|
|
|
|
*@description:
|
|
|
|
|
* @author : wh
|
|
|
|
|
* @date : 2022/8/30 17:59
|
|
|
|
|
* @description:
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh {
|
|
|
|
|
|
|
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
|
|
private Client client;
|
|
|
|
|
|
|
|
|
|
private static final String ENDPOINTS = "endpoints";
|
|
|
|
@ -69,15 +61,13 @@ public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh {
|
|
|
|
|
String authority = etcd.get(AUTHORITY);
|
|
|
|
|
String key = etcd.get(KEY);
|
|
|
|
|
Charset charset = StringUtil.isBlank(etcd.get(CHARSET)) ? StandardCharsets.UTF_8 : Charset.forName(etcd.get(CHARSET));
|
|
|
|
|
|
|
|
|
|
ClientBuilder clientBuilder = Client.builder().endpoints(endpoints.split(","));
|
|
|
|
|
//todo
|
|
|
|
|
// todo
|
|
|
|
|
if (Objects.isNull(client)) {
|
|
|
|
|
client = StringUtil.isAllNotEmpty(user, password) ? clientBuilder.user(ByteSequence.from(user, charset))
|
|
|
|
|
.password(ByteSequence.from(password, charset)).authority(authority)
|
|
|
|
|
.build() : clientBuilder.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo Currently only supports json
|
|
|
|
|
GetResponse getResponse = client.getKVClient().get(ByteSequence.from(key, charset)).get();
|
|
|
|
|
KeyValue keyValue = getResponse.getKvs().get(0);
|
|
|
|
@ -97,20 +87,17 @@ public class EtcdRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh {
|
|
|
|
|
Map map = JSONUtil.parseObject(value, Map.class);
|
|
|
|
|
dynamicRefresh(keyValue1.getKey().toString(charset), map);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onError(Throwable throwable) {
|
|
|
|
|
log.error("dynamic thread pool etcd config watcher exception ", throwable);
|
|
|
|
|
log.error("Dynamic thread pool etcd config watcher exception ", throwable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCompleted() {
|
|
|
|
|
log.info("dynamic thread pool etcd config key refreshed, config key {}", key);
|
|
|
|
|
log.info("Dynamic thread pool etcd config key refreshed, config key {}", key);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|