Giter Site home page Giter Site logo

workerman_api's Introduction

API

High performance api service

单服开10+Worker,轻松支撑C100K请求

Windows下安装PHP7

下载最新PHP7.x http://windows.php.net/download/
解压到任意目录,比如F:\php
将PHP的安装路径F:\php加入PATH 环境变量
进入PHP安装目录(例如 F:\php)。找到php.ini-development文件并复制一份到当前目录,重命名为php.ini
用编辑器打开 php.ini 文件,修改以下配置:
去掉extension_dir = "ext"前面的分号(738 行左右)
去掉extension=php_curl.dll前面的分号(893 行左右)
去掉extension=php_mbstring.dll前面的分号(903 行左右)
去掉extension=php_openssl.dll前面的分号(907 行左右)
去掉extension=php_pdo_mysql.dll前面的分号(909 行左右)

Mac下安装PHP7

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
brew untap homebrew/php
brew uninstall php
brew upgrade
brew clenaup
brew prune
brew install autoconf
brew install php

CentOS7下部署

sudo yum -y install epel-release
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
sudo yum -y install git
sudo yum install php71w php71w-devel php71w-cli php71w-common php71w-gd php71w-ldap php71w-mbstring php71w-mcrypt php71w-mysql php71w-pdo php71w-fpm php71w-pecl-redis -y

cd ~
sudo yum install gcc openssl openssl-devel libevent libevent-devel
sudo pecl install event-2.3.0
sudo vi /etc/php.d/sockets.ini
extension=sockets.so
extension=event.so

sudo yum install redis
sudo systemctl enable redis
sudo systemctl start redis

安装Workerman的pcntl和posix扩展、event或者libevent扩展:http://doc.workerman.net/315116

内核参数调优:http://doc.workerman.net/315302

示例代码

/////////////////////////////////// http
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/lib/app_http.php';
require_once __DIR__ . '/config.php';
ini_set("precision", "-1");
ini_set("serialize_precision", "-1");

$app = new App("http://0.0.0.0:8888");
$app->count = 8;
$app->name = 'api';

$app->onWorkerStart = function ($worker) {
	require_once __DIR__ . '/lib/auth.php';
	require_once __DIR__ . '/lib/db.php';
	require_once __DIR__ . '/lib/clickhouse.php';
	require_once __DIR__ . '/lib/cache.php';
	require_once __DIR__ . '/lib/kafka.php';
	require_once __DIR__ . '/config.php';
	ini_set("precision", "-1");
	ini_set("serialize_precision", "-1");

	echo "[" . date('Y-m-d H:i:s') . "] Worker start[" . $worker->id . "]\n";
};

///////////////////////////////// API接口定义开始

$app->get('/', function ($req) {
	return 'OK';
});

$app->post('/', function ($req) {
	return 'OK';
});

$app->get('/health/check', function ($req) {
	return 'OK';
});

$app->before('/api', function ($req) {
	return Auth::verify_api($req);
});

$app->get('/tables', function ($req) {
	if (CACHE::exists('tables')) {
		return json_decode(CACHE::get('tables'));
	} else {
		$tables = DB::query('show tables');
		CACHE::set('tables', json_encode($tables), 5);
		return json_decode(CACHE::get('tables'));
	}
});

///////////////////////////////// API接口定义结束


App::$logFile = './logs/workerman_api.log';
App::$stdoutFile = './logs/api.log';

App::runAll();

启动服务

mkdir logs
php api.php start

压力测试

ab -n 1000000 -c 1000 -k http://localhost:8888/

workerman_api's People

Contributors

hide2 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.