use yii\httpclient\Client;
$client = new Client();
$requests = [
$client->get('http://domain.com/keep-alive'),
$client->post('http://domain.com/notify', ['userId' => 12]),
];
$responses = $client->batchSend($requests);
可使用特定的 传输方式 提升其性能。 在内置传输中,只有 yii\httpclient\CurlTransport 允许并行发送请求,从而减少程序执行时间。use yii\httpclient\Client;
$client = new Client();
$requests = [
'news' => $client->get('http://domain.com/news'),
'friends' => $client->get('http://domain.com/user/friends', ['userId' => 12]),
'newComment' => $client->post('http://domain.com/user/comments', ['userId' => 12, 'content' => 'New comment']),
];
$responses = $client->batchSend($requests);
// result of `GET http://domain.com/news` :
if ($responses['news']->isOk) {
echo $responses['news']->content;
}
// result of `GET http://domain.com/user/friends` :
if ($responses['friends']->isOk) {
echo $responses['friends']->content;
}
// result of `POST http://domain.com/user/comments` :
if ($responses['newComment']->isOk) {
echo "Comment has been added successfully";
}
use yii\httpclient\Client;
$client = new Client();
$response = $client->createRequest()
->setMethod('POST')
->setUrl('http://domain.com/api/1.0/users')
->setData(['name' => 'John Doe', 'email' => 'johndoe@domain.com'])
->setOptions([
'proxy' => 'tcp://proxy.example.com:5100', // use a Proxy
'timeout' => 5, // set timeout to 5 seconds for the case server is not responding
])
->send();
提示:可以通过 yii\httpclient\Client::$requestConfig 对默认请求进行设置。 可以使用 yii\httpclient\Request::addOptions() 在添加其他设置的同时保留原始的其他参数。use yii\httpclient\Client;
$client = new Client([
'transport' => 'yii\httpclient\CurlTransport' // only cURL supports the options we need
]);
$response = $client->createRequest()
->setMethod('POST')
->setUrl('http://domain.com/api/1.0/users')
->setData(['name' => 'John Doe', 'email' => 'johndoe@domain.com'])
->setOptions([
CURLOPT_CONNECTTIMEOUT => 5, // connection timeout
CURLOPT_TIMEOUT => 10, // data receiving timeout
])
->send();
有关特定参数支持的详细信息,请参阅特定的传输类文档。<?php
//要访问的目标页面
$page_url = "http://dev.kdlapi.com/testproxy";
//代理ip,由快代理提供
$proxy = "47.115.5.19";
$prod = 16816;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//设置代理
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYPORT, $prod);
//自定义header
$headers = array();
$headers[] = 'User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//自定义cookie
curl_setopt($ch, CURLOPT_COOKIE,'');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //使用gzip压缩传输数据让访问更快
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $result;
echo "\n\nfetch ".$info['url']."\ntimeuse: ".$info['total_time']."s\n\n";
?>
运行下php curl.php
yum install supervisor
Debian/Ubuntu可通过apt安装apt-get install supervisor
pip安装pip install supervisor
easy_install安装easy_install supervisor
三、supervisor使用[unix_http_server]
file=/tmp/supervisor.sock ;UNIX socket 文件,supervisorctl 会使用
;chmod=0700 ;socket文件的mode,默认是0700
;chown=nobody:nogroup ;socket文件的owner,格式:uid:gid
;[inet_http_server] ;HTTP服务器,提供web管理界面
;port=127.0.0.1:9001 ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
;username=user ;登录管理后台的用户名
;password=123 ;登录管理后台的密码
[supervisord]
logfile=/tmp/supervisord.log ;日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB ;日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小
logfile_backups=10 ;日志文件保留备份数量默认10,设为0表示不备份
loglevel=info ;日志级别,默认info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ;pid 文件
nodaemon=false ;是否在前台启动,默认是false,即以 daemon 的方式启动
minfds=1024 ;可以打开的文件描述符的最小值,默认 1024
minprocs=200 ;可以打开的进程数的最小值,默认 200
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ;通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
;serverurl=http://127.0.0.1:9001 ; 通过HTTP的方式连接supervisord
; [program:xx]是被管理的进程配置参数,xx是进程的名称
[program:xx]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run ; 程序启动命令
autostart=true ; 在supervisord启动的时候也自动启动
startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3 ; 启动失败自动重试次数,默认是3
user=tomcat ; 用哪个用户启动进程,默认是root
priority=999 ; 进程启动优先级,默认999,值小的优先启动
redirect_stderr=true ; 把stderr重定向到stdout,默认false
stdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默认50MB
stdout_logfile_backups = 20 ; stdout 日志文件备份数,默认是10
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup=false ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false ;默认为false,向进程组发送kill信号,包括子进程
;包含其它配置文件
[include]
files = relative/directory/*.ini ;可以指定一个或多个以.ini结束的配置文件
子进程配置文件说明:#项目名
[program:blog]
#脚本目录
directory=/opt/bin
#脚本执行命令
command=/usr/bin/python /opt/bin/test.py
#supervisor启动的时候是否随着同时启动,默认True
autostart=true
#当程序exit的时候,这个program不会自动重启,默认unexpected,设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。如果为false的时候,无论什么情况下,都不会被重新启动,如果为unexpected,只有当进程的退出码不在下面的exitcodes里面定义的
autorestart=false
#这个选项是子进程启动多少秒之后,此时状态如果是running,则我们认为启动成功了。默认值为1
startsecs=1
#脚本运行的用户身份
user = test
#日志输出
stderr_logfile=/tmp/blog_stderr.log
stdout_logfile=/tmp/blog_stdout.log
#把stderr重定向到stdout,默认 false
redirect_stderr = true
#stdout日志文件大小,默认 50MB
stdout_logfile_maxbytes = 20MB
#stdout日志文件备份数
stdout_logfile_backups = 20
子进程配置示例:#说明同上
[program:test]
directory=/opt/bin
command=/opt/bin/test
autostart=true
autorestart=false
stderr_logfile=/tmp/test_stderr.log
stdout_logfile=/tmp/test_stdout.log
#user = test
五、supervisor命令说明supervisorctl status //查看所有进程的状态
supervisorctl stop es //停止es
supervisorctl start es //启动es
supervisorctl restart //重启es
supervisorctl update //配置文件修改后使用该命令加载新的配置
supervisorctl reload //重新启动配置中的所有程序
注:把es换成all可以管理配置中的所有进程。直接输入supervisorctl进入supervisorctl的shell交互界面,此时上面的命令不带supervisorctl可直接使用。systemctl start supervisord.service //启动supervisor并加载默认配置文件
systemctl enable supervisord.service //将supervisor加入开机启动项
常见问题[root@test-huanqiu ~]# visudo
.....
# User privilege specification
pang ALL=(ALL) ALL
[root@test-huanqiu ~]# visudo
.......
# User privilege specification
pang localhost=(root) /bin/chown,/bin/chmod
#这里的NOPASSWD就是配置支持免密执行的字段
pang ALL=(root) NOPASSWD:/usr/bin/vim
# 切换至root用户
sudo su - root
# 修改/etc/sudoers文件权限
chmod 744 /etc/sudoers
# 修改/etc/sudoers文件,添加如下内容,ubuntu为当前用户
ubuntu ALL=(ALL:ALL) NOPASSWD:ALL
# 保存,并恢复权限
chmod 440 /etc/sudoers
# 重启系统
reboot
新启动一个terminal, 测试没有生效。# User privilege specification
root ALL=(ALL:ALL) ALL
pang ALL=(ALL:ALL) NOPASSWD:ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
pang ALL=(ALL:ALL) NOPASSWD:ALL
[root@test-huanqiu ~]# visudo
.....
# Allow members of group sudo to execute any command
# (Note that later entries override this, so you might need to move it further down)
%sudo ALL=(ALL) ALL
和授权给单个用户类似,只不过将用户名在这里换成%组名,所有在该组中的用户都按照此规则进行授权。[pang@localhost ~]$ cat test
cat: test: Permission denied
[pang@localhost ~]$ sudo !!
sudo cat test
lala
xiixixi'
nihao
[pang@localhost ~]$ sudo cd /sys/kernel/debug/
sudo: cd: command not found
[pang@localhost ~]$ sudo bash //或者sudo su - 或者 sudo -s,其实就使用sudo命令从当前用户切换到root用户
[root@localhost debug]# cd /sys/kernel/debug/
[root@localhost debug]#
4.3 sudo操作记入日志[pang@localhost ~]$ sudo touch /var/log/sudo.log
修改/etc/rsyslog.conf配置文件(有些版本系统的这个文件名为/etc/syslog.conf),在该文件加入下面一行[pang@localhost ~]$ sudo vim /etc/rsyslog.conf
.........
local2.debug /var/log/sudo.log //空白不能用空格,必须用tab
[pang@localhost ~]$ sudo vim /etc/sudoers
........
Defaults logfile=/var/log/sudo.log
重启 syslog 服务[pang@localhost ~]$ sudo service rsyslog restart
验证groupadd sudo #因为没有指定gid,所以就按着文件中的gid往后推的
sudo:x:504: #添加完之后在该文件中就出现了这条字段。
sudo:x:504:batman,test #此时,用户batman和test的附属组就是sudo了
此时再visudo%sudo ALL=(root) NOPASSWD:/usr/bin/vim
%sudo ALL=(root) NOPASSWD:/bin/cat
-rw-------. 1 root root 1595 Jul 24 05:00 rsyncd.conf
-rw-------. 1 root root 8 Jul 24 05:00 rsyncd.pass
-rw-------. 1 root root 14 Jul 24 05:00 rsyncd.pwd
[pang@localhost]$ sudo vim rsyncd.conf
发现也是ok的。(1) 重启电脑,进入ubuntu 恢复模式Grub(一直按esc或shift)
(2) 在Grub的菜单中选择root
(3) 重新输入命令 chown root /etc/sudoers
(4) reboot
command sudo visudo
2、在文件末尾,增加如下内容www-data ALL=NOPASSWD: /sbin/iptables, /usr/bin/du
假设你希望使用超级用户(root)权限运行iptables和du命令。但是,如果你想使用超级用户权限运行每一个应用,添加以下命令来代替上面的命令。 www-data ALL=NOPASSWD: ALL
3、就是这样,现在像下面这种方式在你的php脚本中使用exec()函数redis-cli -h ip -p 6401 -a password cluster info<br>
2.集群节点信息redis-cli -h ip -p 6401 -a password cluster nodes<br>
3.节点内存、cpu、key数量等信息(每个节点都需查看)redis-cli -h ip -p 6401 -a password info<br>
4.从redis集群中查看key(只需要连接master节点查看)redis-cli -h ip -p 6401 -a password keys *<br>
b.查看key的value值redis-cli -h ip -p 6401 -a password -c get key<br>
c.查看key值得有效期redis-cli -h ip -p 6401 -a password -c ttl key
ssh root@ip
Password: password
keys *
查看存储的数据剩余过期时间
ttl key<br>
查看key的类型(string,set,list,hash)
type key<br>
查看key的值(string类型)
get key<br>
查看key的值(set类型)
scard key //获取 set 集合中元素的数量
smembers key //查看 set 中的内容<br>
查看key的值(hash类型)
hlen key //获取 key 键的字段数量
hgetall key //返回 key 键的所有字段及其值
hkeys key //获取 key 键中所有字段的名字
hvals key //获取 key 键中所有字段的值
nohup Command [ Arg ... ] [ & ]
参数说明:nohup /root/runoob.sh &
在终端如果看到以下输出说明运行成功:appending output to nohup.out
这时我们打开 root 目录 可以看到生成了 nohup.out 文件。ps -aux | grep "runoob.sh"
参数说明:kill -9 进程号PID
以下命令在后台执行 root 目录下的 runoob.sh 脚本,并重定向输入到 runoob.log 文件:nohup /root/runoob.sh > runoob.log 2>&1 &
2>&1 解释:0 – stdin (standard input,标准输入)
1 – stdout (standard output,标准输出)
2 – stderr (standard error,标准错误输出)
yum install -y epel-release
yum install -y supervisor
systemctl enable supervisord # 开机自启动
systemctl start supervisord # 启动supervisord服务
# systemctl status supervisord # 查看supervisord服务状态
supervisord
运行supervisor时会启动一个进程supervisord,它负责启动所管理的进程,并将所管理的进程作为自己的子进程来启动,而且可以在所管理的进程出现崩溃时自动重启systemctl restart supervisord
supervisorctl (命令管理工具)
可以用来执行start、stop、restart等命令,来对这些子进程进行管理supervisorctl start hyperf
# 重启 hyperf 应用supervisorctl restart hyperf
# 停止 hyperf 应用supervisorctl stop hyperf
# 查看所有被管理项目运行状态supervisorctl status
# 重新加载配置文件supervisorctl update
# 重新启动所有程序supervisorctl reload
网页管理vim /etc/supervisor.conf
[inet_http_server]
port=127.0.0.1:9001
;username=test
;password=123456
systemctl start supervisord
# sudo /usr/bin/supervisord -n -c /etc/supervisord.conf
打开浏览器,输入地址127.0.0.1:9001help
help <action>
shutdown
重载配置文件并重启supervisord和programreload
读取当前运行配置和program配置文件的差异reread
重载配置文件并重启受影响的programupdate
重启program(不会重载配置文件)restart <name>
restart <name> <name>
restart all
clear <name>
clear <name> <name>
clear all
fg <name>
显示supervisord或program IDpid
pid <name>
pid all
start <name>
start <name> <name>
start all
停止programstop <name>
stop <name> <name>
stop all
status <name>
status <name> <name>
status
tail [-f] <name> [stdout|stderr] (default stdout)
输出supervisord最新日志(Ctrl+C退出)maintail [-f]