Skip to content

Commit f9b60d7

Browse files
authored
Merge pull request #6 from chuntaojun/develop
add polaris-php examples
2 parents 2b9c74c + 15b0db5 commit f9b60d7

File tree

16 files changed

+572
-490
lines changed

16 files changed

+572
-490
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,3 @@ tests/*/*.sh
4040
/php
4141
polaris.log
4242
build.sh
43-
polaris.yaml

doc/ApiDoc.md

Lines changed: 297 additions & 379 deletions
Large diffs are not rendered by default.

example/README.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

example/polaris_consumer.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

example/polaris_provider.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

examples/quickstart/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# QuickStart
2+
3+
根据简单的 php socket 应用示例,演示 php 应用如何快速接入北极星。
4+
5+
## 目录介绍
6+
7+
> consumer
8+
9+
php-client 端示例,负责发起 socket 请求。启动时先从北极星拉取一个对应 php-server 的服务地址并发起调用
10+
11+
> provider
12+
13+
php-server 端示例,负责处理 socket 请求。启动时进程1负责处理socket请求,进程2则进行服务实例的注册,并发送心跳维持
14+
实例的健康状态
15+
16+
## 如何构建
17+
18+
- 构建对应的 polaris-php 插件, [构建文档](../../doc/HowToBuild.md)
19+
20+
## 如何使用
21+
22+
### 创建服务
23+
24+
预先通过北极星控制台创建对应的服务,如果是通过本地一键安装包的方式安装,直接在浏览器通过127.0.0.1:8091打开控制台。
25+
26+
![create_service](./image/create_php_service.png)
27+
28+
### 执行程序
29+
30+
运行 php-server
31+
32+
```shell
33+
cd provider
34+
php provider.php
35+
```
36+
37+
运行 php-client
38+
39+
```shell
40+
cd consumer
41+
php consumer.php
42+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
$br = (php_sapi_name() == "cli")? "":"<br>";
3+
4+
if(!extension_loaded('polaris')) {
5+
dl('polaris.' . PHP_SHLIB_SUFFIX);
6+
}
7+
8+
// 创建一个 polaris-client 实例
9+
$polaris = new PolarisClient(array(
10+
"config_path" => "./polaris.yaml",
11+
"log_dir" => "./"
12+
));
13+
14+
$polaris -> InitConsumer();
15+
16+
$get_req = array(
17+
"namespace" => "default",
18+
"service" => "polaris_php_test",
19+
20+
);
21+
22+
$res = $polaris->GetOneInstance($get_req, 5000, 1);
23+
24+
$instances = $res["response"]["instances"];
25+
26+
$address = $instances[0]['host'];
27+
$service_port = (int)$instances[0]["port"];
28+
29+
// 创建并返回一个套接字(通讯节点)
30+
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
31+
if ($socket === false) {
32+
echo "socket_create() failed, reason: ".socket_strerror(socket_last_error())."\n";
33+
}
34+
35+
echo "Attempting to connect to '$address' on port '$service_port'...";
36+
// 发起socket连接请求
37+
$result = socket_connect($socket, $address, $service_port);
38+
if($result === false) {
39+
echo "socket_connect() failed, reason: ".socket_strerror(socket_last_error($socket))."\n";
40+
exit(1);
41+
} else {
42+
echo "Connect success. \n";
43+
}
44+
45+
$input = "This is a message from client"."\n";
46+
47+
// 向socket服务器发送消息
48+
socket_write($socket, $input, strlen($input));
49+
echo "Client send success \n";
50+
51+
echo "Reading response:\n";
52+
// 读取socket服务器发送的消息
53+
while ($out = socket_read($socket, 8192)) {
54+
echo $out;
55+
}
56+
echo PHP_EOL;
57+
socket_close($socket); // 关闭socket连接
58+
59+
?>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
global:
2+
serverConnector:
3+
addresses:
4+
- 127.0.0.1:8091
40.7 KB
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
global:
2+
serverConnector:
3+
addresses:
4+
- 127.0.0.1:8091

0 commit comments

Comments
 (0)