正式环境docker部署hyperf_Hyperf使用docker-compose集群部署
從運(yùn)行容器開(kāi)始
docker run -v /www:/www -p 9601:9601 -p 9602:9602 -p 9603:9603 -it --entrypoint /bin/sh hyperf/hyperf:latest
# 鏡像容器運(yùn)行后,在容器內(nèi)安裝 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 鏡像設(shè)置為阿里云鏡像,加速?lài)?guó)內(nèi)下載速度
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer
$ cd /www
# 通過(guò) Composer 安裝 hyperf/hyperf-skeleton 項(xiàng)目
composer create-project hyperf/hyperf-skeleton api
composer create-project hyperf/hyperf-skeleton org
composer create-project hyperf/hyperf-skeleton mms
在每個(gè)項(xiàng)目根目錄里面都安裝watch,這是熱加載組件
wget -O watch https://gitee.com/hanicc/hyperf-watch/raw/master/watch
使用docker-compose啟動(dòng)
1、在宿主機(jī)的www目錄下創(chuàng)建一個(gè)docker-compose目錄,該目錄用于存放相關(guān)的docker-compose和dockerfile文件
ps:這里不一定要在www目錄下,但是換了其他目錄的時(shí)候執(zhí)行docker—compose -f 后面要帶路徑
2、在docker-compose目錄下分別創(chuàng)建三個(gè)文件夾api、org、mms,這三個(gè)文件夾存儲(chǔ)對(duì)應(yīng)項(xiàng)目的dockerfile,內(nèi)容如下:
FROM hyperf/hyperf
WORKDIR /www
#CMD ["php","bin/hyperf.php","start"]
CMD php watch -c
因?yàn)槭褂玫亩际峭瑐€(gè)鏡像,所以?xún)?nèi)容都一樣
3、邊寫(xiě)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
在宿主機(jī)執(zhí)行一下命令,啟動(dòng)服務(wù)
docker-compose -f /www/docker-compose/docker-compose.yml up
至此服務(wù)開(kāi)啟成功!
但是不知道為什么這里的watch熱加載組件沒(méi)有成功~~~
配置服務(wù)之間的通訊
api作為消費(fèi)端
org和mms作為服務(wù)端
配置如下:
org服務(wù)端
先配置
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
定義服務(wù)提供者和定義接口文件
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服務(wù)提供者','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服務(wù)端
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
定義服務(wù)提供者和定義接口文件
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服務(wù)端','message'=>'電話(huà)號(hào)碼是'.$cellPhone.',驗(yàn)證碼是'.$code];
}
}
JsonRpc/MmsServiceInterface.php
declare(strict_types=1);
namespace App\JsonRpc;
interface MmsServiceInterface
{
public function mmsTest(string $cellPhone, int $code) :array ;
}
api 消費(fèi)端配置
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 ;
}
控制其中調(diào)用
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;
}
}
瀏覽器訪(fǎng)問(wèn)index/testRpc
輸出:
[{"code":0,"data":"這是org服務(wù)提供者","params1":1,"params2":2},{"code":0,"data":"這是mms服務(wù)端","message":"電話(huà)號(hào)碼是13681242210,驗(yàn)證碼是1234"}]
總結(jié)
以上是生活随笔為你收集整理的正式环境docker部署hyperf_Hyperf使用docker-compose集群部署的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python sub 不区分大小写_Py
- 下一篇: 显示部分数据标签_长春市农贸市场监测数据