grpc

GRPC cross-platform micro-service framework, but the lack of service governance function, service discovery provides a shelf to be realized by itself.

Go package download

go get -u google.golang.org/grpc
Copy the code

Download the proto tool

1. Download idL code generation tool

Repo1.maven.org/maven2/com/… The protoc is placed in the path of the environment variable

2. Download and generate the GO code plug-in

go get -u github.com/golang/protobuf/protoc-gen-go
Copy the code

Copy protoc-gen-go from the GoBin directory and place it in the environment variable path

IDL

IDL syntax, see the Protobuf3 language guide for details


example

Code scaffolding

1. Write the book.proto file

syntax = "proto3"; Book_id 32 bits integer message BookInfoParams {int32 book_id = 1; } // Structure of book details book_name String type message BookInfo {int32 book_id = 1; string book_name = 2; } message BookListParams {int32 page = 1; int32 limit = 2; Message BookList {repeated BookInfo book_list = 1; Service BookService {RPC GetBookInfo (BookInfoParams) returns (BookInfo) {} RPC GetBookList (BookListParams) returns (BookList) {}}Copy the code

2. Generate the corresponding proto code

protoc –go_out=plugins=grpc:. book.proto

Book.pb. go files are generated in the current folder, and code in other languages can also be generated

protoc –php_out=. book.proto

ContextPkgPath = “golang.org/x/net/conte…” Import “golang.org/x/net/conte… “from book.proto. Changed to “context”

Server code

Package main import ("grpc-test/pb" "net" "context" "google.golang.org/grpc") /** Create BookServer structure implementation BookServiceServer interface {GetBookInfo(context. context, *BookInfoParams) (*BookInfo, error) GetBookList(context.Context, *BookListParams) (*BookList, error) } */ type BookServer struct {} func (s *BookServer) GetBookInfo(ctx context.Context, in *book.BookInfoParams) (*book.BookInfo, B := new(book.bookinfo) b.bookid = in.bookid b.bookName = "21 days proficient in PHP "return b,nil} func (s) *BookServer) GetBookList(ctx context.Context, in *book.BookListParams) (*book.BookList, Bl := new(book.bookList) bl.BookList = append(bl.booklist, &book.bookinfo {BookId:1,BookName:"21 days proficient in PHP "}) bl.bookList = append(bl.booklist, &book.bookinfo {BookId:2,BookName:"21 days proficient in Java "}) return bl,nil} func main() {serviceAddress := ":50052" bookServer := New (BookServer) // create TCP listener ls, _ := net.Listen(" TCP ", Gs serviceAddress) / / create GRPC service: = GRPC. NewServer () / / registered bookServer book. RegisterBookServiceServer (gs, BookServer) // Start service gs.serve (ls)}Copy the code

Client code

package main import ( "fmt" "grpc-test/pb" "google.golang.org/grpc" "context" ) func main() { serviceAddress := Conn, err := grpc.Dial(serviceAddress, grpc.withInsecure ()) if err! = nil { panic("connect error") } defer conn.Close() bookClient := book.NewBookServiceClient(conn) Bi,_:= bookClient.getBookInfo (context.background (),& book.bookInfoparams {BookId:1}) FMT.Println(" Get book details ") fmt.Println("bookId: 1", "=>", "bookName:", bi.BookName) bl,_ := bookClient.GetBookList(context.Background(), &book.BookListParams{Page:1, Println("bookId:", b.bookid, "=>", "bookName:", b.BookName) } }Copy the code

Start the test

Start the server and the client

GOROOT=D:\Go #gosetup GOPATH=D:\go\gopath #gosetup D:\Go\bin\go.exe build -i -o C:\Users\Administrator\AppData\Local\Temp\___17110go_build_main_go.exe D:/go/gopath/src/grpc-test/client/main.go Exe C:\Users\Administrator\AppData\Local\Temp\___17110go_build_main_go.exe #gosetup D:\ software \GoLand\bin\runnerw.exe C:\Users\Administrator\AppData\Local\Temp\___17110go_build_main_go.exe #gosetup BookId: 1 => bookName: 21 days proficient in PHP bookId: 2 => bookName: 21 days proficient in PHP bookId: 2 => bookName: 21 days proficient in PHP 21 days Java Process Finished with Exit Code 0Copy the code