Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,3 @@ tests/*/*.sh
/php
polaris.log
build.sh
polaris.yaml
676 changes: 297 additions & 379 deletions doc/ApiDoc.md

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions example/README.md

This file was deleted.

25 changes: 0 additions & 25 deletions example/polaris_consumer.php

This file was deleted.

71 changes: 0 additions & 71 deletions example/polaris_provider.php

This file was deleted.

42 changes: 42 additions & 0 deletions examples/quickstart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# QuickStart

根据简单的 php socket 应用示例,演示 php 应用如何快速接入北极星。

## 目录介绍

> consumer

php-client 端示例,负责发起 socket 请求。启动时先从北极星拉取一个对应 php-server 的服务地址并发起调用

> provider

php-server 端示例,负责处理 socket 请求。启动时进程1负责处理socket请求,进程2则进行服务实例的注册,并发送心跳维持
实例的健康状态

## 如何构建

- 构建对应的 polaris-php 插件, [构建文档](../../doc/HowToBuild.md)

## 如何使用

### 创建服务

预先通过北极星控制台创建对应的服务,如果是通过本地一键安装包的方式安装,直接在浏览器通过127.0.0.1:8091打开控制台。

![create_service](./image/create_php_service.png)

### 执行程序

运行 php-server

```shell
cd provider
php provider.php
```

运行 php-client

```shell
cd consumer
php consumer.php
```
59 changes: 59 additions & 0 deletions examples/quickstart/consumer/consumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
$br = (php_sapi_name() == "cli")? "":"<br>";

if(!extension_loaded('polaris')) {
dl('polaris.' . PHP_SHLIB_SUFFIX);
}

// 创建一个 polaris-client 实例
$polaris = new PolarisClient(array(
"config_path" => "./polaris.yaml",
"log_dir" => "./"
));

$polaris -> InitConsumer();

$get_req = array(
"namespace" => "default",
"service" => "polaris_php_test",

);

$res = $polaris->GetOneInstance($get_req, 5000, 1);

$instances = $res["response"]["instances"];

$address = $instances[0]['host'];
$service_port = (int)$instances[0]["port"];

// 创建并返回一个套接字(通讯节点)
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed, reason: ".socket_strerror(socket_last_error())."\n";
}

echo "Attempting to connect to '$address' on port '$service_port'...";
// 发起socket连接请求
$result = socket_connect($socket, $address, $service_port);
if($result === false) {
echo "socket_connect() failed, reason: ".socket_strerror(socket_last_error($socket))."\n";
exit(1);
} else {
echo "Connect success. \n";
}

$input = "This is a message from client"."\n";

// 向socket服务器发送消息
socket_write($socket, $input, strlen($input));
echo "Client send success \n";

echo "Reading response:\n";
// 读取socket服务器发送的消息
while ($out = socket_read($socket, 8192)) {
echo $out;
}
echo PHP_EOL;
socket_close($socket); // 关闭socket连接

?>
4 changes: 4 additions & 0 deletions examples/quickstart/consumer/polaris.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global:
serverConnector:
addresses:
- 127.0.0.1:8091
Binary file added examples/quickstart/image/create_php_service.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/quickstart/provider/polaris.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global:
serverConnector:
addresses:
- 127.0.0.1:8091
99 changes: 99 additions & 0 deletions examples/quickstart/provider/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

if(!extension_loaded('polaris')) {
dl('polaris.' . PHP_SHLIB_SUFFIX);
}

set_time_limit(0);

class TCPServer
{
private $ip = '127.0.0.1';
private $port = 9996;

private $_socket = null;

public function __construct()
{
$this->_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->_socket === false) {
die(socket_strerror(socket_last_error($this->_socket)));
}
}

public function run()
{
$bind_ok = socket_bind($this->_socket, $this->ip, $this->port);

if ($bind_ok === false) {
echo "socket_bind() failed, reason: ".socket_strerror(socket_last_error())."\n";
} else {
echo "socket_bind() success";
}

socket_listen($this->_socket, 5);
while(true) {
$socketAccept = socket_accept($this->_socket);
$request = socket_read($socketAccept, 1024);
echo "receive from client '$request'";
socket_write($socketAccept, 'hello. I`m provider', strlen('hello. I`m provider'));
socket_close($socketAccept);
}
}

public function close()
{
socket_close($this->_socket);
}
}


$pid = pcntl_fork();
if ( $pid == 0 ) {
// 创建一个 polaris-provider 实例
$polaris = new PolarisClient(array(
"config_path" => "./polaris.yaml",
"log_dir" => "./"
));

$polaris -> InitProvider();

// 实例注册信息
$register_instance_info = array(
"namespace" => "default",
"service" => "polaris_php_test",
"host" => "127.0.0.1",
"port" => "9996",
"heartbeat" => "true",
"protocol" => "TCP",
"vpc_id" => "",
"weight" => "88",
"ttl" => "5",
"metadata" => array(
"client" => "php"
)
);

// 执行实例注册动作
$res = $polaris->Register($register_instance_info, 5000, 1);
var_dump($res);

// 实例心跳信息
$heartbeat_info = array(
"namespace" => "default",
"service" => "polaris_php_test",
"host" => "127.0.0.1",
"port" => "9996",
);
while (true) {
// 先进行一次心跳上报,触发实例租约计算任务
$res = $polaris->Heartbeat($heartbeat_info);
sleep(4);
var_dump($res);
}
} else {
echo "run tpc server\n";
$tcp_server = new TCPServer();
$tcp_server->run();
}
?>
59 changes: 59 additions & 0 deletions examples/ratelimit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# RateLimit Example

根据简单的 polaris-php 使用示例,演示 php 应用如何快速使用北极星的服务限流功能。

## 如何构建

- 构建对应的 polaris-php 插件, [构建文档](../../doc/HowToBuild.md)

## 如何使用

### 创建服务

- 预先通过北极星控制台创建对应的服务,如果是通过本地一键安装包的方式安装,直接在浏览器通过127.0.0.1:8091打开控制台。
- ![create_service](./image/create_php_service.png)
- 配置该服务的限流参数
- ![setting_ratelimit](./image/setting_ratelimit_rule.png)


### 执行程序

```shell
php ratelimit.php
```

观察输出结果

- 第一次输出期望值

```
array(3) {
["code"]=> int(0)
["err_msg"]=> string(9) "0-success"
["quota_result"]=> array(6) {
["quota_result_code"]=> int(0) // 配额正常
["quota_duration"]=> int(1000)
["quota_left"]=> int(0)
["quota_all"]=> int(1)
["degrade"]=> bool(false)
["wait_time"]=> int(0)
}
}
```

- 第二次输出期望值

```
array(3) {
["code"]=> int(0)
["err_msg"]=> string(9) "0-success"
["quota_result"]=> array(6) {
["quota_result_code"]=> int(1) // 配额被限流
["quota_duration"]=> int(1000)
["quota_left"]=> int(0)
["quota_all"]=> int(1)
["degrade"]=> bool(false)
["wait_time"]=> int(0)
}
}
```
Binary file added examples/ratelimit/image/create_php_service.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/ratelimit/polaris.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global:
serverConnector:
addresses:
- 127.0.0.1:8091
Loading