grpc

command
v0.0.0-...-b446b4a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2020 License: MIT Imports: 11 Imported by: 0

README

gRPC

In gRPC a client application can directly call methods on a server application on a different machine as if it was a local object, making it easier for you to create distributed applications and services.

Requirements

  • protoc complier for protobuf
  • gRPC: go get -u google.golang.org/grpc
  • protoc-gen-go plugin: go get -u github.com/golang/protobuf/protoc-gen-go

Demo

Build the stubs:

make compile
# protoc -I contacts/ contacts/contacts.proto --go_out=plugins=grpc:contacts

Run the server & client:

go run server.go

go run client/main.go
Makefile

Ref.: https://blog.gopheracademy.com/advent-2017/make/

Beyond Basics

Ref.: https://blog.gopheracademy.com/advent-2017/go-grpc-beyond-basics/

Secure gRPC

Generate the certificate:

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
# IMPORTANT: MUST specify server name after the prompt
# Common Name (e.g. server FQDN or YOUR name) []:localhost

Add credential options:

// server
cred, err := credentials.NewServerTLSFromFile("cert.pem", "key.pem")
s := grpc.NewServer(grpc.Creds(cred))

// client
cred, err := credentials.NewClientTLSFromFile("cert.pem", "")
conn, err := grpc.Dial("localhost:15001", grpc.WithTransportCredentials(cred))
Interceptor (Middleware)

Interceptor for Unary RPC (single request) with metadata through context.Context.

More

Details

Proto3 Design
  • All fields are "optional" by design - "required/optional" keywords are removed.

We dropped required fields in proto3 because required fields are generally considered harmful and violating protobuf's compatibility semantics. (https://github.com/google/protobuf/issues/2497)).

... primitive (non-message) fields are no longer nullable. It's better/more accurate to not even think of "unset"; i.e., a new message object already has every primitive field set. The intent, as I understand it, was to make a message more like a plain struct. https://github.com/google/protobuf/issues/359#issuecomment-101756694

See https://github.com/google/protobuf/blob/master/src/google/protobuf/wrappers.proto

// Wrappers for primitive (non-message) types. These types are useful
// for embedding primitives in the `google.protobuf.Any` type and for places
// where we need to distinguish between the absence of a primitive
// typed field and its default value.
gPRC (Proto3) in Go

https://developers.google.com/protocol-buffers/docs/reference/go-generated

message Bar {}

message Baz {
  Bar foo = 1;
}

message Baz2 {
  repeated Bar foo = 1;
}

The compiler will generate a Go struct

type Baz struct {
  Foo *Bar
}

type Baz2 struct {
  Foo []*Bar
}

Thus message fields will be set to nil if unset.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL