正式环境docker部署hyperf_Hyperf使用docker-compose集群部署
從運行容器開始
docker run -v /www:/www -p 9601:9601 -p 9602:9602 -p 9603:9603 -it --entrypoint /bin/sh hyperf/hyperf:latest
# 鏡像容器運行后,在容器內安裝 Composer
wget https://github.com/composer/composer/releases/download/1.8.6/composer.phar
chmod u+x composer.phar
mv composer.phar /usr/local/bin/composer
# 將 Composer 鏡像設置為阿里云鏡像,加速國內下載速度
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer
$ cd /www
# 通過 Composer 安裝 hyperf/hyperf-skeleton 項目
composer create-project hyperf/hyperf-skeleton api
composer create-project hyperf/hyperf-skeleton org
composer create-project hyperf/hyperf-skeleton mms
在每個項目根目錄里面都安裝watch,這是熱加載組件
wget -O watch https://gitee.com/hanicc/hyperf-watch/raw/master/watch
使用docker-compose啟動
1、在宿主機的www目錄下創建一個docker-compose目錄,該目錄用于存放相關的docker-compose和dockerfile文件
ps:這里不一定要在www目錄下,但是換了其他目錄的時候執行docker—compose -f 后面要帶路徑
2、在docker-compose目錄下分別創建三個文件夾api、org、mms,這三個文件夾存儲對應項目的dockerfile,內容如下:
FROM hyperf/hyperf
WORKDIR /www
#CMD ["php","bin/hyperf.php","start"]
CMD php watch -c
因為使用的都是同個鏡像,所以內容都一樣
3、邊寫docker-compose.yml
version: '2'
networks:
dev_yes:
services:
api:
build:
context: ./api
dockerfile: dockerfile
ports:
- "9601:9601"
volumes:
- /www/api:/www
networks:
- dev_yes
mms:
build:
context: ./mms
dockerfile: dockerfile
ports:
- "9602:9602"
volumes:
- /www/mms:/www
networks:
- dev_yes
org:
build:
context: ./org
dockerfile: dockerfile
ports:
- "9603:9603"
volumes:
- /www/org:/www
networks:
- dev_yes
在宿主機執行一下命令,啟動服務
docker-compose -f /www/docker-compose/docker-compose.yml up
至此服務開啟成功!
但是不知道為什么這里的watch熱加載組件沒有成功~~~
配置服務之間的通訊
api作為消費端
org和mms作為服務端
配置如下:
org服務端
先配置
config/autoload/server.php
'servers' => [
[
'name' => 'jsonrpc',
'type' => Server::SERVER_BASE,
'host' => '0.0.0.0',
'port' => 9603,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
SwooleEvent::ON_RECEIVE => [Hyperf\JsonRpc\TcpServer::class, 'onReceive'],
],
],
]
修改.env
APP_NAME=org
定義服務提供者和定義接口文件
JsonRpc/TestService.php
declare(strict_types=1);
namespace App\JsonRpc;
use Hyperf\RpcServer\Annotation\RpcService;
/**
* Class TestService
* @package App\JsonRpc
* @RpcService(name="TestService",protocol="jsonrpc",server="jsonrpc")
*/
class TestService implements TestServiceInterface
{
public function test(int $a , int $b) :array
{
return ['code'=>0,'data'=>'這是org服務提供者','params1'=>$a,'params2'=>$b];
}
}
JsonRpc/TestServiceInterface.php
declare(strict_types=1);
namespace App\JsonRpc;
interface TestServiceInterface
{
public function test(int $a , int $b):array ;
}
mms服務端
config/autoload/server.php
'servers' => [
[
'name' => 'jsonrpc',
'type' => Server::SERVER_BASE,
'host' => '0.0.0.0',
'port' => 9602,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
SwooleEvent::ON_RECEIVE => [Hyperf\JsonRpc\TcpServer::class, 'onReceive'],
],
],
]
修改.env
APP_NAME=mms
定義服務提供者和定義接口文件
JsonRpc/MmsService.php
declare(strict_types=1);
namespace App\JsonRpc;
use Hyperf\RpcServer\Annotation\RpcService;
/**
* Class MmsServeice
* @package App\JsonRpc
* @RpcService(name="MmsService",protocol="jsonrpc",server="jsonrpc")
*/
class MmsService implements MmsServiceInterface
{
public function mmsTest(string $cellPhone,int $code) :array
{
return ['code'=>0,'data'=>'這是mms服務端','message'=>'電話號碼是'.$cellPhone.',驗證碼是'.$code];
}
}
JsonRpc/MmsServiceInterface.php
declare(strict_types=1);
namespace App\JsonRpc;
interface MmsServiceInterface
{
public function mmsTest(string $cellPhone, int $code) :array ;
}
api 消費端配置
config/autoload/services.php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
return [
'consumers' => value(function (){
$services = [
'TestService'=>['service'=>\App\JsonRpc\TestServiceInterface::class,'host'=>'org','port'=>9603],
'MmsService'=>['service'=>\App\JsonRpc\MmsServiceInterface::class,'host'=>'mms','port'=>9602],
];
$consumers =[];
foreach ($services as $name => $value){
$consumers[] = [
'name' => $name,
'service' => $value['service'],
'protocol' => 'jsonrpc',
'load_balancer' => 'random',
'nodes' => [
['host' => $value['host'], 'port' => $value['port']],
],
];
}
return $consumers;
})
];
定義org和mms接口文件
JsonRpc/TestServiceInterface.php
declare(strict_types=1);
namespace App\JsonRpc;
interface TestServiceInterface
{
public function test(int $a , int $b):array ;
}
JsonRpc/MmsServiceInterface.php
declare(strict_types=1);
namespace App\JsonRpc;
interface MmsServiceInterface
{
public function mmsTest(string $cellPhone, int $code) :array ;
}
控制其中調用
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
use App\JsonRpc\MmsServiceInterface;
use App\JsonRpc\TestServiceInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\Utils\ApplicationContext;
/**
* Class IndexController
* @package App\Controller
* @Controller(prefix="index")
*/
class IndexController extends AbstractController
{
/**
* @Author xue
* @DateTime 2020-08-25
* @GetMapping(path="testRpc")
*/
public function testRpc()
{
$orgClient = ApplicationContext::getContainer()->get(TestServiceInterface::class);
$data1 = $orgClient->test(1,2);
$mmsClient = ApplicationContext::getContainer()->get(MmsServiceInterface::class);
$data2 = $mmsClient->mmsTest('13681242210',1234);
$data[]=$data1;
$data[]=$data2;
return $data;
}
}
瀏覽器訪問index/testRpc
輸出:
[{"code":0,"data":"這是org服務提供者","params1":1,"params2":2},{"code":0,"data":"這是mms服務端","message":"電話號碼是13681242210,驗證碼是1234"}]
總結
以上是生活随笔為你收集整理的正式环境docker部署hyperf_Hyperf使用docker-compose集群部署的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python sub 不区分大小写_Py
- 下一篇: 显示部分数据标签_长春市农贸市场监测数据