Commit 1ba55fd9 authored by 陈红帅's avatar 陈红帅

perf(admin):读取zookeeper中的配置信息

parent 98036098
......@@ -115,6 +115,13 @@
<version>${project.parent.version}</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
<build>
......
package com.xxl.job.admin;
import com.xxl.job.admin.util.ZookeeperClientUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.StringUtils;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
/**
* @author xuxueli 2018-10-28 00:38:13
......@@ -9,8 +15,39 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class XxlJobAdminApplication {
public static void main(String[] args) {
public static void main(String[] args){
init(args);
SpringApplication.run(XxlJobAdminApplication.class, args);
}
private static void init(String[] args){
// 促使化zookeeper客户端工具类
if(args == null || args.length == 0){
throw new RuntimeException("请传入zookeeper的地址");
}
ZookeeperClientUtil.init(args[0], "/weixt/conf");
// 创建zookeeper客户端
ZookeeperClientUtil configurationClient = new ZookeeperClientUtil();
try {
String resource = XxlJobAdminApplication.class.getResource("/application.properties").getPath();
Properties properties = new Properties();
properties.load(new FileInputStream(resource));
makeProperties(properties,configurationClient,"spring.datasource.url");
makeProperties(properties,configurationClient,"spring.datasource.username");
makeProperties(properties,configurationClient,"spring.datasource.password");
FileOutputStream fileOutputStream = new FileOutputStream(resource);
properties.store(fileOutputStream,"");
fileOutputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
private static void makeProperties(Properties properties, ZookeeperClientUtil configurationClient, String key){
String value = configurationClient.getData(configurationClient.mainPath + "/" + key);
if(StringUtils.isEmpty(value)){
throw new RuntimeException("zookeeper中没有该属性的值:" + key);
}
properties.setProperty(key,value);
}
}
\ No newline at end of file
package com.xxl.job.admin.util;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooDefs.Perms;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
//@Component
public class ZookeeperClientUtil {
private static Logger logger = LoggerFactory.getLogger(ZookeeperClientUtil.class);
private ZooKeeper zk;
// zookeeper地址
private static String servers;
// 链接超时时间
private int sessionTimeout = 60000;
public static String mainPath;
public static void init(String serversAddress, String mainPathStr) {
servers = serversAddress;
mainPath = mainPathStr;
}
public ZooKeeper getAliveZk() {
ZooKeeper aliveZk = zk;
if (aliveZk != null && aliveZk.getState().isAlive()) {
return aliveZk;
} else {
zkReconnect();
return zk;
}
}
public synchronized void zkReconnect() {
close();
try {
connect();
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void close() {
if (zk != null) {
try {
zk.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
zk = null;
}
}
public void mkPaths(String path) throws KeeperException, InterruptedException {
String[] subs = path.split("\\/");
if (subs.length < 2) {
return;
}
String curPath = "";
for (int i = 1; i < subs.length; i++) {
curPath = curPath + "/" + subs[i];
Stat stat = getAliveZk().exists(curPath, false);
if (stat == null) {
getAliveZk().create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
}
private synchronized void connect() throws IOException {
if (zk == null && !StringUtils.isEmpty(servers))
zk = new ZooKeeper(servers, sessionTimeout, null);
}
public String getData(String path) {
String result = null;
try {
byte[] data = getAliveZk().getData(path, Boolean.TRUE, null);
if (null != data) {
result = new String(data, "UTF-8");
}
} catch (KeeperException e) {
logger.error("获取节点发送错误,path=" + path + " errmsg=" + e.getMessage());
} catch (InterruptedException e) {
logger.error("获取节点发送错误,path=" + path + " errmsg=" + e.getMessage());
} catch (Exception e) {
logger.error("获取子节点发送错误,mainPath=" + mainPath + " errmsg=" + e.getMessage());
}
return result;
}
public void setData(String path, String dataStr) {
try {
Stat stat = getAliveZk().exists(path, false);
if (stat == null) {
getAliveZk().create(path, null, Collections.singletonList(new ACL(Perms.ALL, Ids.ANYONE_ID_UNSAFE)),
CreateMode.PERSISTENT);
}
getAliveZk().setData(path, dataStr.getBytes(), -1);
} catch (KeeperException e) {
logger.error("获取节点发送错误,path=" + path, e);
} catch (InterruptedException e) {
logger.error("获取节点发送错误,path=" + path, e);
} catch (Exception e) {
logger.error("获取子节点发送错误,mainPath=" + mainPath, e);
}
}
public List<String> getChildren() {
List<String> data = null;
try {
data = getAliveZk().getChildren(mainPath, Boolean.FALSE);
} catch (KeeperException e) {
logger.error("获取子节点发送错误,mainPath=" + mainPath, e);
} catch (InterruptedException e) {
logger.error("获取子节点发送错误,mainPath=" + mainPath, e);
} catch (Exception e) {
logger.error("获取子节点发送错误,mainPath=" + mainPath, e);
}
return data;
}
public void setSessionTimeout(int sessionTimeout) {
this.sessionTimeout = sessionTimeout;
}
// public String getMainPath() {
// return mainPath;
// }
// public void setMainPath(String mainPath) {
// this.mainPath = mainPath;
// }
// public static void setServers(String servers) {
// this.servers = servers;
// }
}
......@@ -17,9 +17,10 @@ spring.freemarker.settings.number_format=0.##########
mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://135.202.16.39:3306/xxl-job?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=Space1234
#spring.datasource.url=jdbc:mysql://135.202.16.39:3306/xxl-job?useUnicode=true&characterEncoding=UTF-8
#spring.datasource.url=jdbc:mysql://10.10.10.192:3306/xxl-job-new?useUnicode=true&characterEncoding=UTF-8
#spring.datasource.username=root
#spring.datasource.password=Space1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
......@@ -44,3 +45,5 @@ xxl.job.accessToken=
### xxl-job, i18n (default empty as chinese, "en" as english)
xxl.job.i18n=
###
abc.zookeeper.mainpath=/weixt/conf
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