log4j平滑升级log4j2

log4j升级

  • log4j在异步日志打印下性能极大提升,好处多多
  1. log4j2可以选择异步与同步日志打印.使用异步打印必须配置disruptor包

    1
    2
    3
    4
    5
               <dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.2</version>
    </dependency>
  2. log4j2的xml配置中有AsyncLogger和Logger配置区别。Async就是异步配置

  3. log4j2有包架构不匹配。当原有系统不方便需改时。可以使用桥接包

    1
    2
    3
    4
    5
    <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.13.0</version>
    </dependency>
  4. 参考其他文章

xcodebuild找不到

xcode找不到

1
2
3
1. 直接appStore上下载不会有问题
2. 自己额外下载会有可能变更安装目录,造成找不到xcodebuild
3. 解决原因:sudo xcode-select -s (/Applications/Xcode.app/Contents/Developer|你的安装地址 )

jstack定位死循环代码

定位正在运行的程序死循环处(linux处使用)

查看java的pid

  1. ps -ef | grep java 或者使用 top
    找到你的java程序的进程id, 定位 pid

  2. top -Hp $pid
    查看耗cpu时间最多的几个线程, 记录下线程的id.注:这里的进程是10进制的。

  3. jstack $pid > stack.log 生成dump文件。将上面线程的id换成16进制,加上0x$id 。查找dump出来的文件。就是该线程的堆栈信息

  4. 统计线程数量 ps -hH $pid| wc -l

    cpu运行

    top 按1键看每个数据,z 彩色 c是, f是

  5. us是用户的,运行一些数据会飙升

  6. sy系统调度资源

  7. ni 运行已调整优先级的用户进程的CPU时间

  8. id 空闲的,

  9. wa 等待

  10. hi 处理硬件中断的CPU时间

  11. si 处理软件中断的CPU时间

场景1:java启动100个cpu密集型任务,在24核上处理,假设平均的分在每一核心上执行, us或者sy很高,us执行,sy调度。id和wa很低

查看程序占用端口

  1. linux : netstat -naop
  2. windows: netstat -ano |findstr 62045

mongo操作

mongodb使用总结

【grom】非内嵌类使用many-to-one or one-to-one等问题

1
若非内嵌类,则不能使用类似belongsTo = [xx:XX] 或者 hasOne = [xx:XX] 或者 XX xx等的关联方式,需要指定使用String xxId

【grom】非内嵌类使用one-to-many等问题

1
2
若非内嵌类,则不能使用类似List<XX> xxs的关联方式,否则虽不会报错,
但是却不能正常保存(保存的都为null);然而可以使用hasMany = [xxs: XX]的方式

【grom】数组查询

1
2
3
4
5
6
7
8
9
10
11
12
13
假设Cart中有List<String> strIds;则可以针对数组进行各式查询:
* 精确查询 -- (包括数组的顺序) -- 查询出strIds为["a0","a1","a2"]的数据
def cartList = Cart.createCriteria().list {
eq("strIds", ["a0","a1","a2"])
}
* 模糊查询 -- 数组包含即可 -- 查询出所有strIds中包含a8的数据
def cartList = Cart.createCriteria().list {
eq("strIds", "a8")
}
* 下标查询 -- 查询出所有strIds中下标8的值为a8的数据
def cartList = Cart.createCriteria().list {
eq("strIds.8", "a8")
}

mongo CRUD

导出

