|
1 | | -# grpc |
2 | | -RRv2 gRPC plugin |
| 1 | +## PHP Client |
| 2 | + |
| 3 | +[Roadrunner GRPC](https://github.com/spiral/roadrunner-grpc) |
| 4 | + |
| 5 | +### Compiling proto sample: |
| 6 | +``` |
| 7 | +protoc --plugin=protoc-gen-php-grpc --php-grpc_out=<OUTPUT DIRECTORY> simple.proto |
| 8 | +``` |
| 9 | + |
| 10 | +- `protoc-gen-php-grpc` plugin can be downloaded from the `roadrunner-binary` releases page. |
| 11 | + |
| 12 | +## Configuration |
| 13 | + |
| 14 | +```yaml |
| 15 | +grpc: |
| 16 | + # GRPC address to listen |
| 17 | + # |
| 18 | + # This option is required |
| 19 | + listen: "tcp://localhost:9001" |
| 20 | + |
| 21 | + # Proto files to use |
| 22 | + # |
| 23 | + # This option is required. At least one proto file must be specified. |
| 24 | + proto: |
| 25 | + - "proto/test/test.proto" |
| 26 | + - "proto/health/health.proto" |
| 27 | + |
| 28 | + # GRPC TLS configuration |
| 29 | + # |
| 30 | + # This section is optional |
| 31 | + tls: |
| 32 | + # Path to the key file |
| 33 | + # |
| 34 | + # This option is required |
| 35 | + key: "" |
| 36 | + |
| 37 | + # Path to the certificate |
| 38 | + # |
| 39 | + # This option is required |
| 40 | + cert: "" |
| 41 | + |
| 42 | + # Path to the CA certificate |
| 43 | + # |
| 44 | + # This option is optional |
| 45 | + root_ca: "" |
| 46 | + |
| 47 | + # Client auth type |
| 48 | + # |
| 49 | + # This option is optional. Default value: no_client_certs. Possible values: request_client_cert, require_any_client_cert, verify_client_cert_if_given, require_and_verify_client_cert, no_client_certs |
| 50 | + client_auth_type: "" |
| 51 | + |
| 52 | + # Maximum send message size |
| 53 | + # |
| 54 | + # This option is optional. Default value: 50 (MB) |
| 55 | + max_send_msg_size: 50 |
| 56 | + |
| 57 | + # Maximum receive message size |
| 58 | + # |
| 59 | + # This option is optional. Default value: 50 (MB) |
| 60 | + max_recv_msg_size: 50 |
| 61 | + |
| 62 | + # MaxConnectionIdle is a duration for the amount of time after which an |
| 63 | + # idle connection would be closed by sending a GoAway. Idleness duration is |
| 64 | + # defined since the most recent time the number of outstanding RPCs became |
| 65 | + # zero or the connection establishment. |
| 66 | + # |
| 67 | + # This option is optional. Default value: infinity. |
| 68 | + max_connection_idle: 0s |
| 69 | + |
| 70 | + # MaxConnectionAge is a duration for the maximum amount of time a |
| 71 | + # connection may exist before it will be closed by sending a GoAway. A |
| 72 | + # random jitter of +/-10% will be added to MaxConnectionAge to spread out |
| 73 | + # connection storms. |
| 74 | + # |
| 75 | + # This option is optional. Default value: infinity. |
| 76 | + max_connection_age: 0s |
| 77 | + |
| 78 | + # MaxConnectionAgeGrace is an additive period after MaxConnectionAge after |
| 79 | + # which the connection will be forcibly closed. |
| 80 | + max_connection_age_grace: 0s |
| 81 | + |
| 82 | + # MaxConnectionAgeGrace is an additive period after MaxConnectionAge after |
| 83 | + # which the connection will be forcibly closed. |
| 84 | + # |
| 85 | + # This option is optional: Default value: 10 |
| 86 | + max_concurrent_streams: 10 |
| 87 | + |
| 88 | + # After a duration of this time if the server doesn't see any activity it |
| 89 | + # pings the client to see if the transport is still alive. |
| 90 | + # If set below 1s, a minimum value of 1s will be used instead. |
| 91 | + # |
| 92 | + # This option is optional. Default value: 2h |
| 93 | + ping_time: 1s |
| 94 | + |
| 95 | + # After having pinged for keepalive check, the server waits for a duration |
| 96 | + # of Timeout and if no activity is seen even after that the connection is |
| 97 | + # closed. |
| 98 | + # |
| 99 | + # This option is optional. Default value: 20s |
| 100 | + timeout: 200s |
| 101 | + |
| 102 | + # Usual workers pool configuration |
| 103 | + pool: |
| 104 | + num_workers: 2 |
| 105 | + max_jobs: 0 |
| 106 | + allocate_timeout: 60s |
| 107 | + destroy_timeout: 60 |
| 108 | +``` |
| 109 | +
|
| 110 | +## Minimal dependencies: |
| 111 | +
|
| 112 | +1. `Server` plugin for the workers pool. |
| 113 | +2. `Logger` plugin to show log messages. |
| 114 | +3. `Config` plugin to read and populate plugin's configuration. |
| 115 | + |
| 116 | +## GRPC worker sample: |
| 117 | + |
| 118 | +```php |
| 119 | +<?php |
| 120 | +
|
| 121 | +/** |
| 122 | + * Sample GRPC PHP server. |
| 123 | + */ |
| 124 | +
|
| 125 | +use Service\EchoInterface; |
| 126 | +use Spiral\RoadRunner\GRPC\Server; |
| 127 | +use Spiral\RoadRunner\Worker; |
| 128 | +
|
| 129 | +require __DIR__ . '/vendor/autoload.php'; |
| 130 | +
|
| 131 | +$server = new Server(null, [ |
| 132 | + 'debug' => false, // optional (default: false) |
| 133 | +]); |
| 134 | +
|
| 135 | +$server->registerService(EchoInterface::class, new EchoService()); |
| 136 | +
|
| 137 | +$server->serve(Worker::create()); |
| 138 | +
|
| 139 | +``` |
| 140 | + |
| 141 | +#### Proto file sample: |
| 142 | + |
| 143 | +```protobuf |
| 144 | +syntax = "proto3"; |
| 145 | +
|
| 146 | +package service; |
| 147 | +option go_package = "./;test"; |
| 148 | +
|
| 149 | +service Test { |
| 150 | + rpc Echo (Message) returns (Message) { |
| 151 | + } |
| 152 | +
|
| 153 | + rpc Throw (Message) returns (Message) { |
| 154 | + } |
| 155 | +
|
| 156 | + rpc Die (Message) returns (Message) { |
| 157 | + } |
| 158 | +
|
| 159 | + rpc Info (Message) returns (Message) { |
| 160 | + } |
| 161 | +
|
| 162 | + rpc Ping (EmptyMessage) returns (EmptyMessage) { |
| 163 | + } |
| 164 | +} |
| 165 | +
|
| 166 | +message Message { |
| 167 | + string msg = 1; |
| 168 | +} |
| 169 | +
|
| 170 | +message EmptyMessage { |
| 171 | +} |
| 172 | +
|
| 173 | +message DetailsMessageForException { |
| 174 | + uint64 code = 1; |
| 175 | + string message = 2; |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +Test certificates (including `root ca`) located [here](../../tests/plugins/grpc/configs/test-certs). |
| 180 | + |
| 181 | +## Common issues: |
| 182 | +1. Registering two services with the same name is not allowed. GRPC server will panic after that. |
0 commit comments