Protocol Buffer Services: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=Internal=
=Internal=
* [[Protocol_Buffer_Concepts#Services|Protocol Buffer Concepts]]
* [[Protocol_Buffer_Concepts#Services|Protocol Buffer Concepts]]
* [[API]]


=Overview=
=Overview=
Line 37: Line 39:
{{Internal|GRPC_Concepts#gRPC_and_Protocol_Buffer_Services|gRPC and Protocol Buffer Services}}
{{Internal|GRPC_Concepts#gRPC_and_Protocol_Buffer_Services|gRPC and Protocol Buffer Services}}


=Example=
<font size=-2>
.
├── go.mod
├── pkg
│    ├── main
│    │    └── main.go
│    ├── modelpb              <font color=teal># Generated</font>
│    │    └── messages.pb.go  <font color=teal># Generated</font>
│    └── servicepb            <font color=teal># Generated</font>
│        └── services.pb.go  <font color=teal># Generated</font>
└── protobuf
      ├── modelpb
      │    └── messages.proto
      └── servicepb
          └── services.proto
</font>
'''services.proto''':
<syntaxhighlight lang='protobuf'>
syntax = "proto3";
option go_package = "./servicepb";
package servicepb;
import "modelpb/messages.proto";
service SomeService {
  rpc SomeEndpoint(modelpb.SomeRequest) returns (modelpb.SomeResponse);
}
</syntaxhighlight>


=Example=
'''messages.proto''':
 
<syntaxhighlight lang='protobuf'>
syntax = "proto3";
 
option go_package = "./modelpb";
package modelpb;
 
message SomeRequest {
  int32 id = 1;
  string payload = 2;
}
 
message SomeResponse {
  int32 id = 1;
  string payload = 2;
}
</syntaxhighlight>
 
Code generation:
 
<font size=-2>
  protoc \
    --proto_path=./protobuf --go_out=./pkg \
      modelpb/messages.proto servicepb/services.proto
</font>

Latest revision as of 02:06, 11 May 2024

External

Internal

Overview

Protocol Buffers can define services that use messages to exchange data.

A service is a set of endpoints, introduced by the rpc keyword, with different semantics that can be used to call into the service, by sending a request, and then receiving a response.

service SomeService {
  rpc SomeEndpoint(SomeRequest) returns (SomeResponse);
  rpc SomeOtherEndpoint(SomeOtherRequest) returns (SomeOtherResponse);
}

message SomeRequest {
  ...
}

message SomeResponse {
  ...
}

message SomeOtherRequest {
  ...
}

message SomeOtherResponse {
  ...
}

This is how you define an API.

The service and the client code is generated by a framework, and the preferred one is gRPC.

gRPC and Protocol Buffer Services

Example

.
├── go.mod
├── pkg
│    ├── main
│    │    └── main.go
│    ├── modelpb               # Generated
│    │    └── messages.pb.go   # Generated
│    └── servicepb             # Generated
│         └── services.pb.go   # Generated 
└── protobuf
     ├── modelpb
     │    └── messages.proto
     └── servicepb
          └── services.proto

services.proto:

syntax = "proto3";

option go_package = "./servicepb";
package servicepb;

import "modelpb/messages.proto";

service SomeService {
  rpc SomeEndpoint(modelpb.SomeRequest) returns (modelpb.SomeResponse);
}

messages.proto:

syntax = "proto3";

option go_package = "./modelpb";
package modelpb;

message SomeRequest {
  int32 id = 1;
  string payload = 2;
}

message SomeResponse {
  int32 id = 1;
  string payload = 2;
}

Code generation:

 protoc \
   --proto_path=./protobuf --go_out=./pkg \
     modelpb/messages.proto servicepb/services.proto