1
2
导出使用mongodump命令。mongodump --help查看命令的帮助。可以批量导出数据库表
* [地址](http://docs.mongodb.org/manual/reference/program/mongodump/)

导入

1
导入备份 mongorestore命令。该命令可以导入一个文件夹下所有集合。mongoimport只能导入单个json

添加用户

1
2
3
4
5
6
7
8
use test
db.createUser(
{
user:"user123",
pwd:"12345678",
roles:[ "readWrite", { role:"changeOwnPasswordCustomDataRole", db:"admin" } ]
}
)
* [地址](http://docs.mongodb.org/manual/tutorial/change-own-password-and-custom-data/)

修改用户

1
2
3
4
5
6
7
8
use test
db.updateUser(
"user123",
{
pwd: "KNlZmiaNUp0B",
customData: { title: "Senior Manager" }
}
)
  1. db.getCollection(‘order’).find({“logistics”:{ $in: [null],$exists:true}}) // 数值为null 字段也在
  2. mongo客户端 robomongo
  3. 建立唯一索引db.getCollection(‘xxx’).ensureIndex({id:1},{unique:true})

mongodb常用命令

mongodb常用操作

  1. 添加用户名

    1
    2
    3
    4
    5
    db.createUser( {
    user: "test",
    pwd: "test",
    roles: [ { role: "dbOwner", db: "test" } ]
    });
  2. 删除数据库

    1
    2
    db.dropDatabase()
    这将删除选定的数据库。如果还没有选择任何数据库,然后它会删除默认的 ' test' 数据库
  3. 删除用户

    1
    db.system.users.remove({user:"java1"});
  4. 克隆数据库

    1
    db.copyDatabase(fromdb, todb, fromhost, username, password)  // username是fromdb的数据库名
  5. 更新数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    1).update()命令

    db.collection.update( criteria, objNew, upsert, multi )
    db.collection.update( {id:"11"}, {$set:{id:"22"}}, {multi:true} ) // 将所有id=11的更新成id=22
    criteria : update的查询条件,类似sql update查询内where后面的
    objNew : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
    upsert : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
    multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

    db.test0.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 只更新了第一条记录
    db.test0.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 全更新了
  6. 管道概念,参考官方网址

    1
    2
    3
    4
    5
    6
    7
    查询一个条件后进行一个条件
    db.oldUser.aggregate({$project:{address:1,gender: 1}},{$skip:1},{$limit:1});
    上面语句意思是,返回数据只包含address和gender字段,略过第一条数据,只返回一条数据。如果limit和skip反过来,则是返回一条数据,同时略过,则返回为空。管道按照顺序来
    db.oldUser.aggregate({$match:{ $or:[ {origin:"淘宝",$or:[{address:"上海"},{_id:"111"},{address:"杭州"}]},{origin:"淘宝",_id:"111"}] } });
    // 查询字段 (来自淘宝同时地址要是上海或者id是xxx或者是杭州)或者(来自淘宝同时id是xxx)
    db.oldUser.aggregate({$match:{ $or:[{origin:"淘宝"]}},{$match:{ $or:[{address:"天猫"]}});
    // and查询,既要是来自淘宝,同时地址还是天猫的
  7. 查询字段为空

1
2
3
4
5
6
db.getCollection('maodoudou').find({scroe:{$in:[null], $exists: true}}) // 查询该字段存在,值为null
db.getCollection('maodoudou').find({scroe:{$in:[null], $exists: false}}) // 查询该字段值为null,不管字段是否存不存在,没有字段也视为null
db.getCollection('adc').find({$or:[{_id:"110000"},{_id:"110101"}]}) // mongodb or查询
db.getCollection('adc').find({$and:[{_id:"110000"},{name:"北京市"}]}) // and查询

db.hotelBillsOrder.aggregate({$unwind: "$orderItems"},{$group:{_id:null,count:{$sum:"$orderItems.day"}}})

mongodb的安装/启动/集群

mongodb的安装

  1. 复制mongo.conf 进行配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    # mongod.conf

    # for documentation of all options, see:
    # http://docs.mongodb.org/manual/reference/configuration-options/
    net:
    port: 27010
    bindIp: 127.0.0.1

    processManagement:
    pidFilePath: /data0/mongod/mongod1.pid
    fork: true

    storage:
    dbPath: /data0/mongod/data/shard1
    directoryPerDB: true

    systemLog:
    destination: file
    path : /data0/mongod/log/shard1/mongodb.log
    logAppend: true

    replication:
    replSetName: rs0
    oplogSizeMB: 1000
    # security:
    # authorization: enabled

    单机不需要replication参数
  2. 运行 我的mongodb

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    bin/mongod -f xxx/mongod.conf 启动服务
    bin/mongod -shutdown -f xxx/mongod.conf 停止服务
    mongod 密码验证,刚开始。注释上面security:
    进入mongodb /use admin库
    创建超级用户db.createUser({
    user: "admin",
    pwd: "admin",
    roles:
    [{
    role: "userAdminAnyDatabase",
    db: "admin"
    }]})
    在开启上面配置文件的 security。这时候mongodb将使用密码访问
  3. mongodb开机启动

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    [Unit]
    Description=MongoDB Server
    After=network.target

    [Service]
    User=mongod
    Group=mongod
    Type=forking

    PIDFile=/data0/mongod/mongod1.pid
    ExecStartPre=
    ExecStart=/usr/local/mongodb1/bin/mongod -f /usr/local/mongodb1/mongod.conf
    ExecReload=/usr/local/mongodb1/bin/kill -s HUP $MAINPID
    ExecStop=/usr/local/mongodb1/bin/kill -s QUIT $MAINPID
    WorkingDirectory=/data0/mongod
    Restart=always

    LimitFSIZE=infinity
    LimitCPU=infinity
    LimitAS=infinity
    LimitNOFILE=64000
    LimitRSS=infinity
    LimitNPROC=64000

    PrivateTmp=true

    [Install]
    WantedBy=multi-user.target

mongodb集群服务

  1. mongodb配置一组relica sets。 仲裁节点(偶数台需要,奇数台不需要,作用是偶数台电脑,照成投票平均,仲裁节点可以增加票数)

  2. 注:请先登陆到admin数据库在进行操作,否则后续操作会出错 (重点)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    ntpdate ntp.api.bz  集群时,首先执行命令 同步时钟(重要)
    mongod1.conf。可以多复制几个,使用一个mongod同时运行。
    mongo -port xx // 链接到其中一台mongodb服务器上去
    configx = {_id:"rs0",version:1,members:[{_id:0,host:'127.0.0.1:27010',priority :2},{_id:1,host:'127.0.0.1:27020',priority:1},{_id:2,host:'127.0.0.1:27030',priority:1}]} // 添加各个节点上去
    rs.initiate(configx) // 初始化一个 replica sets
    rs.conf() // 查看获取的配置
    rs.reconfig(configx,{force:true}) // reconfig() 用来重新执行文档,force:true 是否覆盖以前
    rs.status() // 查看状态。
    备注:从库只读 需要 rs.slaveOk();可以设置为只读 不可以写
  3. mongod sharding configServer配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    configServer的配置和mongod1.conf要多加如下面一句话。只需要dbpath和logpath换下就行

    **sharding:**
    **clusterRole: configsvr**

    configServer也需要dbpath

    另外#replication:replSetName:xxx 尽量别和replica set中的重名。否则mongos添加shard时通过Id会有可能找成configServer。非一台电脑可以不换,只需要不冲突就行,

    configx = {_id:"configrs0",configsvr: true,members:[{_id:0,host:'127.0.0.1:27010',priority :2},{_id:1,host:'127.0.0.1:27020',priority:1},{_id:2,host:'127.0.0.1:27030',priority:1}]} // 添加各个节点上去
  4. mongod sharding mongos路由的配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    net:
    port: 20000
    bindIp: test13.kingsilk.xyz

    processManagement:
    pidFilePath: /data0/mongod/mongos.pid
    #fork: true

    systemLog:
    destination: file
    path : /data0/mongod/log/mongos/mongodb.log
    logAppend: true

    sharding:
    configDB: xxxxxxxx:27000,xxxxxx:28000,xxxxxxx:29000
    mongos.conf 配置文件
    启动:mongos -f mongos.conf
  5. 集群后的配置

    1
    2
    键入:mongo -host xx -port xx   mongos的地址
    sh.addShard("rs0/xxx:27010,txx:27020,xxx27030"); // 刚才输入的一组replica set。如果rs0和configServer中的replicationName一样。则可能会添加失败.修改Id 别冲突

mongodb密码登录 一些快速创建密码账户,可以略过不用看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

db.createUser(
{
user: "admin",
pwd: "admin",
roles:
[
{
role: "userAdminAnyDatabase",
db: "admin"
}
]
}
)
db.createUser(
{
user: "repl",
pwd: "replication",
roles: [
{ role: "readWrite", db: "admin" }
]
}
)

db.createUser(
{
user:"root",
pwd:"root",
roles:["root"]
}
)

集群的密码配置(先看完上面的)

  1. linux 命令行:openssl rand -base64 741 > mongodb-keyfile

  2. linux 命令行: chmod 600 mongodb-keyfile

  3. https://docs.mongodb.org/manual/tutorial/configure-ssl/ mongos认证

  4. mongos.conf的配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    net:
    port: 20000
    bindIp: 127.0.0.1

    processManagement:
    pidFilePath: /data0/mongod/mongos.pid
    #fork: true

    #storage:
    # dbPath: /data0/mongod/data/mongos
    # directoryPerDB: true

    systemLog:
    destination: file
    path : /data0/mongod/log/mongos/mongodb.log
    logAppend: true

    sharding:
    configDB: configServer1:27000,configServer2:28000,configServer3:29000
    #replication:
    # replSetName: rs0
    # oplogSizeMB: 1000
    security:
    # authorization: enabled
    keyFile: mongodb-keyfile
    安全需要先注释,进入admin添加用户后在打开,和mongodb一样,这里不重复了
    运行:mongos -f mongos.conf
  5. 将所有的mongod.conf和config.conf 添加上

    1
    2
    3
    security:
    authorization: enabled
    keyFile: mongodb-keyfile
  6. mongos.conf只需要添加keyFile,不需要authorization。添加这个之前现在mongos无密码登陆创建用户先

  7. 进入mongos到admin

    1
    2
    3
    4
    5
    6
    sh.addShard("rs0/xxxxxxxx:27010,xxxxxxxxxxx:27030,xxxxxxxxxxxxxx:27030");
    需要在admin中添加。副本集也要在admin中添加,否则无法添加
    #指定uba分片生效
    db.runCommand( { enablesharding :"uba"});
    #指定数据库里需要分片的集合和片键
    db.runCommand( { shardcollection : "uba.table1",key : {id: 1} } )

mysql知识点(B/B+数据结构)

mysql知识点

先放出友情链接

事务的说明探讨

参考:1

mysql知识点

  1. mysql事务实现:MVCC概念

  2. 组合索引:innodb 索引会带上当前的索引,同时加上主键Id,order 也会用索引

  3. mysql order by 工作过程:文章1 | 文章2

    1
    2
    3
    4
    5
    上诉排序问题:sql为:select city,name,age from t where city in ('杭州','苏州') order by name limit 1000;这时候怎么办?
    1: 组合索引的排序规则是city_name 这时候city=杭州 但是name排序不对。
    2: 业务上分别拆分成2条:select city,name,age from t where city='杭州' order by name limit 1000;
    and select city,name,age from t where city='苏州' order by name limit 1000; 然后再业务代码中进行name排序取出前1000.
    也是一种方法
  4. mysql explan 中type含义: 文章

  5. B树 B+数区别 :文章

    1
    2
    3
    4
    5
    6
    7
    8
    B树和B+树的区别

    这都是由于B+树和B具有这不同的存储结构所造成的区别,以一个m阶树为例。
    1. 关键字的数量不同;B+树中分支结点有m个关键字,其叶子结点也有m个,其关键字只是起到了一个索引的作用,但是B树虽然也有m个子结点,但是其只拥有m-1个关键字。
    2. 存储的位置不同;B+树中的数据都存储在叶子结点上,也就是其所有叶子结点的数据组合起来就是完整的数据,但是B树的数据存储在每一个结点中,并不仅仅存储在叶子结点上。
    3. 分支结点的构造不同;B+树的分支结点仅仅存储着关键字信息和儿子的指针(这里的指针指的是磁盘块的偏移量),也就是说内部结点仅仅包含着索引信息。
    4. 查询不同;B树在找到具体的数值以后,则结束,而B+树则需要通过索引找到叶子结点中的数据才结束,也就是说B+树的搜索过程中走了一条从根结点到叶子结点的路径。

mvn私服配置

maven私服配置

  1. 下载nexus直接运行bin目录 ./nexus start启动;
  2. 浏览器输入http://localhost:8081/nexus
  3. maven需要配置。在~/.m2/setting.xml,添加一下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
</pluginGroups>

<proxies>
</proxies>

<servers>
</servers>

<mirrors>
<mirror>
<mirrorOf>*</mirrorOf>
<name>kingsilk</name>
<url>http://mvn.kingsilk.xyz/content/groups/public/</url>
<id>kingsilk</id>
</mirror>
</mirrors>

<profiles>
<profile>
<id>downloadSources</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>

<activeProfiles>
<activeProfile>downloadSources</activeProfile>
</activeProfiles>
</settings>

1.nexus下载地址选择对应的包

clean install -Dmaven.test.skip=true

nginx上传图片错误和代理resolver错误

nginx日常使用

  1. 使用

在上传图片时候,使用电脑浏览器未有错误,手机客户端浏览器会抛错。nginx缓存大小

1
2
3
4
astcgi_buffers 32 8k;
client_body_buffer_size 1024k;
client_max_body_size 10m;
nginx http.conf http中

nginx proxy_pass

  1. 问题:no resolver defined to resolve xxx.xxx

    • 原因是Nginx0.6.18以后的版本中启用了一个resolver指令,在使用变量来构造某个server地址的时候一定要用resolver指令来制定DNS服务器的地址。在nginx的配置文件中的http{}部分添加一行resolver 8.8.8.8;即可
  2. 引申问题 由于resolver加入 8.8.8.8那么域名都会解析到广域网中的ip。 如果需要将域名解析至特定的ip。可以自己搭建一个dns服务器。

  • windows中搭建方法: 自行搜索,暂无看到好的解决方案。windows server可以支持
  • linux 中搭建方法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    brew install  dnsmasq
    vim /usr/local/etc/dnsmasq.conf 该路径下没有在etc中
    vim /etc/dnsmasq.conf

    >>把以下注释打开
    domain-needed
    bogus-priv
    cache-size=51200
    listen-address=127.0.0.1
    resolv-file=/etc/resolv.conf

    >> 启动
    /etc/init.d/dnsmasq start 注:默认nobady用户。提示无权限修改上面conf中usre配置。填写对应用户

    >> 在nginx配置中增加
    location ~ /testa {
    resolver 127.0.0.1; // 本句
    proxy_pass http://beta.test.com; #apache环境
    }

nodejs简介

参考

安装

二进制安装

打开 nodejs 官网的下载页, 下载二进制安装包

1
2
3
4
5
6
7
8
9
10
sudo mkdir /usr/local/nodejs
sudo tar zxvf node-v0.12.1-linux-x64.tar.gz -C /usr/local/nodejs

sudo vi /etc/profile.d/xxx.sh # 追加以下配置
export NODEJS_HOME=/usr/local/nodejs/node-v0.12.1-linux-x64
export PATH=$NODEJS_HOME/bin:$PATH

cd /usr/local/nodejs/node-v0.12.1-linux-x64
sudo chmod 777 bin
sudo chmod 777 lib/node_modules

Ubuntu

参考这里

1
2
3
4
5
6
curl -sL https://deb.nodesource.com/setup | sudo bash -
#sudo add-apt-repository ppa:chris-lea/node.js
#sudo apt-get update
apt-cache policy nodejs
sudo apt-get install nodejs
sudo apt-get install build-essential

Http Hello world

新建 hi.js,内容如下

1
2
3
4
5
6
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

然后运行:

1
node hi.js

最后浏览器访问 http://127.0.0.1:1337/

Centos

使用Linux二进制包。

使用 nvm

1
2
3
4
su - 
curl -sL https://deb.nodesource.com/setup | sudo bash -
su -
nvm install v0.10.34

npm

使用国内淘宝的镜像

  • 通过 config 命令

    1
    2
    npm config set registry https://registry.npm.taobao.org
    npm info underscore
  • 通过命令行参数

    1
    npm --registry https://registry.npm.taobao.org info underscore
  • 通过修改 ~/.npmrc 加入以下内容

    1
    registry = https://registry.npm.taobao.org