在linux下安装很简单:
pecl install swoole
用这种方式可以自动下载安装。
我采取的是第二种方式噢。
安装完成,需要更改php.ini的配置,将
extension=swoole.so
放在这个配置中,然后重启nginx。完成以后执行
php -m
查看是否有swoole。有swoole说明安装成功。
新建一个server.php
<?php
class Server
{
private $serv;
public function __construct() {
$this->serv = new swoole_server("127.0.0.1", 9501);
$this->serv->set(array(
'worker_num' => 1, //一般设置为服务器CPU数的1-4倍
// 'daemonize' => 1, //以守护进程执行
'max_request' => 10000,
'dispatch_mode' => 2,
'task_worker_num' => 8, //task进程的数量
"task_ipc_mode " => 3 , //使用消息队列通信,并设置为争抢模式
"package_eof" => PHP_EOL, // 设置EOF
"log_file" => "log/taskqueueu.log" ,//日志
));
$this->serv->on('Receive', array($this, 'onReceive'));
// bind callback
$this->serv->on('Task', array($this, 'onTask'));
$this->serv->on('Finish', array($this, 'onFinish'));
$this->serv->start();
}
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
echo "Get Message From Client {$fd}:{$data}n";
// send a task to task worker.
$serv->send($fd, "Server: ".$data);
$serv->task( $data );
}
public function onTask($serv,$task_id,$from_id, $data) {
$array = json_decode( $data , true );
file_put_contents("test.txt", "onTask......\r\n", FILE_APPEND);
file_put_contents("test.txt", $data."\r\n", FILE_APPEND);
sleep(6);
file_put_contents("test.txt", "onTask after sleep......\r\n", FILE_APPEND);
if ($array['url']) {
return $this->httpGet( $array['url'] , $array['param'] );
}
}
public function onFinish($serv,$task_id, $data) {
//echo "Task {$task_id} finishn";
$temp = json_encode($data);
file_put_contents("test.txt", "onFinish.{$temp}.....\r\n", FILE_APPEND);
echo "Task {$task_id} finish\n";
echo "Result: {$data}\n";
//echo "Result: {$data}n";
}
protected function httpGet($url,$data){
if ($data) {
$url .='?'.http_build_query($data) ;
}
$curlObj = curl_init(); //初始化curl,
curl_setopt($curlObj, CURLOPT_URL, $url); //设置网址
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); //将curl_exec的结果返回
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curlObj, CURLOPT_HEADER, 0); //是否输出返回头信息
$response = curl_exec($curlObj); //执行
curl_close($curlObj); //关闭会话
return $response;
}
}
$server = new Server();
再弄一个cleint.php
<?php
class Client
{
private $client;
public function __construct() {
$this->client = new swoole_client(SWOOLE_SOCK_TCP);
}
public function connect() {
if( !$this->client->connect("127.0.0.1", 9501 , 1) ) {
echo "Connect Error";
}
$data = array(
"url" => "http://test.dev/a.php" , //这个是在服务器去请求的
"param" => array(
"username"=>'test',
"password" => 'test'
)
);
$json_data = json_encode($data);
$this->client->send( $json_data );
}
public function ret()
{
return $this->client->recv();
}
}
echo 'this'.PHP_EOL;
$client = new Client();
$client->connect();
//从服务器接收数据
$data = $client->ret();
if (!$data)
{
die("recv failed.");
}
var_dump($data);
echo 'dfdfdfd';
可以在test.dev下访问到的a.php
<?php
/**
* @Author: Administrator
* @Date: 2017-04-17 14:07:44
* @Last Modified by: Administrator
* @Last Modified time: 2017-04-20 15:33:44
*/
json_encode($_REQUEST);
file_put_contents("test.txt", "This is another something.".json_encode($_REQUEST), FILE_APPEND);
echo json_encode( ['test' => 'test-hello']);
然后启动: php server.php
在浏览器访问 client.php
就可以看到效果啦。
搜索
标签
study
ab
amap
apache
apahe
awk
aws
bat
centos
CFS
chrome
cmd
cnpm
composer
consul
crontab
css
curl
cygwin
devops
di
docker
docker,docker-compose
ethereum
excel
fiddler
fluentd
framework
front-end
git
gitgui
github
glide
go
golang
gorm
grafana
gzip
ioc
item2
iterm2
javascript
jenkins
jsonp
kafka
laradock
laravel
larval
linux
liunux
log
mac
mac, wi-fi
macos
magento
mariaDB
minikube
mongoDB
msp
mysql
netbeans
nginx
nodejs
nohup
npm
nsq
oracle
php
php-fpm
php7
phpstorm
php扩展
Protobuf
python
redis
scp
server
shell
soap
socket
socket5
sql
sre
ssdb
ssh
ssl
study
sublime
swift
system
td-agent
uml
v2ray
vagrant
vagrnat
vim
vpn
vue
vue.js
webpack
webrtc
websocket
webtatic
windows
windows7
word
wps
xdebug
yarn
yii2
yum
zookeeper
世界国家
互联网
以太坊
分类
前端
小程序
打印机
排序算法
搞笑
权限
粤语
缓存
网络
虚拟机
视频
设计模式
项目管理
热门文章
友情链接