ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
## Mix Micro 与 go-micro 生态深度集成的 php 微服务开发框架,可独立使用 ## 组件 使用 [composer]([https://www.phpcomposer.com/](https://www.phpcomposer.com/)) 安装: ``` composer require mix/micro ``` ## 概述 支持的服务器 - [Mix gRPC](https://github.com/mix-php/grpc) - [Mix JSON-RPC](https://github.com/mix-php/json-rpc) - [Mix Http Server](https://github.com/mix-php/http-server) 支持的注册中心、配置中心 - [Mix Micro Etcd](https://github.com/mix-php/micro-etcd) 其他微服务治理组件 - [Mix Micro Hystrix](https://github.com/mix-php/micro-hystrix) `需单独安装` - [Mix Tracing Zipkin](https://github.com/mix-php/tracing-zipkin) `需单独安装` ## 使用 ### 创建一个 gRPC 服务器 详细使用请查看 “gRPC” 章节: ```php $server = new \Mix\Grpc\Server('0.0.0.0'); // 默认会随机分配端口,也可以指定 $server->register(SayService::class); ``` ### 创建一个注册中心 详细使用请查看 “服务注册” 章节: ```php $registry = new \Mix\Micro\Etcd\Registry('http://127.0.0.1:2379/v3'); ``` ### 创建一个配置中心 详细使用请查看 “配置中心” 章节: ```php $config = new \Mix\Micro\Etcd\Config('http://127.0.0.1:2379/v3'); $config->listen($eventDispatcher); // PSR-14 Event Dispatcher ``` ### 执行一个微服务 启动后会自动监听 SIGINT, SIGTERM, SIGQUIT 信号,自动优雅停止,如果想自己处理信号,传入 `Micro::signal(false)` 配置 ```php use Mix\Micro\Micro; Micro::service( Micro::server($server), Micro::registry($registry), Micro::config($config), Micro::logger($logger), // PSR-3 Logger Micro::version('latest'), Micro::metadata(['foo' => 'bar']) )->run(); ``` 执行后日志打印: ```php [2020-05-21 21:01:18] API.INFO: Server started [0.0.0.0:51438] [2020-05-21 21:01:18] API.INFO: Register service [php.micro.grpc.greeter-2938109e-9b63-11ea-80bd-000057bdb82e] ``` 启动后的微服务可使用 [Micro](https://micro.mu/docs/runtime.html) 工具包 API Gateway、Web Dashboard、Web Proxy 等多种微服务管理工具,也可在 PHP 代码中使用 [Mix gRPC](https://github.com/mix-php/grpc) 的 Client 直接调用 [go-micro](https://github.com/micro/go-micro) 编写的微服务。