-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#143 support zookeeper config center
- Loading branch information
Showing
11 changed files
with
288 additions
and
58 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
...c/main/java/cn/hippo4j/example/core/zookeeper/Hippo4jCoreZookeeperExampleApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ple/hippo4j-core-zookeeper-spring-boot-starter-example/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
40 changes: 40 additions & 0 deletions
40
...j-core-zookeeper-spring-boot-starter-example/src/main/resources/zookeeper-demo.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...ot-starter/src/main/java/cn/hippo4j/core/starter/refresher/ZookeeperRefresherHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
Oops, something went wrong.