mirror of https://github.com/longtai-cn/hippo4j
commit
7cf7baaa9f
@ -0,0 +1,18 @@
|
||||
package cn.hippo4j.example.core.zookeeper;
|
||||
|
||||
import cn.hippo4j.core.enable.EnableDynamicThreadPool;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author Redick01
|
||||
* @date 2022/3/14 20:40
|
||||
*/
|
||||
@EnableDynamicThreadPool
|
||||
@SpringBootApplication(scanBasePackages = "cn.hippo4j.example.core")
|
||||
public class Hippo4jCoreZookeeperExampleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Hippo4jCoreZookeeperExampleApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: dynamic-threadpool-zookeeper-example
|
||||
|
||||
dynamic:
|
||||
thread-pool:
|
||||
config-file-type: properties
|
||||
zookeeper:
|
||||
zk-connect-str: 127.0.0.1:2181
|
||||
config-version: 1.0.0
|
||||
root-node: /configserver/userproject
|
||||
node: zookeeper-demo
|
||||
|
||||
#logging:
|
||||
# level:
|
||||
# root: DEBUG
|
@ -0,0 +1,40 @@
|
||||
# Export from zookeeper configuration group: [/configserver/userproject] - [1.0.0] - [zookeeper-demo].
|
||||
|
||||
spring.application.name=dynamic-threadpool-zookeeper-example
|
||||
spring.dynamic.thread-pool.banner=true
|
||||
spring.dynamic.thread-pool.check-state-interval=5
|
||||
spring.dynamic.thread-pool.collect=true
|
||||
spring.dynamic.thread-pool.config-file-type=properties
|
||||
spring.dynamic.thread-pool.enable=true
|
||||
spring.dynamic.thread-pool.executors[0].allow-core-thread-time-out=true
|
||||
spring.dynamic.thread-pool.executors[0].blocking-queue=LinkedBlockingQueue
|
||||
spring.dynamic.thread-pool.executors[0].core-pool-size=2
|
||||
spring.dynamic.thread-pool.executors[0].execute-time-out=1000
|
||||
spring.dynamic.thread-pool.executors[0].keep-alive-time=6691
|
||||
spring.dynamic.thread-pool.executors[0].maximum-pool-size=4
|
||||
spring.dynamic.thread-pool.executors[0].notify.active-alarm=80
|
||||
spring.dynamic.thread-pool.executors[0].notify.capacity-alarm=80
|
||||
spring.dynamic.thread-pool.executors[0].notify.interval=8
|
||||
spring.dynamic.thread-pool.executors[0].notify.is-alarm=true
|
||||
spring.dynamic.thread-pool.executors[0].notify.receives.DING=177****6993
|
||||
spring.dynamic.thread-pool.executors[0].queue-capacity=100
|
||||
spring.dynamic.thread-pool.executors[0].rejected-handler=AbortPolicy
|
||||
spring.dynamic.thread-pool.executors[0].thread-name-prefix=message-consume
|
||||
spring.dynamic.thread-pool.executors[0].thread-pool-id=message-consume
|
||||
spring.dynamic.thread-pool.executors[1].allow-core-thread-time-out=true
|
||||
spring.dynamic.thread-pool.executors[1].blocking-queue=LinkedBlockingQueue
|
||||
spring.dynamic.thread-pool.executors[1].core-pool-size=1
|
||||
spring.dynamic.thread-pool.executors[1].execute-time-out=1000
|
||||
spring.dynamic.thread-pool.executors[1].keep-alive-time=6691
|
||||
spring.dynamic.thread-pool.executors[1].maximum-pool-size=1
|
||||
spring.dynamic.thread-pool.executors[1].notify.active-alarm=80
|
||||
spring.dynamic.thread-pool.executors[1].notify.capacity-alarm=80
|
||||
spring.dynamic.thread-pool.executors[1].notify.interval=8
|
||||
spring.dynamic.thread-pool.executors[1].notify.is-alarm=true
|
||||
spring.dynamic.thread-pool.executors[1].notify.receives.DING=177****6993
|
||||
spring.dynamic.thread-pool.executors[1].queue-capacity=1
|
||||
spring.dynamic.thread-pool.executors[1].rejected-handler=AbortPolicy
|
||||
spring.dynamic.thread-pool.executors[1].thread-name-prefix=message-produce
|
||||
spring.dynamic.thread-pool.executors[1].thread-pool-id=message-produce
|
||||
spring.dynamic.thread-pool.notify-platforms[0].platform=DING
|
||||
spring.dynamic.thread-pool.notify-platforms[0].secret-key=aab197577f6d8cc3aa8b52ee38adb6e16a46642a9c4986f5e45ca6946fdcea6f
|
@ -0,0 +1,95 @@
|
||||
package cn.hippo4j.core.starter.refresher;
|
||||
|
||||
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
|
||||
import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
|
||||
import com.google.common.base.Charsets;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.apache.curator.framework.CuratorFramework;
|
||||
import org.apache.curator.framework.CuratorFrameworkFactory;
|
||||
import org.apache.curator.framework.api.CuratorListener;
|
||||
import org.apache.curator.framework.api.GetChildrenBuilder;
|
||||
import org.apache.curator.framework.api.GetDataBuilder;
|
||||
import org.apache.curator.framework.state.ConnectionState;
|
||||
import org.apache.curator.framework.state.ConnectionStateListener;
|
||||
import org.apache.curator.retry.ExponentialBackoffRetry;
|
||||
import org.apache.curator.utils.ZKPaths;
|
||||
import org.apache.zookeeper.WatchedEvent;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Redick01
|
||||
* @date 2022/3/14 16:03
|
||||
*/
|
||||
@Slf4j
|
||||
public class ZookeeperRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh {
|
||||
|
||||
private CuratorFramework curatorFramework;
|
||||
|
||||
public ZookeeperRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, BootstrapCoreProperties bootstrapCoreProperties) {
|
||||
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
Map<String, String> zkConfigs = bootstrapCoreProperties.getZookeeper();
|
||||
curatorFramework = CuratorFrameworkFactory.newClient(zkConfigs.get("zk-connect-str"),
|
||||
new ExponentialBackoffRetry(1000, 3));
|
||||
String nodePath = ZKPaths.makePath(ZKPaths.makePath(zkConfigs.get("root-node"),
|
||||
zkConfigs.get("config-version")), zkConfigs.get("node"));
|
||||
final ConnectionStateListener connectionStateListener = (client, newState) -> {
|
||||
if (newState == ConnectionState.CONNECTED) {
|
||||
loadNode(nodePath);
|
||||
} else if (newState == ConnectionState.RECONNECTED) {
|
||||
loadNode(nodePath);
|
||||
}};
|
||||
|
||||
final CuratorListener curatorListener = (client, curatorEvent) -> {
|
||||
final WatchedEvent watchedEvent = curatorEvent.getWatchedEvent();
|
||||
if (null != watchedEvent) {
|
||||
switch (watchedEvent.getType()) {
|
||||
case NodeChildrenChanged:
|
||||
case NodeDataChanged:
|
||||
loadNode(nodePath);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}};
|
||||
curatorFramework.getConnectionStateListenable().addListener(connectionStateListener);
|
||||
curatorFramework.getCuratorListenable().addListener(curatorListener);
|
||||
curatorFramework.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* load config info and refresh.
|
||||
* @param nodePath zk config node path.
|
||||
*/
|
||||
public void loadNode(String nodePath) {
|
||||
try {
|
||||
final GetChildrenBuilder childrenBuilder = curatorFramework.getChildren();
|
||||
final List<String> children = childrenBuilder.watched().forPath(nodePath);
|
||||
StringBuilder content = new StringBuilder();
|
||||
children.forEach(c -> {
|
||||
String n = ZKPaths.makePath(nodePath, c);
|
||||
final String nodeName = ZKPaths.getNodeFromPath(n);
|
||||
final GetDataBuilder data = curatorFramework.getData();
|
||||
String value = "";
|
||||
try {
|
||||
value = new String(data.watched().forPath(n), Charsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
final Pair<String, String> keyValue = new ImmutablePair<>(nodeName, value);
|
||||
content.append(keyValue.getKey()).append("=").append(keyValue.getValue()).append("\n");
|
||||
});
|
||||
dynamicRefresh(content.toString());
|
||||
registerNotifyAlarmManage();
|
||||
} catch (Exception e) {
|
||||
log.error("load zk node error, nodePath is {}", nodePath, e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue