标签:apt ast correct actor watch zookeeper framework baidu root
dataDir=/home/zookeeper_application/zookeeper-3.4.10/data
clientPort=2181
initLimit=10
syncLimit=5
tickTime=2000
server.1=master:2888:3888
server.2=slave1:2888:3888创建zk.properties
zk.server=192.168.2.111:2181
zk.authentication_type=digest
zk.correct_authentication=rightKey
zk.bad_authentication=worngKey
ZkConfig.java
package org.seckill.zk.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @author mxn
* @create 2018-07-16 17:22
*/
public class ZkConfig {
public static final Logger logger = LoggerFactory.getLogger(ZkConfig.class);
public static final String FILE_NAME = "zk.properties";
private String server;
private String authenticationType;
private String correctAuthentication;
private String badAuthentication;
/**
* 配置文件中的常量.
*/
public static final String zk_server = "zk.server";
public static final String zk_authentication_type = "zk.authentication_type";
public static final String zk_correct_authentication = "zk.correct_authentication";
public static final String zk_bad_authentication = "zk.bad_authentication";
/**
* 操作对象.
*/
private static ZkConfig config = new ZkConfig();
private ZkConfig() {
super();
}
private Properties properties;
/**
* 获取config对象.
*
* @return
*/
public static ZkConfig getConfig() {
return config;
}
public static void main(String[] args) {
System.out.println(1);
}
static {
config.loadPropertiesFromSrc();
}
public void loadPropertiesFromSrc() {
InputStream in = null;
try {
in = ZkConfig.class.getClassLoader().getResourceAsStream(FILE_NAME);
if (null != in) {
properties = new Properties();
try {
properties.load(in);
loadProperties(properties);
} catch (IOException e) {
throw e;
}
} else {
logger.error(FILE_NAME + "属性文件未能在classpath指定的目录下 " + ZkConfig.class.getClassLoader().getResource("").getPath() + " 找到!");
}
} catch (Exception e) {
logger.error("ZkConfig.getZkConfig error:", e);
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
}
/**
* 根据传入的 {@link # load(Properties)}对象设置配置参数
*
* @param pro
*/
public void loadProperties(Properties pro) {
logger.info("开始从属性文件中加载配置项");
String value = null;
value = pro.getProperty(zk_server);
if (!ZkUtil.isEmpty(value)) {
this.server = value.trim();
logger.info("配置项:服务器ip==>" + zk_server + "==>" + value + " 已加载");
}
value = pro.getProperty(zk_authentication_type);
if (!ZkUtil.isEmpty(value)) {
this.authenticationType = value.trim();
logger.info("配置项:验证类型==>" + zk_authentication_type + " 已加载");
}
value = pro.getProperty(zk_correct_authentication);
if (!ZkUtil.isEmpty(value)) {
this.correctAuthentication = value.trim();
logger.info("配置项:正确的密码==>" + zk_correct_authentication + " 已加载");
}
value = pro.getProperty(zk_bad_authentication);
if (!ZkUtil.isEmpty(value)) {
this.badAuthentication = value.trim();
logger.info("配置项:错误的密码==>" + zk_bad_authentication + " 已加载");
}
}
public static Logger getLogger() {
return logger;
}
public static String getFileName() {
return FILE_NAME;
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
public String getAuthenticationType() {
return authenticationType;
}
public void setAuthenticationType(String authenticationType) {
this.authenticationType = authenticationType;
}
public String getCorrectAuthentication() {
return correctAuthentication;
}
public void setCorrectAuthentication(String correctAuthentication) {
this.correctAuthentication = correctAuthentication;
}
public String getBadAuthentication() {
return badAuthentication;
}
public void setBadAuthentication(String badAuthentication) {
this.badAuthentication = badAuthentication;
}
public static String getZk_server() {
return zk_server;
}
public static String getZk_authentication_type() {
return zk_authentication_type;
}
public static String getZk_correct_authentication() {
return zk_correct_authentication;
}
public static String getZk_bad_authentication() {
return zk_bad_authentication;
}
public static void setConfig(ZkConfig config) {
ZkConfig.config = config;
}
}
ZkUtil.java 一个工具类
package org.seckill.zk.util;
/**
* @author 12084
* @create 2018-07-17 9:09
*/
public class ZkUtil {
/**
* 判断字符串是否为NULL或空
*
* @param s
* 待判断的字符串数据
* @return 判断结果 true-是 false-否
*/
public static boolean isEmpty(String s) {
return null == s || "".equals(s.trim());
}
}
ZkAuth.java
package org.seckill.zk;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.seckill.zk.util.ZkConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author 12084
* @create 2018-07-16 15:59
*/
public class ZkAuth implements Watcher {
public static final Logger logger = LoggerFactory.getLogger(ZkAuth.class);
final static String PATH = "/auth_test";
final static String PATH_DEL = "/auth_test/del_node";
static ZooKeeper zk = null;
AtomicInteger seq = new AtomicInteger();
public void process(WatchedEvent event) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (event == null) {
return;
}
//连接状态
Event.KeeperState keeperState = event.getState();
//事件类型
Event.EventType eventType = event.getType();
String logPrefix = "【Watcher-" + seq.incrementAndGet() + "】";
logger.info(logPrefix + "收到Watcher通知");
logger.info(logPrefix + "连接状态:\t" + keeperState.toString());
logger.info(logPrefix + "事件类型:\t" + eventType.toString());
if (Event.KeeperState.SyncConnected == keeperState) {
//成功连接上ZK服务器
if (Event.EventType.None == eventType) {
logger.info(logPrefix + "成功连接上ZK服务器");
}
} else if (Event.KeeperState.Disconnected == keeperState) {
logger.info(logPrefix + "与ZK服务器断开连接");
} else if (Event.KeeperState.AuthFailed == keeperState) {
logger.info(logPrefix + "权限检查失败");
} else if (Event.KeeperState.Expired == keeperState) {
logger.info(logPrefix + "会话失效");
}
logger.info("--------------------------------------------");
}
/**
* 创建ZK连接
*
* @param connectString ZK服务器地址列表
* @param sessionTimeout Session超时时间
*/
public void createConnection(String connectString, int sessionTimeout) {
releaseConnection();
try {
zk = new ZooKeeper(connectString, sessionTimeout, this);
//授权
zk.addAuthInfo(ZkConfig.getConfig().getAuthenticationType(), ZkConfig.getConfig().getCorrectAuthentication().getBytes());
logger.info("开始连接ZK服务器....");
while (zk.getState() != ZooKeeper.States.CONNECTED) {
Thread.sleep(3000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 关闭ZK连接
*/
public void releaseConnection() {
if (zk != null) {
try {
zk.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 获取数据:不采用密码
*/
static void getDataByNoAuthentication() {
String prefix = "[不使用任何授权信息]";
logger.info(prefix + "获取数据:" + PATH);
try {
ZooKeeper noZk = new ZooKeeper(ZkConfig.getConfig().getServer(), 2000, null);
Thread.sleep(4000);
logger.info(prefix + "成功获取数据:" + noZk.getData(PATH, false, null));
} catch (Exception e) {
logger.error(prefix + "获取数据失败,原因:" + e.getMessage());
}
}
/**
* 获取数据:采用错误的密码
*/
static void getDataByBadAuthentication() {
String prefix = "[使用错误的授权信息]";
try {
ZooKeeper badzk = new ZooKeeper(ZkConfig.getConfig().getServer(), 2000, null);
//授权
Thread.sleep(4000);
badzk.addAuthInfo(ZkConfig.getConfig().getAuthenticationType(), ZkConfig.getConfig().getBadAuthentication().getBytes());
logger.info(prefix + "获取数据:" + PATH);
logger.info(prefix + "成功获取数据:" + badzk.getData(PATH, false, null));
} catch (Exception e) {
logger.error(prefix + "获取数据失败,原因:" + e.getMessage());
}
}
/**
* 采用正确的密码
*/
static void getDataByCorrectAuthentication() {
String prefix = "[使用正确的授权信息]";
try {
logger.info(prefix + "获取数据:" + PATH);
logger.warn(prefix + "成功获取数据:" + new String(zk.getData(PATH, false, null)));
} catch (Exception e) {
logger.error(prefix + "获取数据失败,原因:" + e.getMessage());
}
}
/**
* 更新数据:不采用密码
*/
static void updateDataByNoAuthentication() {
String prefix = "[不使用任何授权信息]";
logger.info(prefix + "更新数据:" + PATH);
try {
ZooKeeper nozk = new ZooKeeper(ZkConfig.getConfig().getServer(), 2000, null);
Thread.sleep(4000);
Stat stat = nozk.exists(PATH, false);
if (stat != null) {
nozk.setData(PATH, prefix.getBytes(), -1);
logger.info(prefix + "更新成功");
}
} catch (Exception e) {
logger.error(prefix + "更新失败,原因是:" + e.getMessage());
}
}
/**
* 更新数据:采用错误的密码
*/
static void updateDataByBadAuthentication() {
String prefix = "[使用错误的授权信息]";
logger.info(prefix + "更新数据:" + PATH);
try {
ZooKeeper badzk = new ZooKeeper(ZkConfig.getConfig().getServer(), 2000, null);
//授权
badzk.addAuthInfo(ZkConfig.getConfig().getAuthenticationType(), ZkConfig.getConfig().getCorrectAuthentication().getBytes());
Stat exists = badzk.exists(PATH, false);
if (exists != null) {
badzk.setData(PATH, prefix.getBytes(), -1);
logger.info(prefix + "更新成功");
}
} catch (Exception e) {
logger.error(prefix + "更新失败,原因是:" + e.getMessage());
}
}
/**
* 更新数据:采用正确的密码
*/
static void updateDataByCorrectAuthentication() {
String prefix = "[使用正确的授权信息]";
logger.info(prefix + "更新数据:" + PATH);
try {
Stat stat = zk.exists(PATH, false);
if (stat != null) {
zk.setData(PATH, prefix.getBytes(), -1);
logger.info(prefix + "更新成功");
}
} catch (Exception e) {
logger.error(prefix + "更新失败,原因是:" + e.getMessage());
}
}
static void deleteParent() throws Exception {
Stat stat = zk.exists(PATH_DEL, false);
if (stat != null) {
zk.delete(PATH_DEL, -1);
zk.delete(PATH, -1);
return;
}
Stat stat2 = zk.exists(PATH, false);
if (stat2 != null) {
zk.delete(PATH, -1);
}
}
public static void main(String[] args) {
ZkAuth zkAuth = null;
try {
zkAuth = new ZkAuth();
zkAuth.createConnection(ZkConfig.getConfig().getServer(), 5000);
deleteParent();
zk.create(PATH, "init content".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
// zk.create(PATH_DEL, "will be deleted!".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
logger.info("客户端开始访问-----------------------------------------------");
//获取数据
getDataByNoAuthentication();
getDataByBadAuthentication();
getDataByCorrectAuthentication();
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放连接
zkAuth.releaseConnection();
}
}
}
Connected to the target VM, address: ‘127.0.0.1:52280‘, transport: ‘socket‘
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/Maven/repo/ch/qos/logback/logback-classic/1.1.1/logback-classic-1.1.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/Maven/repo/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
13:56:20,355 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:56:20,356 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:56:20,370 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/workspace/maven/seckill/target/classes/logback.xml]
13:56:20,527 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
13:56:20,531 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
13:56:20,559 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:56:20,617 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
13:56:20,617 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
13:56:20,618 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
13:56:20,620 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@17c386de - Registering current configuration as safe fallback point
13:56:20.626 [main] INFO org.seckill.zk.util.ZkConfig - 开始从属性文件中加载配置项
13:56:20.634 [main] INFO org.seckill.zk.util.ZkConfig - 配置项:服务器ip==>zk.server==>192.168.2.111:2181 已加载
13:56:20.634 [main] INFO org.seckill.zk.util.ZkConfig - 配置项:验证类型==>zk.authentication_type 已加载
13:56:20.634 [main] INFO org.seckill.zk.util.ZkConfig - 配置项:正确的密码==>zk.correct_authentication 已加载
13:56:20.634 [main] INFO org.seckill.zk.util.ZkConfig - 配置项:错误的密码==>zk.bad_authentication 已加载
13:56:20.645 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
13:56:20.645 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:host.name=windows10.microdone.cn
13:56:20.645 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.version=1.8.0_11
13:56:20.646 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.vendor=Oracle Corporation
13:56:20.646 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.home=C:\Program Files\Java\jdk1.8.0_11\jre
ar;D:\Maven\repo\org\slf4j\slf4j-api\1.7.12\slf4j-api-1.7.12.jar;D:\Maven\repo\ch\qos\logback\logback-core\1.1.1\logback-core-1.1.1.jar;D:\Maven\repo\ch\qos\logback\logback-classic\1.1.1\logback-classic-1.1.1.jar;D:\Maven\repo\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar;D:\Maven\repo\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar;D:\Maven\repo\org\mybatis\mybatis\3.3.0\mybatis-3.3.0.jar;D:\Maven\repo\org\mybatis\mybatis-spring\1.2.3\mybatis-spring-1.2.3.jar;D:\Maven\repo\taglibs\standard\1.1.2\standard-1.1.2.jar;D:\Maven\repo\jstl\jstl\1.2\jstl-1.2.jar;D:\Maven\repo\com\fasterxml\jackson\core\jackson-databind\2.5.4\jackson-databind-2.5.4.jar;D:\Maven\repo\com\fasterxml\jackson\core\jackson-annotations\2.5.0\jackson-annotations-2.5.0.jar;D:\Maven\repo\com\fasterxml\jackson\core\jackson-core\2.5.4\jackson-core-2.5.4.jar;D:\Maven\repo\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;D:\Maven\repo\org\springframework\spring-core\4.1.7.RELEASE\spring-core-4.1.7.RELEASE.jar;D:\Maven\repo\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\Maven\repo\org\springframework\spring-beans\4.1.7.RELEASE\spring-beans-4.1.7.RELEASE.jar;D:\Maven\repo\org\springframework\spring-context\4.1.7.RELEASE\spring-context-4.1.7.RELEASE.jar;D:\Maven\repo\org\springframework\spring-aop\4.1.7.RELEASE\spring-aop-4.1.7.RELEASE.jar;D:\Maven\repo\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\Maven\repo\org\springframework\spring-expression\4.1.7.RELEASE\spring-expression-4.1.7.RELEASE.jar;D:\Maven\repo\org\springframework\spring-jdbc\4.1.7.RELEASE\spring-jdbc-4.1.7.RELEASE.jar;D:\Maven\repo\org\springframework\spring-tx\4.1.7.RELEASE\spring-tx-4.1.7.RELEASE.jar;D:\Maven\repo\org\springframework\spring-web\4.1.7.RELEASE\spring-web-4.1.7.RELEASE.jar;D:\Maven\repo\org\springframework\spring-webmvc\4.1.7.RELEASE\spring-webmvc-4.1.7.RELEASE.jar;D:\Maven\repo\org\springframework\spring-test\4.1.7.RELEASE\spring-test-4.1.7.RELEASE.jar;D:\Maven\repo\redis\clients\jedis\2.7.3\jedis-2.7.3.jar;D:\Maven\repo\org\apache\commons\commons-pool2\2.3\commons-pool2-2.3.jar;D:\Maven\repo\com\dyuproject\protostuff\protostuff-core\1.0.8\protostuff-core-1.0.8.jar;D:\Maven\repo\com\dyuproject\protostuff\protostuff-api\1.0.8\protostuff-api-1.0.8.jar;D:\Maven\repo\com\dyuproject\protostuff\protostuff-runtime\1.0.8\protostuff-runtime-1.0.8.jar;D:\Maven\repo\com\dyuproject\protostuff\protostuff-collectionschema\1.0.8\protostuff-collectionschema-1.0.8.jar;D:\Maven\repo\commons-collections\commons-collections\3.2\commons-collections-3.2.jar;D:\Maven\repo\org\apache\zookeeper\zookeeper\3.4.6\zookeeper-3.4.6.jar;D:\Maven\repo\org\slf4j\slf4j-log4j12\1.6.1\slf4j-log4j12-1.6.1.jar;D:\Maven\repo\log4j\log4j\1.2.16\log4j-1.2.16.jar;D:\Maven\repo\io\netty\netty\3.7.0.Final\netty-3.7.0.Final.jar;D:\Maven\repo\jline\jline\0.9.94\jline-0.9.94.jar;D:\install\Idea\IntelliJ IDEA 2018.1.1\lib\idea_rt.jar;C:\Users\12084\AppData\Local\Temp\capture1jars\debugger-agent.jar
13:56:20.648 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.library.path=C:\Program Files\Java\jdk1.8.0_11\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Program Files\TortoiseSVN\bin;C:\Program Files\SlikSvn\bin;D:\Maven\apache-maven-3.3.9\bin;D:\install\MySQL\MySQL Server 5.1\bin;C:\Program Files\Java\jdk1.8.0_11\bin;D:\Git\Git\cmd;C:\WINDOWS\System32\OpenSSH;C:\Users\12084\AppData\Local\Microsoft\WindowsApps;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;;.
13:56:20.648 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.io.tmpdir=C:\Users\12084\AppData\Local\Temp
13:56:20.648 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.compiler=
Process finished with exit code 0
13:56:20.634 [main] INFO org.seckill.zk.util.ZkConfig - 配置项:服务器ip==>zk.server==>192.168.2.111:2181 已加载
13:56:20.634 [main] INFO org.seckill.zk.util.ZkConfig - 配置项:验证类型==>zk.authentication_type 已加载
13:56:20.634 [main] INFO org.seckill.zk.util.ZkConfig - 配置项:正确的密码==>zk.correct_authentication 已加载
13:56:20.634 [main] INFO org.seckill.zk.util.ZkConfig - 配置项:错误的密码==>zk.bad_authentication 已加载
13:56:20.680 [main] INFO org.seckill.zk.ZkAuth - 开始连接ZK服务器....
13:56:20.910 [main-EventThread] INFO org.seckill.zk.ZkAuth - 【Watcher-1】收到Watcher通知
13:56:20.911 [main-EventThread] INFO org.seckill.zk.ZkAuth - 【Watcher-1】连接状态: SyncConnected
13:56:20.911 [main-EventThread] INFO org.seckill.zk.ZkAuth - 【Watcher-1】事件类型: None
13:56:20.911 [main-EventThread] INFO org.seckill.zk.ZkAuth - 【Watcher-1】成功连接上ZK服务器
13:56:23.714 [main] INFO org.seckill.zk.ZkAuth - 客户端开始访问-----------------------------------------------
13:56:23.715 [main] INFO org.seckill.zk.ZkAuth - [不使用任何授权信息]获取数据:/auth_test
13:56:27.734 [main] ERROR org.seckill.zk.ZkAuth - [不使用任何授权信息]获取数据失败,原因:KeeperErrorCode = NoAuth for /auth_test
13:56:31.738 [main] ERROR org.seckill.zk.ZkAuth - [使用错误的授权信息]获取数据失败,原因:KeeperErrorCode = NoAuth for /auth_test
13:56:31.754 [main] WARN org.seckill.zk.ZkAuth - [使用正确的授权信息]成功获取数据:init content
标签:apt ast correct actor watch zookeeper framework baidu root
原文地址:https://www.cnblogs.com/menxn/p/9323064.html