Log4j从异常堆栈中获取类、方法、行数演示

好奇Log4j格式化日志时,上述信息获取方式。 跟踪代码,发现了其利用异常实现的,下面是简单例子:

  1. package com.billstudy.test;
  2. import java.lang.reflect.Method;
  3. /**
  4. * Test exception stack infomation analysis
  5. * @author LiBiao - Bill
  6. * @since V1.0 2016年4月15日 - 下午5:48:35
  7. */
  8. @SuppressWarnings({ "unchecked", "rawtypes" })
  9. public class StackDemo {
  10. private static Method getStackTraceMethod;
  11. private static Method getClassNameMethod;
  12. private static Method getMethodNameMethod;
  13. private static Method getFileNameMethod;
  14. private static Method getLineNumberMethod;
  15. static {
  16. Class[] noArgs = null;
  17. try {
  18. getStackTraceMethod = Throwable.class.getMethod("getStackTrace", noArgs);
  19. Class stackTraceElementClass = Class.forName("java.lang.StackTraceElement");
  20. getClassNameMethod = stackTraceElementClass.getMethod("getClassName", noArgs);
  21. getMethodNameMethod = stackTraceElementClass.getMethod("getMethodName", noArgs);
  22. getFileNameMethod = stackTraceElementClass.getMethod("getFileName", noArgs);
  23. getLineNumberMethod = stackTraceElementClass.getMethod("getLineNumber", noArgs);
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. /**
  29. * Obtain relevant information from the exception stack
  30. * @author LiBiao - Bill
  31. * @since V1.0 2016年4月15日 - 下午6:14:33
  32. * @param e
  33. * @param className
  34. */
  35. private static void analysisException(Exception e, String className) {
  36. Object[] noArgs = null;
  37. try {
  38. Object[] elements = (Object[]) getStackTraceMethod.invoke(e,noArgs);
  39. for (int i = 0; i < elements.length; i++) {
  40. Object element = elements[i];
  41. String clazz = (String) getClassNameMethod.invoke(element, noArgs);
  42. // find
  43. if (className.equals(clazz)) {
  44. String method = (String) getMethodNameMethod.invoke(element, noArgs);
  45. int line = (Integer) getLineNumberMethod.invoke(element, noArgs);
  46. String fileName = (String) getFileNameMethod.invoke(element, noArgs);
  47. String result = String.format(
  48. "class:%s \r\n "
  49. + "method:%s \r\n "
  50. + "line:%s \r\n "
  51. + "fileName:%s \r\n errorMsg:%s", clazz,method,line,fileName,e.getMessage());
  52. System.out.println(result);
  53. break;
  54. }
  55. }
  56. } catch (Exception ex) {
  57. ex.printStackTrace();
  58. }
  59. }
  60. public static void main(String[] args) {
  61. String findClassName = "com.billstudy.test.StackDemo$B";
  62. try {
  63. A a = new A();
  64. a.test();
  65. } catch (Exception e) {
  66. //e.printStackTrace();
  67. analysisException(e, findClassName);
  68. }
  69. // a.nullException();
  70. }
  71. public static class A {
  72. private B b = new B();
  73. public void test() {
  74. b.test();
  75. }
  76. public void nullException() {
  77. String target = null;
  78. target.intern();
  79. }
  80. }
  81. public static class B {
  82. private C c = new C();
  83. public void test() {
  84. c.test();
  85. }
  86. }
  87. public static class C {
  88. public void test() {
  89. throw new RuntimeException("别问我为啥,就是想抛个异常 ~");
  90. }
  91. }
  92. }

(更多…)

测试服务器- Zookeeper安装手记

1.解压zookeeper-3.4.6.tar.gz到重命名到/opt/bigdata/zookeeper下 

2.创建文件夹
  1. #进入到Zookeeper安装目录执行
  2. mkdir data
  3. mkdir log
3.进入到conf中复制并修改zoo.cfg文件
  1. # The number of milliseconds of each tick
  2. tickTime=2000
  3. # The number of ticks that the initial
  4. # synchronization phase can take
  5. initLimit=10
  6. # The number of ticks that can pass between
  7. # sending a request and getting an acknowledgement
  8. syncLimit=5
  9. # the directory where the snapshot is stored.
  10. # do not use /tmp for storage, /tmp here is just
  11. # example sakes.
  12. dataDir=/opt/bigdata/zookeeper/data
  13. dataLogDir=/opt/bigdata/zookeeper/log
  14. # the port at which the clients will connect
  15. clientPort=2181
  16. # the maximum number of client connections.
  17. # increase this if you need to handle more clients
  18. #maxClientCnxns=60
  19. #
  20. # Be sure to read the maintenance section of the
  21. # administrator guide before turning on autopurge.
  22. #
  23. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  24. #
  25. # The number of snapshots to retain in dataDir
  26. #autopurge.snapRetainCount=3
  27. # Purge task interval in hours
  28. # Set to "0" to disable auto purge feature
  29. #autopurge.purgeInterval=1
  30. server.1=m1:2888:3888
  31. server.2=m2:2888:3888
  32. server.3=m3:2888:3888
4.添加zk节点的myid,在data文件夹中
  1. #这个id 在别的机器上需要修改,根据zoo.cfg中声明的填写
  2. echo 1 > /opt/bigdata/zookeeper/conf/myid
5.分发整个文件夹到别的节点
  1. cd /opt/bigdata/
  2. #下面这个是我写的批量scp脚本
  3. #语法是:
  4. #Usage: /usr/bin/scp_many filename targetname host[s]
  5. #e.g: /usr/bin/scp_many love.txt /home/love.txt v2,v3,v4
  6. scp_many ./zookeeper/ $PWD m2,m3
6.执行zkServer.sh start然后在status看状态就好了。
批量scp脚本:
  1. #!/bin/bash
  2. # description: scp file to many hosts
  3. filename=$1
  4. targetname=$2
  5. hosts=$3
  6. if [ $# -lt 3 ];then
  7. echo -e "Usage:\033[45;37m $0 filename targetname host[s] \033[0m"
  8. echo -e "e.g:\033[45;37m $0 love.txt /home/love.txt v2,v3,v4\033[0m"
  9. exit 1
  10. fi
  11. #for host in $(echo $3|awk -F,)
  12. for host in ${hosts//,/ }
  13. do
  14. cmd="scp -r $filename $host:$targetname"
  15. echo -e "$\033[46;37m$cmd\033[0m"
  16. $cmd
  17. done

sqoop 和mysql相关操作

数据迁移工具,可以和RDBMS相互迁移数据

需要先将db driver copy to sqoop lib dir
注意:
    sqoop是以mapreduce的方式来运行任务的,用hdfs来存储数据的。所以依赖NameNode和ResourceManager,只要机器上配置了这两个就可以正常运行,程序运行时会自动读取环境变量. (更多…)

mysql常用解决方案

1.删除了mysql数据库user表记录,会导致用户不能正常登录。

此时将mysqld 以skip-grant-tables启动,忽略权限。
进入到mysql库中,执行以下脚本。
就可以用root/bill登录了
  1. /*
  2. Navicat MySQL Data Transfer
  3. Source Server : localhost
  4. Source Server Version : 50703
  5. Source Host : localhost:3306
  6. Source Database : mysql
  7. Target Server Type : MYSQL
  8. Target Server Version : 50703
  9. File Encoding : 65001
  10. Date: 2015-06-13 20:07:50
  11. */
  12. SET FOREIGN_KEY_CHECKS=0;
  13. -- ----------------------------
  14. -- Table structure for `user`
  15. -- ----------------------------
  16. DROP TABLE IF EXISTS `user`;
  17. CREATE TABLE `user` (
  18. `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
  19. `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
  20. `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
  21. `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  22. `Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  23. `Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  24. `Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  25. `Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  26. `Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  27. `Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  28. `Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  29. `Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  30. `File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  31. `Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  32. `References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  33. `Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  34. `Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  35. `Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  36. `Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  37. `Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  38. `Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  39. `Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  40. `Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  41. `Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  42. `Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  43. `Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  44. `Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  45. `Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  46. `Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  47. `Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  48. `Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  49. `Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  50. `ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '',
  51. `ssl_cipher` blob NOT NULL,
  52. `x509_issuer` blob NOT NULL,
  53. `x509_subject` blob NOT NULL,
  54. `max_questions` int(11) unsigned NOT NULL DEFAULT '0',
  55. `max_updates` int(11) unsigned NOT NULL DEFAULT '0',
  56. `max_connections` int(11) unsigned NOT NULL DEFAULT '0',
  57. `max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',
  58. `plugin` char(64) COLLATE utf8_bin NOT NULL DEFAULT 'mysql_native_password',
  59. `authentication_string` text COLLATE utf8_bin,
  60. `password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
  61. PRIMARY KEY (`Host`,`User`)
  62. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges';
  63. -- ----------------------------
  64. -- Records of user
  65. -- ----------------------------
  66. INSERT INTO `user` VALUES ('%', 'root', '*29A1BB43D3B9EB42028B4566E4836353285B9395', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', '0', '0', '0', '0', 'mysql_native_password', '', 'N');
  67. INSERT INTO `user` VALUES ('127.0.0.1', 'root', '*29A1BB43D3B9EB42028B4566E4836353285B9395', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', '0', '0', '0', '0', 'mysql_native_password', '', 'N');
  68. INSERT INTO `user` VALUES ('::1', 'root', '*29A1BB43D3B9EB42028B4566E4836353285B9395', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', '0', '0', '0', '0', 'mysql_native_password', '', 'N');
  69. INSERT INTO `user` VALUES ('localhost', '', '', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', '', '', '', '', '0', '0', '0', '0', 'mysql_native_password', null, 'N');

(更多…)