From c582c6313aa91663e9f0e2977c3702a918ba236b Mon Sep 17 00:00:00 2001 From: shandy Date: Thu, 6 Sep 2018 18:31:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8spring=20boot=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E6=94=B9=E9=80=A0,=E4=BD=BF=E7=94=A8java=20-jar=20zkW?= =?UTF-8?q?eb-V1.1.jar=E5=8D=B3=E5=8F=AF=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 + pom-jar.xml | 206 +++++++++++++++++ pom-tomcat.xml | 181 +++++++++++++++ pom.xml | 215 ------------------ .../zkweb/ZkWebSpringBootApplication.java | 40 ++++ .../com/yasenagat/zkweb/model/TreeRoot.java | 2 +- .../java/com/yasenagat/zkweb/util/H2Util.java | 16 +- .../com/yasenagat/zkweb/util/ZkCache.java | 8 +- .../yasenagat/zkweb/util/ZkCfgManager.java | 1 - .../zkweb/util/ZkCfgManagerImpl.java | 75 ++++-- .../com/yasenagat/zkweb/util/ZkManager.java | 32 ++- .../yasenagat/zkweb/util/ZkManagerImpl.java | 77 ++----- .../zkweb/util/ZkSpringBootConfiguration.java | 140 ++++++++++++ .../yasenagat/zkweb/web/ZkCacheServlet.java | 9 +- .../yasenagat/zkweb/web/ZkCfgController.java | 2 +- .../com/yasenagat/zkweb/web/ZkController.java | 25 +- ...itional-spring-configuration-metadata.json | 52 +++++ src/main/resources/application-local.yml | 14 ++ src/main/resources/application.yaml | 63 +++++ src/main/resources/c3p0.properties | 14 -- src/main/resources/log4j.xml | 46 ---- src/main/resources/logback-zkweb.xml | 70 ++++++ .../{controllers.xml => controllers-bak.xml} | 0 ...et-context.xml => servlet-context-bak.xml} | 0 src/main/resources/spring/root-context.xml | 8 - .../{zk.properties => zk-bak.properties} | 0 .../webapp/WEB-INF/{web.xml => web-bak.xml} | 8 +- ...zh_CN.properties => strings_zh.properties} | 1 + src/test/java/T.java | 2 +- 29 files changed, 917 insertions(+), 396 deletions(-) create mode 100644 pom-jar.xml create mode 100644 pom-tomcat.xml delete mode 100644 pom.xml create mode 100644 src/main/java/com/yasenagat/zkweb/ZkWebSpringBootApplication.java create mode 100644 src/main/java/com/yasenagat/zkweb/util/ZkSpringBootConfiguration.java create mode 100644 src/main/resources/META-INF/additional-spring-configuration-metadata.json create mode 100644 src/main/resources/application-local.yml create mode 100644 src/main/resources/application.yaml delete mode 100644 src/main/resources/c3p0.properties delete mode 100644 src/main/resources/log4j.xml create mode 100644 src/main/resources/logback-zkweb.xml rename src/main/resources/spring/appServlet/{controllers.xml => controllers-bak.xml} (100%) rename src/main/resources/spring/appServlet/{servlet-context.xml => servlet-context-bak.xml} (100%) delete mode 100644 src/main/resources/spring/root-context.xml rename src/main/resources/{zk.properties => zk-bak.properties} (100%) rename src/main/webapp/WEB-INF/{web.xml => web-bak.xml} (91%) rename src/main/webapp/resources/locale/{strings_zh_CN.properties => strings_zh.properties} (90%) diff --git a/.gitignore b/.gitignore index 3489f56..f80d16f 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,9 @@ hs_err_pid* /.settings/org.eclipse.wst.jsdt.ui.superType.container /.settings/org.eclipse.wst.jsdt.ui.superType.name /.settings/org.eclipse.wst.validation.prefs +/.factorypath +/.settings/org.eclipse.jdt.apt.core.prefs +/.settings/org.eclipse.jst.j2ee.ejb.annotations.xdoclet.prefs +/.settings/org.springframework.ide.eclipse.beans.core.prefs +/.settings/org.springframework.ide.eclipse.core.prefs +/.springBeans diff --git a/pom-jar.xml b/pom-jar.xml new file mode 100644 index 0000000..0c29dfb --- /dev/null +++ b/pom-jar.xml @@ -0,0 +1,206 @@ + + + 4.0.0 + com.yasenagat.zkweb + zkWeb + jar + v1.1 + + zkWeb + https://github.com/zhitom/zkweb + + org.springframework.boot + spring-boot-starter-parent + 2.0.2.RELEASE + + + UTF-8 + 1.8 + 5.0.5.RELEASE + 1.9.1 + 1.7.25 + 2.9.6 + + + + + + ${basedir}/src/main/webapp + + META-INF/resources/webapp + + WEB-INF/** + + + **/** + + + + ${basedir}/src/main/webapp/WEB-INF + + /WEB-INF/ + + **/** + + + + ${basedir}/src/main/resources + + **/** + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + org.springframework.boot + spring-boot-devtools + true + + + + org.springframework.boot + spring-boot-starter-jdbc + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + + + + + + + + + + + + org.springframework.session + spring-session-jdbc + + + javax.annotation + javax.annotation-api + + + + + + + + + org.slf4j + slf4j-api + + + org.slf4j + jcl-over-slf4j + + + + + com.fasterxml.jackson.jr + jackson-jr-all + + + + javax.servlet + servlet-api + 3.0-alpha-1 + + + + + + org.apache.tomcat.embed + tomcat-embed-jasper + + + + javax.servlet + jstl + + + + + org.apache.zookeeper + zookeeper + 3.4.12 + + + org.slf4j + slf4j-log4j12 + + + + + + com.h2database + h2 + + + + + commons-dbutils + commons-dbutils + 1.7 + + + + com.google.guava + guava + 23.0 + + + + com.mchange + c3p0 + 0.9.5.2 + + + + + com.mchange + mchange-commons-java + 0.2.15 + + + junit + junit + test + + + + \ No newline at end of file diff --git a/pom-tomcat.xml b/pom-tomcat.xml new file mode 100644 index 0000000..cfaf381 --- /dev/null +++ b/pom-tomcat.xml @@ -0,0 +1,181 @@ + + + 4.0.0 + com.yasenagat.zkweb + zkWeb + war + v1.1 + + zkWeb + https://github.com/zhitom/zkweb + + org.springframework.boot + spring-boot-starter-parent + 2.0.2.RELEASE + + + UTF-8 + 1.8 + 5.0.5.RELEASE + 1.9.1 + 1.7.25 + 2.9.6 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + org.springframework.boot + spring-boot-devtools + true + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + org.springframework.boot + spring-boot-starter-jdbc + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + + + + + + + + + + + + org.springframework.session + spring-session-jdbc + + + javax.annotation + javax.annotation-api + + + + + + + + + org.slf4j + slf4j-api + + + org.slf4j + jcl-over-slf4j + + + + + com.fasterxml.jackson.jr + jackson-jr-all + + + + javax.servlet + servlet-api + 3.0-alpha-1 + + + + + + org.apache.tomcat.embed + tomcat-embed-jasper + provided + + + javax.servlet + jstl + + + + + org.apache.zookeeper + zookeeper + 3.4.12 + + + org.slf4j + slf4j-log4j12 + + + + + + com.h2database + h2 + + + + + commons-dbutils + commons-dbutils + 1.7 + + + + com.google.guava + guava + 23.0 + + + + com.mchange + c3p0 + 0.9.5.2 + + + + + com.mchange + mchange-commons-java + 0.2.15 + + + junit + junit + test + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml deleted file mode 100644 index 7c5d8dc..0000000 --- a/pom.xml +++ /dev/null @@ -1,215 +0,0 @@ - - - 4.0.0 - com.yasenagat.zkweb - zkWeb - war - v1.1 - - zkWeb - http://maven.apache.org - - - UTF-8 - 1.8 - 5.0.5.RELEASE - 1.9.1 - 1.7.25 - 2.9.6 - - - - - - maven-war-plugin - 3.2.2 - - - - - - - - - - org.springframework - spring-context - ${org.springframework-version} - - - - - - - - - - org.springframework - spring-webmvc - ${org.springframework-version} - - - - - - - org.slf4j - slf4j-api - ${org.slf4j-version} - - - org.slf4j - jcl-over-slf4j - ${org.slf4j-version} - runtime - - - org.slf4j - slf4j-log4j12 - ${org.slf4j-version} - runtime - - - log4j - log4j - 1.2.16 - runtime - - - - - javax.inject - javax.inject - 1 - - - com.fasterxml.jackson.core - jackson-annotations - ${jackson.version} - - - com.fasterxml.jackson.jr - jackson-jr-all - ${jackson.version} - - - com.fasterxml.jackson.core - jackson-core - ${jackson.version} - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - - org.apache.tomcat - tomcat-servlet-api - 7.0.75 - provided - - - - - - - - - - - - jstl - jstl - 1.2 - - - - taglibs - standard - 1.1.2 - - - - - org.codehaus.jackson - jackson-mapper-asl - 1.8.1 - - - - - - - - - - - - - - - - - org.apache.zookeeper - zookeeper - 3.4.12 - - - - - - junit - junit - 4.8.2 - test - - - - - - com.h2database - h2 - 1.4.197 - - - commons-dbutils - commons-dbutils - 1.7 - - - - com.google.guava - guava - 23.0 - - - - com.mchange - c3p0 - 0.9.5.2 - - - - \ No newline at end of file diff --git a/src/main/java/com/yasenagat/zkweb/ZkWebSpringBootApplication.java b/src/main/java/com/yasenagat/zkweb/ZkWebSpringBootApplication.java new file mode 100644 index 0000000..5a8856f --- /dev/null +++ b/src/main/java/com/yasenagat/zkweb/ZkWebSpringBootApplication.java @@ -0,0 +1,40 @@ +package com.yasenagat.zkweb; +/** + * + */ + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.ServletComponentScan; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; + +/** + * @author Shandy + * + */ +//@SpringBootApplication(exclude = {WebFluxAutoConfiguration.class,ReactorCoreAutoConfiguration.class}) +@SpringBootApplication +@ComponentScan +@ServletComponentScan +//SpringBootServletInitializer for war打包方式 +public class ZkWebSpringBootApplication extends SpringBootServletInitializer{ +// @Bean(name = "sessionProperties") +// @Qualifier(value = "sessionProperties") +// public SessionProperties sessionProperties(){ +// return new SessionProperties(null); +// } +// @Override +// protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { +// return application.sources(SpringBootApplication.class); +// } + //for war打包方式 + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(ZkWebSpringBootApplication.class); + } + public static void main(String[] args) { + SpringApplication.run(ZkWebSpringBootApplication.class, args); + } +} \ No newline at end of file diff --git a/src/main/java/com/yasenagat/zkweb/model/TreeRoot.java b/src/main/java/com/yasenagat/zkweb/model/TreeRoot.java index 587c6df..2481307 100644 --- a/src/main/java/com/yasenagat/zkweb/model/TreeRoot.java +++ b/src/main/java/com/yasenagat/zkweb/model/TreeRoot.java @@ -6,7 +6,7 @@ import java.util.Map; public class TreeRoot extends ArrayList{ - + private static final long serialVersionUID = -8094096973144418349L; Map atr = new HashMap(); private Tree root = new Tree(0,"/",Tree.STATE_CLOSED,null,atr); public TreeRoot() { diff --git a/src/main/java/com/yasenagat/zkweb/util/H2Util.java b/src/main/java/com/yasenagat/zkweb/util/H2Util.java index fc6d817..6ca5920 100644 --- a/src/main/java/com/yasenagat/zkweb/util/H2Util.java +++ b/src/main/java/com/yasenagat/zkweb/util/H2Util.java @@ -8,21 +8,23 @@ import java.util.List; import java.util.Map; -import org.apache.commons.dbutils.ResultSetHandler; +import javax.sql.DataSource; -import com.mchange.v2.c3p0.ComboPooledDataSource; +import org.apache.commons.dbutils.ResultSetHandler; +import org.springframework.beans.factory.annotation.Autowired; public class H2Util { - private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); - - public static ComboPooledDataSource getDataSource(){ + //private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); + @Autowired + private static DataSource dataSource; + public static DataSource getDataSource(){ return dataSource; } public static void destroyDataSource() { - if(dataSource!=null) - dataSource.close(); +// if(dataSource!=null) +// dataSource.close(); } public static ResultSetHandler objectHandler = new ResultSetHandler() { public Object[] handle(ResultSet rs) throws SQLException { diff --git a/src/main/java/com/yasenagat/zkweb/util/ZkCache.java b/src/main/java/com/yasenagat/zkweb/util/ZkCache.java index 141f950..3ec8dd1 100644 --- a/src/main/java/com/yasenagat/zkweb/util/ZkCache.java +++ b/src/main/java/com/yasenagat/zkweb/util/ZkCache.java @@ -4,8 +4,11 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class ZkCache { - + private static final Logger log = LoggerFactory.getLogger(ZkCache.class); private static Map _cache = new ConcurrentHashMap(); public static ZkManager put(String key,ZkManager zk){ @@ -35,8 +38,9 @@ public static void set_cache(Map _cache) { public static void init(ZkCfgManager cfgManager){ List> list = cfgManager.query(); - + log.info("zk info size={}",list.size()); for(Map m : list){ + log.info("zk info: id={},connectstr={},timeout={}",m.get("ID"),m.get("CONNECTSTR"),m.get("SESSIONTIMEOUT")); ZkCache.put(m.get("ID").toString(), ZkManagerImpl.createZk().connect(m.get("CONNECTSTR").toString(), Integer.parseInt(m.get("SESSIONTIMEOUT").toString()))); } } diff --git a/src/main/java/com/yasenagat/zkweb/util/ZkCfgManager.java b/src/main/java/com/yasenagat/zkweb/util/ZkCfgManager.java index 6bc03fd..0b23e9e 100644 --- a/src/main/java/com/yasenagat/zkweb/util/ZkCfgManager.java +++ b/src/main/java/com/yasenagat/zkweb/util/ZkCfgManager.java @@ -5,7 +5,6 @@ public interface ZkCfgManager { - public boolean init(); public boolean add(String desc,String connectStr,String sessionTimeOut); public boolean add(String id,String desc,String connectStr,String sessionTimeOut); public List> query(int page, int rows, String whereSql); diff --git a/src/main/java/com/yasenagat/zkweb/util/ZkCfgManagerImpl.java b/src/main/java/com/yasenagat/zkweb/util/ZkCfgManagerImpl.java index 4f839bc..a0b8ac3 100644 --- a/src/main/java/com/yasenagat/zkweb/util/ZkCfgManagerImpl.java +++ b/src/main/java/com/yasenagat/zkweb/util/ZkCfgManagerImpl.java @@ -11,31 +11,35 @@ import java.util.Map; import java.util.UUID; +import javax.sql.DataSource; + import org.apache.commons.dbutils.QueryRunner; -import org.h2.jdbcx.JdbcConnectionPool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; -import com.yasenagat.zkweb.web.ZkController; - -public class ZkCfgManagerImpl implements ZkCfgManager { +@Component +public class ZkCfgManagerImpl implements InitializingBean,ZkCfgManager { private static Logger log = LoggerFactory.getLogger(ZkCfgManagerImpl.class); // jdbc:h2:tcp://localhost/~/test - - private static JdbcConnectionPool cp = JdbcConnectionPool.create("jdbc:h2:~/zkweb","sa","sa"); + @Autowired + private DataSource dataSource; + //private JdbcConnectionPool cpool = JdbcConnectionPool.create("jdbc:h2:~/zkweb","sa","sa"); // private static JdbcConnectionPool cp = JdbcConnectionPool.create("jdbc:h2:tcp://127.0.0.1/~/zkweb","sa","sa"); private static Connection conn = null; static QueryRunner run = new QueryRunner(H2Util.getDataSource()); public ZkCfgManagerImpl() { - cp.setMaxConnections(20); - cp.setLoginTimeout(1000 * 50); - init(); + //cpool.setMaxConnections(20); + //cpool.setLoginTimeout(1000 * 50); + //init(); }; private Connection getConnection() throws SQLException{ if(null == conn){ - conn = cp.getConnection(); + conn = dataSource.getConnection(); } return conn; } @@ -53,18 +57,33 @@ private void closeConn(){ public void destroyPool() { H2Util.destroyDataSource(); closeConn(); - if(cp!=null) { - cp.dispose(); - } +// if(cpool!=null) { +// cpool.dispose(); +// } + } - public boolean init() { + + private boolean init() { if(isTableOk()) { return true; } + log.error("create table ({})...",ZkCfgManager.initSql); PreparedStatement ps = null; try { ps = getConnection().prepareStatement(ZkCfgManager.initSql); - return ps.executeUpdate()>0; + int ret=ps.executeUpdate(); + if(ret>=0) { + log.error("create table OK !ret={}",ret); + List> result=query(""); + if(result==null) { + log.error("table select check Error!"); + }else { + log.error("table select check OK!"); + } + return true; + } + log.error("create table error !ret={}",ret); + return false; } catch (Exception e) { e.printStackTrace(); log.info("init zkCfg error : {}" , e.getMessage()); @@ -109,10 +128,22 @@ public boolean add(String desc, String connectStr, String sessionTimeOut) { } public List> query() { + List> result=query("where not(desc like 'ignore_%')"); + if(result==null) + return new ArrayList>(); + return result; + } + public List> queryAll() { + List> result=query(""); + if(result==null) + return new ArrayList>(); + return result; + } + private List> query(String whereSql) { PreparedStatement ps = null; ResultSet rs = null; try { - ps = getConnection().prepareStatement("SELECT * FROM ZK where not(desc like 'ignore_%')"); + ps = getConnection().prepareStatement("SELECT * FROM ZK "+whereSql); rs = ps.executeQuery(); List> list = new ArrayList>(); @@ -151,7 +182,7 @@ public List> query() { } } - return new ArrayList>(); + return null; } public boolean update(String id, String desc, String connectStr, @@ -376,7 +407,7 @@ private boolean isTableOk() { try(PreparedStatement psps = getConnection().prepareStatement("drop table ZK")){ psps.execute(); } catch (SQLException e1) { - e1.printStackTrace(); + //e1.printStackTrace(); } return false; } finally { @@ -396,4 +427,12 @@ private boolean isTableOk() { } } } + + //@PostConstruct + @Override + public void afterPropertiesSet() throws Exception { + init(); + ZkCache.init(ZkCfgFactory.createZkCfgManager()); + log.info(" afterPropertiesSet init {} zk instance" , ZkCache.size()); + } } diff --git a/src/main/java/com/yasenagat/zkweb/util/ZkManager.java b/src/main/java/com/yasenagat/zkweb/util/ZkManager.java index 5733e53..813d80c 100644 --- a/src/main/java/com/yasenagat/zkweb/util/ZkManager.java +++ b/src/main/java/com/yasenagat/zkweb/util/ZkManager.java @@ -1,9 +1,7 @@ package com.yasenagat.zkweb.util; -import java.io.IOException; import java.util.List; import java.util.Map; -import java.util.Properties; public interface ZkManager { @@ -18,6 +16,7 @@ public interface ZkManager { public List getChildren(String path); public String getData(String path); + public String getData(String path, boolean isPrintLog); public Map getNodeMeta(String nodePath); @@ -56,7 +55,32 @@ public enum Acl { scheme, id, perms } - - public List getJMXInfo(boolean simpleFlag); + public static class PropertyPanel{ + private String name; + private String value; + private String group; + private String editor="text"; + public String getName() { + return name; + } + public String getValue() { + return value; + } + public String getGroup() { + return group; + } + public String getEditor() { + return editor; + } + public void setInfo(String name,String value,String group) { + this.name = name; + this.value = value; + this.group = group; + } + public void setEditor(String editor) { + this.editor = editor; + } + } + public List getJMXInfo(boolean simpleFlag); } diff --git a/src/main/java/com/yasenagat/zkweb/util/ZkManagerImpl.java b/src/main/java/com/yasenagat/zkweb/util/ZkManagerImpl.java index a7b590b..e4b0484 100644 --- a/src/main/java/com/yasenagat/zkweb/util/ZkManagerImpl.java +++ b/src/main/java/com/yasenagat/zkweb/util/ZkManagerImpl.java @@ -4,27 +4,15 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; -import java.lang.management.ClassLoadingMXBean; -import java.lang.management.CompilationMXBean; -import java.lang.management.ManagementFactory; -import java.lang.management.OperatingSystemMXBean; -import java.lang.management.ThreadMXBean; import java.net.Socket; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.stream.Collectors; - import javax.management.InstanceNotFoundException; import javax.management.IntrospectionException; -import javax.management.JMX; import javax.management.MBeanServerConnection; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; @@ -33,8 +21,6 @@ import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; @@ -43,9 +29,7 @@ import org.apache.zookeeper.ZooDefs.Perms; import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.Stat; -import org.apache.zookeeper.server.ConnectionMXBean; -import org.apache.zookeeper.server.DataTreeMXBean; -import org.apache.zookeeper.server.ZooKeeperServerMXBean; +import org.slf4j.LoggerFactory; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -54,14 +38,13 @@ public class ZkManagerImpl implements Watcher,ZkManager { private ZooKeeper zk=null; - private ZkJMXInfo jmxInfo; private ServerStatusByCMD serverStatusByCMD; private ZkConnectInfo zkConnectInfo=new ZkConnectInfo(); private final String ROOT = "/"; - private static final Log log = LogFactory.getLog(ZkManagerImpl.class); + private static final org.slf4j.Logger log = LoggerFactory.getLogger(ZkManagerImpl.class); // private static final ZkManagerImpl _instance = new ZkManagerImpl(); public ZkManagerImpl(){ - jmxInfo=new ZkJMXInfo(zkConnectInfo); + new ZkJMXInfo(zkConnectInfo); serverStatusByCMD=new ServerStatusByCMD(zkConnectInfo); } @@ -117,37 +100,12 @@ public void setTimeout(int timeout) { } } private interface ZkState{ - List state() throws IOException, MalformedObjectNameException, + List state() throws IOException, MalformedObjectNameException, InstanceNotFoundException, IntrospectionException, ReflectionException; - List simpleState() throws IOException, MalformedObjectNameException, + List simpleState() throws IOException, MalformedObjectNameException, InstanceNotFoundException, IntrospectionException, ReflectionException; }; - public static class PropertyPanel{ - private String name; - private String value; - private String group; - private String editor="text"; - public String getName() { - return name; - } - public String getValue() { - return value; - } - public String getGroup() { - return group; - } - public String getEditor() { - return editor; - } - public void setInfo(String name,String value,String group) { - this.name = name; - this.value = value; - this.group = group; - } - public void setEditor(String editor) { - this.editor = editor; - } - } + public static class ServerStatusByCMD implements ZkState{ private ZkConnectInfo zkConnectInfo; @@ -291,18 +249,18 @@ private List executeOneCmdSimple(Socket sock,String cmd,String gr } } } - public List state() throws IOException, MalformedObjectNameException, + public List state() throws IOException, MalformedObjectNameException, InstanceNotFoundException, IntrospectionException, ReflectionException{ return innerState(false); } - public List simpleState() throws MalformedObjectNameException, InstanceNotFoundException, IntrospectionException, ReflectionException, IOException { + public List simpleState() throws MalformedObjectNameException, InstanceNotFoundException, IntrospectionException, ReflectionException, IOException { return innerState(true); } - public List innerState(boolean simpleFlag) throws IOException, MalformedObjectNameException, + public List innerState(boolean simpleFlag) throws IOException, MalformedObjectNameException, InstanceNotFoundException, IntrospectionException, ReflectionException{ String host; int port; - List retList=new ArrayList<>(); + List retList=new ArrayList<>(); String group; for(ZkHostPort zkHostPort:zkConnectInfo.getConnectInfo()) { host=zkHostPort.getHost(); @@ -342,9 +300,7 @@ public List innerState(boolean simpleFlag) throws IOException, Malforme } public static class ZkJMXInfo { private JMXConnector connectorJMX; - private ZkConnectInfo zkConnectInfo; public ZkJMXInfo(ZkConnectInfo zkConnectInfo) { - this.zkConnectInfo=zkConnectInfo; } /** @@ -546,7 +502,8 @@ public MBeanServerConnection createMBeanServer(String ip, String jmxport, String * - MBean的某个属性名 * @return 属性值 */ - private String getAttribute(MBeanServerConnection mbeanServer, + @SuppressWarnings("unused") + private String getAttribute(MBeanServerConnection mbeanServer, ObjectName objName, String objAttr) { if (mbeanServer == null || objName == null || objAttr == null) throw new IllegalArgumentException(); @@ -603,7 +560,7 @@ private String getAttribute(MBeanServerConnection mbeanServer, // } // }; @Override - public List getJMXInfo(boolean simpleFlag) { + public List getJMXInfo(boolean simpleFlag) { try { if(simpleFlag) return serverStatusByCMD.simpleState(); @@ -657,6 +614,9 @@ public List getChildren(String path){ } public String getData(String path) { + return getData(path,true); + } + public String getData(String path,boolean isPrintLog) { try { Stat s = zk.exists(path, false); if (s != null) { @@ -664,8 +624,9 @@ public String getData(String path) { if(null == b){ return ""; } - log.info("data : "+new String(zk.getData(path, false, s))); - return new String(zk.getData(path, false, s)); + String pathContent=new String(zk.getData(path, false, s)); + if(isPrintLog)log.info("data[{}] : {}",path,pathContent); + return pathContent; } } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/com/yasenagat/zkweb/util/ZkSpringBootConfiguration.java b/src/main/java/com/yasenagat/zkweb/util/ZkSpringBootConfiguration.java new file mode 100644 index 0000000..9560a41 --- /dev/null +++ b/src/main/java/com/yasenagat/zkweb/util/ZkSpringBootConfiguration.java @@ -0,0 +1,140 @@ +package com.yasenagat.zkweb.util; + + +import java.beans.PropertyVetoException; +import java.util.Locale; + +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.core.env.Environment; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; +import org.springframework.web.servlet.i18n.SessionLocaleResolver; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; + +import com.mchange.v2.c3p0.ComboPooledDataSource; + +@Configuration +public class ZkSpringBootConfiguration implements WebMvcConfigurer { + @Autowired + private Environment env; + + @Bean(name = "dataSource") + @Qualifier(value = "dataSource") + @Primary + //@ConfigurationProperties(prefix = "c3p0") + public DataSource getDataSource(){ + //DruidDataSource dataSource = new DruidDataSource(); + ComboPooledDataSource dataSource= org.springframework.boot.jdbc.DataSourceBuilder.create().type(ComboPooledDataSource.class).build(); + dataSource.setJdbcUrl(env.getProperty("spring.datasource.url")); + dataSource.setUser(env.getProperty("spring.datasource.username")); + dataSource.setPassword(env.getProperty("spring.datasource.password")); + try { + dataSource.setDriverClass(env.getProperty("spring.datasource.driver-class-name")); + } catch (PropertyVetoException e) { + e.printStackTrace(); + return null; + } + dataSource.setInitialPoolSize(Integer.parseInt(env.getProperty("spring.datasource.initial-pool-size"))); + dataSource.setMinPoolSize(Integer.parseInt(env.getProperty("spring.datasource.min-pool-size"))); + dataSource.setMaxPoolSize(Integer.parseInt(env.getProperty("spring.datasource.max-pool-size"))); + dataSource.setAcquireIncrement(Integer.parseInt(env.getProperty("spring.datasource.acquire-increment"))); + dataSource.setIdleConnectionTestPeriod(Integer.parseInt(env.getProperty("spring.datasource.idle-connection-test-period"))); + dataSource.setMaxIdleTime(Integer.parseInt(env.getProperty("spring.datasource.max-idle-time"))); + dataSource.setMaxStatements(Integer.parseInt(env.getProperty("spring.datasource.max-statements"))); + dataSource.setAcquireRetryAttempts(Integer.parseInt(env.getProperty("spring.datasource.acquire-retry-attempts"))); + dataSource.setBreakAfterAcquireFailure(Boolean.parseBoolean(env.getProperty("spring.datasource.break-after-acquire-failure"))); + return dataSource; + } + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("/home"); + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/", + "classpath:/META-INF/resources/webapp/resources/");//,"classpath:/resources/webapp/WEB-INF/views/"); + //registry.addResourceHandler("/**"); + } + + @Bean + public SessionLocaleResolver localeResolver() { + SessionLocaleResolver slr = new SessionLocaleResolver(); + // 默认语言 + slr.setDefaultLocale(Locale.CHINA); + return slr; + } + + @Bean + public LocaleChangeInterceptor localeChangeInterceptor() { + LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); + // 参数名 + lci.setParamName("lang"); + return lci; + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(localeChangeInterceptor()); + // 拦截/freemarker后路径 + //registry.addInterceptor(new JoeInterceptor()).addPathPatterns("/freemarker/**"); + // addPathPatterns 用于添加拦截规则 + // excludePathPatterns 用户排除拦截 + //registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/zk","/zkcfg"); + } + +// @Bean +// public MessageSource messageSource() { +// //logger.info("MessageSource"); +// ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); +// messageSource.setBasename("config.messages.messages"); +// +// return messageSource; +// } + + @Bean + public RequestMappingHandlerAdapter requestMappingHandlerAdapter() { + RequestMappingHandlerAdapter requestMappingHandlerAdapter=new RequestMappingHandlerAdapter(); + //spring boot 已经自带 +// StringHttpMessageConverter stringHttpMessageConverter=new StringHttpMessageConverter(); +// stringHttpMessageConverter.setSupportedMediaTypes(Lists.newArrayList(new MediaType("text/html;charset=UTF-8"), +// new MediaType("text/plain;charset=UTF-8"))); +// MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter=new MappingJackson2HttpMessageConverter(); +// mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Lists.newArrayList(new MediaType("application/json;charset=UTF-8"))); +// requestMappingHandlerAdapter.setMessageConverters(Lists.newArrayList(stringHttpMessageConverter, +// mappingJackson2HttpMessageConverter)); + return requestMappingHandlerAdapter; + } + + //相当于 spring.mvc.servlet.load-on-startup=1 +// @Bean +// public static BeanFactoryPostProcessor beanFactoryPostProcessor() { +// return new BeanFactoryPostProcessor() { +// +// @Override +// public void postProcessBeanFactory( +// ConfigurableListableBeanFactory beanFactory) throws BeansException { +// BeanDefinition bean = beanFactory.getBeanDefinition( +// DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME); +// +// bean.getPropertyValues().add("loadOnStartup", 1); +// } +// }; +// } + //用@WebServlet(urlPatterns = "/cache/*")\@ServletComponentScan代替了 +// @Bean(name = "cacheServlet") +// public ServletRegistrationBean ZkCacheServlet(){ +// return new ServletRegistrationBean(new ZkCacheServlet(),"/cache/*"); +// } +} + diff --git a/src/main/java/com/yasenagat/zkweb/web/ZkCacheServlet.java b/src/main/java/com/yasenagat/zkweb/web/ZkCacheServlet.java index 28a71b4..545527f 100644 --- a/src/main/java/com/yasenagat/zkweb/web/ZkCacheServlet.java +++ b/src/main/java/com/yasenagat/zkweb/web/ZkCacheServlet.java @@ -3,19 +3,18 @@ import java.io.IOException; import java.util.Map; import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - import com.yasenagat.zkweb.util.ZkCache; import com.yasenagat.zkweb.util.ZkCfgFactory; import com.yasenagat.zkweb.util.ZkManagerImpl; -@Component +@WebServlet(name = "cacheServlet",urlPatterns = "/cache/*") public class ZkCacheServlet extends HttpServlet { private static final Logger log = LoggerFactory.getLogger(ZkCacheServlet.class); @@ -52,10 +51,6 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) @Override public void init() throws ServletException { - - ZkCache.init(ZkCfgFactory.createZkCfgManager()); - - log.info("init {} zk instance" , ZkCache.size()); super.init(); } diff --git a/src/main/java/com/yasenagat/zkweb/web/ZkCfgController.java b/src/main/java/com/yasenagat/zkweb/web/ZkCfgController.java index 72a1657..653c444 100644 --- a/src/main/java/com/yasenagat/zkweb/web/ZkCfgController.java +++ b/src/main/java/com/yasenagat/zkweb/web/ZkCfgController.java @@ -20,7 +20,7 @@ @Controller @RequestMapping("/zkcfg") -public class ZkCfgController { +public class ZkCfgController{ private static final Logger log = LoggerFactory.getLogger(ZkCfgController.class); diff --git a/src/main/java/com/yasenagat/zkweb/web/ZkController.java b/src/main/java/com/yasenagat/zkweb/web/ZkController.java index cb42963..f8a5bd8 100644 --- a/src/main/java/com/yasenagat/zkweb/web/ZkController.java +++ b/src/main/java/com/yasenagat/zkweb/web/ZkController.java @@ -6,6 +6,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; + +import javax.servlet.http.HttpServletResponse; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.DisposableBean; @@ -20,8 +23,7 @@ import com.yasenagat.zkweb.model.TreeRoot; import com.yasenagat.zkweb.util.ZkCache; import com.yasenagat.zkweb.util.ZkCfgFactory; -import com.yasenagat.zkweb.util.ZkCfgManagerImpl; -import com.yasenagat.zkweb.util.ZkManagerImpl; +import com.yasenagat.zkweb.util.ZkManager.PropertyPanel; @Controller @RequestMapping("/zk") @@ -63,20 +65,24 @@ public String queryzNodeInfo( public @ResponseBody String queryZKOk(Model model,@RequestParam(required=true) String cacheId){ String exmsg="Disconnected Or Exception"; try { - if(ZkCache.get(cacheId).getData("/")!=null) + if(ZkCache.get(cacheId).getData("/",false)!=null) { + log.info("cacheId[{}] : {}",cacheId,"Connected"); return "Connected"; - else + } + else { + log.info("cacheId[{}] : {}",cacheId,"Disconnected Or Exception"); return exmsg; + } } catch (Exception e) { e.printStackTrace(); } return exmsg; } - @RequestMapping(value="/queryZKJMXInfo") - public @ResponseBody List queryZKJMXInfo( + @RequestMapping(value="/queryZKJMXInfo", produces="application/json;charset=UTF-8") + public @ResponseBody List queryZKJMXInfo( @RequestParam(required=true) String simpleFlag, - @RequestParam(required=true) String cacheId + @RequestParam(required=true) String cacheId,HttpServletResponse response ){ try { @@ -84,8 +90,9 @@ public String queryzNodeInfo( // //model.addAttribute("acls", ZkCache.get(cacheId).getACLs(path)); // //model.addAttribute("path",path); // model.addAttribute("cacheId", cacheId); - List result=ZkCache.get(cacheId).getJMXInfo(Integer.parseInt(simpleFlag)==0?false:true); - log.info("simpleFlag={},cacheId={},result : {}",simpleFlag,cacheId,JSON.std.asString(result)); + List result=ZkCache.get(cacheId).getJMXInfo(Integer.parseInt(simpleFlag)==0?false:true); + log.info("queryZKJMXInfo simpleFlag={},cacheId={},result : {}",simpleFlag,cacheId,JSON.std.asString(result)); + response.addHeader("Access-Control-Allow-Origin", "*"); return result; } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 0000000..0f7379e --- /dev/null +++ b/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,52 @@ +{"properties": [ + { + "name": "spring.datasource.initial-pool-size", + "type": "java.lang.Integer", + "description": "A description for 'spring.datasource.initial-pool-size'" + }, + { + "name": "spring.datasource.min-pool-size", + "type": "java.lang.Integer", + "description": "A description for 'spring.datasource.min-pool-size'" + }, + { + "name": "spring.datasource.max-pool-size", + "type": "java.lang.Integer", + "description": "A description for 'spring.datasource.max-pool-size'" + }, + { + "name": "spring.datasource.max-pool-size", + "type": "java.lang.Integer", + "description": "A description for 'spring.datasource.max-pool-size'" + }, + { + "name": "spring.datasource.acquire-increment", + "type": "java.lang.Integer", + "description": "A description for 'spring.datasource.acquire-increment'" + }, + { + "name": "spring.datasource.idle-connection-test-period", + "type": "java.lang.Integer", + "description": "A description for 'spring.datasource.idle-connection-test-period'" + }, + { + "name": "spring.datasource.max-idle-time", + "type": "java.lang.Integer", + "description": "A description for 'spring.datasource.max-idle-time'" + }, + { + "name": "spring.datasource.max-statements", + "type": "java.lang.Integer", + "description": "A description for 'spring.datasource.max-statements'" + }, + { + "name": "spring.datasource.acquire-retry-attempts", + "type": "java.lang.Integer", + "description": "A description for 'spring.datasource.acquire-retry-attempts'" + }, + { + "name": "spring.datasource.break-after-acquire-failure", + "type": "java.lang.Boolean", + "description": "A description for 'spring.datasource.break-after-acquire-failure'" + } +]} \ No newline at end of file diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml new file mode 100644 index 0000000..4873ec4 --- /dev/null +++ b/src/main/resources/application-local.yml @@ -0,0 +1,14 @@ +logging: + config: classpath:logback-zkweb.xml +spring: + h2: + console: + enabled: 'true' + path: /console + settings: + trace: false + web-allow-others: 'true' + jta: + enabled: false + hateoas: + use-hal-as-default-json-media-type: 'false' diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml new file mode 100644 index 0000000..3b0c788 --- /dev/null +++ b/src/main/resources/application.yaml @@ -0,0 +1,63 @@ +# +spring: + http: + encoding: + force: true + thymeleaf: + enabled: false + cache: false + datasource: + platform: h2 + type: com.mchange.v2.c3p0.ComboPooledDataSource + url: jdbc:h2:file:~/.h2/zkweb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;FILE_LOCK=SOCKET + username: sa + password: sa + driver-class-name: org.h2.Driver + initial-pool-size: 10 + min-pool-size: 5 + max-pool-size: 20 + acquire-increment: 3 + idle-connection-test-period: 60 + max-idle-time: 60 + max-statements: 100 + acquire-retry-attempts: 30 + break-after-acquire-failure: false + resources: + cache: + period: 0 +# static-locations: +# - classpath:/META-INF/resources/ +# - classpath:/resources/ +# - classpath:/static/ +# - classpath:/public/ + profiles: + active: local + mvc: + servlet: + load-on-startup: 1 + view: + prefix: /WEB-INF/views/ + suffix: .jsp + session: + store-type: NONE + cache: + type: NONE + #freemarker +# freemarker: +# allow-request-override: false +# cache: true +# check-template-location: true +# charset: UTF-8 +# content-type: text/html +# expose-request-attributes: false +# expose-session-attributes: false +# expose-spring-macro-helpers: false +server: + port: 8099 + servlet: + session: + timeout: 60 + contextPath: / + tomcat: + uri-encoding: UTF-8 + max-threads: 80 diff --git a/src/main/resources/c3p0.properties b/src/main/resources/c3p0.properties deleted file mode 100644 index bea3984..0000000 --- a/src/main/resources/c3p0.properties +++ /dev/null @@ -1,14 +0,0 @@ -#c3p0.jdbcUrl=jdbc:mysql://127.0.0.1/conseb?characterEncoding=utf-8 -#c3p0.driverClass=com.mysql.jdbc.Driver -c3p0.jdbcUrl=jdbc:h2:~/zkweb -c3p0.driverClass=org.h2.Driver -c3p0.user=sa -c3p0.password=sa - -c3p0.acquireIncrement=3 -c3p0.idleConnectionTestPeriod=60 -c3p0.initialPoolSize=10 -c3p0.maxIdleTime=60 -c3p0.maxPoolSize=20 -c3p0.maxStatements=100 -c3p0.minPoolSize=5 \ No newline at end of file diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml deleted file mode 100644 index 27494f3..0000000 --- a/src/main/resources/log4j.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/logback-zkweb.xml b/src/main/resources/logback-zkweb.xml new file mode 100644 index 0000000..adfb4d9 --- /dev/null +++ b/src/main/resources/logback-zkweb.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + true + + ${path.log}/default-%d{yyyy-MM-dd-HH}-%i.log + 30 + 10MB + 10GB + + + [%d{yyyy-MM-dd HH:mm:ss} %-5level %thread %file:%line] %logger{36} --> %msg%n + + + + + + [%d{yyyy-MM-dd HH:mm:ss} %-5level %thread %file:%line] %logger{36} --> %msg%n + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/spring/appServlet/controllers.xml b/src/main/resources/spring/appServlet/controllers-bak.xml similarity index 100% rename from src/main/resources/spring/appServlet/controllers.xml rename to src/main/resources/spring/appServlet/controllers-bak.xml diff --git a/src/main/resources/spring/appServlet/servlet-context.xml b/src/main/resources/spring/appServlet/servlet-context-bak.xml similarity index 100% rename from src/main/resources/spring/appServlet/servlet-context.xml rename to src/main/resources/spring/appServlet/servlet-context-bak.xml diff --git a/src/main/resources/spring/root-context.xml b/src/main/resources/spring/root-context.xml deleted file mode 100644 index c38cdff..0000000 --- a/src/main/resources/spring/root-context.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - diff --git a/src/main/resources/zk.properties b/src/main/resources/zk-bak.properties similarity index 100% rename from src/main/resources/zk.properties rename to src/main/resources/zk-bak.properties diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web-bak.xml similarity index 91% rename from src/main/webapp/WEB-INF/web.xml rename to src/main/webapp/WEB-INF/web-bak.xml index 1cfbdd9..0eb0c37 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web-bak.xml @@ -4,10 +4,10 @@ version="3.0"> - - contextConfigLocation - classpath:spring/root-context.xml - + + + + diff --git a/src/main/webapp/resources/locale/strings_zh_CN.properties b/src/main/webapp/resources/locale/strings_zh.properties similarity index 90% rename from src/main/webapp/resources/locale/strings_zh_CN.properties rename to src/main/webapp/resources/locale/strings_zh.properties index d8b89d8..31cdac7 100644 --- a/src/main/webapp/resources/locale/strings_zh_CN.properties +++ b/src/main/webapp/resources/locale/strings_zh.properties @@ -1,3 +1,4 @@ +#strings_zh\u4f18\u5148\u5339\u914dstrings_zh_CN,\u8be6\u60c5\u89c1jquery.i18n.properties.js\u91cc\u8fb9\u7684shortCode\u548clongCode\u76f8\u5173\u7684fileNames\u5217\u8868 none= north_title=ZK\u8fde\u63a5\u914d\u7f6e west_title=ZK\u8282\u70b9\u6811 diff --git a/src/test/java/T.java b/src/test/java/T.java index 880b1fd..aab9a3b 100644 --- a/src/test/java/T.java +++ b/src/test/java/T.java @@ -159,7 +159,7 @@ public void testZkCfg(){ ZkCfgManager zkCfgManager = ZkCfgFactory.createZkCfgManager(); try { - zkCfgManager.init(); + //zkCfgManager.init(); // for(int i = 0 ; i < 9 ; i ++){ //