PHP
Dapr은 PHP 애플리케이션 개발을 돕는 SDK를 제공해요. 이것을 사용하면 Dapr로 PHP 클라이언트, 서버, 가상 액터를 만들 수 있어요.
출처: Dapr 문서
본문
설정하기 (Setting up)
사전 준비사항 (Prerequisites)
- Composer
- PHP 8
선택적 사전 준비사항 (Optional Prerequisites)
- Docker
- xdebug – 디버깅용
프로젝트 초기화하기
서비스를 만들 디렉터리에서 composer init을 실행하고 질문에 답해요.
composer require dapr/php-sdk와 사용하고 싶은 다른 의존성을 설치해요.
서비스 구성하기
아래 내용을 복사해서 config.php를 만들어요:
<?php
use Dapr\Actors\Generators\ProxyFactory;
use Dapr\Middleware\Defaults\{Response\ApplicationJson,Tracing};
use Psr\Log\LogLevel;
use function DI\{env,get};
return [
// set the log level
'dapr.log.level' => LogLevel::WARNING,
// Generate a new proxy on each request - recommended for development
'dapr.actors.proxy.generation' => ProxyFactory::GENERATED,
// put any subscriptions here
'dapr.subscriptions' => [],
// if this service will be hosting any actors, add them here
'dapr.actors' => [],
// if this service will be hosting any actors, configure how long until dapr should consider an actor idle
'dapr.actors.idle_timeout' => null,
// if this service will be hosting any actors, configure how often dapr will check for idle actors
'dapr.actors.scan_interval' => null,
// if this service will be hosting any actors, configure how long dapr will wait for an actor to finish during drains
'dapr.actors.drain_timeout' => null,
// if this service will be hosting any actors, configure if dapr should wait for an actor to finish
'dapr.actors.drain_enabled' => null,
// you shouldn't have to change this, but the setting is here if you need to
'dapr.port' => env('DAPR_HTTP_PORT', '3500'),
// add any custom serialization routines here
'dapr.serializers.custom' => [],
// add any custom deserialization routines here
'dapr.deserializers.custom' => [],
// the following has no effect, as it is the default middlewares and processed in order specified
'dapr.http.middleware.request' => [get(Tracing::class)],
'dapr.http.middleware.response' => [get(ApplicationJson::class), get(Tracing::class)],
];
서비스 만들기
index.php를 만들고 다음 내용을 넣어요:
<?php
require_once __DIR__.'/vendor/autoload.php';
use Dapr\App;
$app = App::create(configure: fn(\DI\ContainerBuilder $builder) => $builder->addDefinitions(__DIR__ . '/config.php'));
$app->get('/hello/{name}', function(string $name) {
return ['hello' => $name];
});
$app->start();
실행해 보기
dapr init으로 dapr을 초기화하고 dapr run -a dev -p 3000 -- php -S 0.0.0.0:3000으로 프로젝트를 시작해요.
이제 웹 브라우저를 열고 http://localhost:3000/hello/world 로 접속해 보세요.
world를 여러분의 이름이나 애완동물 이름, 원하는 무엇이든 바꿔 넣어 보세요.
축하합니다, 첫 Dapr 서비스를 만들었어요! 여러분이 이것으로 무엇을 만들지 기대되네요!
더 많은 정보 (More Information)
- Packagist
- Dapr SDK 직렬화(serialization)