Question
- Nowadays, the use of microservice architecture is more common than ever, and the benefits are unquestionable. I am participating in such a project and want to share it with you.
- This article I will divide into 2 parts, and simply display each
hello world
(the article is used temporarily examples of grpc but will change for me to present from a-> z for you to understand, and you often see how it is more complicated than PHP’secho "hello world"
(yaoming))
- Part 1: Create a core server (I use golang, usually a place that uses general data) and a go-server (connect to core server)
- Part 2: Use a child node server to connect to the core server => So the skills in this use will be: golang, nodejs and grpc . A few points to know:
- The article is based on the actual project I did
- proto (grpc) will be created a separate github, then imported into other projects (actual project, in the framework of this article, use only a basic way)
Let’s do it
The first is to create a file helloworld.proto (if you do not know what the .proto extension is, please find out here ).
proto / helloworld.proto
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | syntax = "proto3"; option go_package = "core_server/proto-go/helloworld"; package helloworld; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloReply { string message = 1; } |
So to compile this proto we have protobuf and the command to that compiler is
1 2 3 4 | protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative helloworld/helloworld.proto |
You can see more installation instructions here. After compiling we will have a directory like this.
- Now we will set up the servers
Core Server
The first step we will create go module
with go mod init core_server
command, then of course will create a file go.mod
.
Create the file main.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // Package main implements a server for Greeter service. package main import ( "context" "log" "net" "google.golang.org/grpc" pb "core_server/proto" // mình sẽ lấy dữ liệu proto sau khi generate ở đây ) const ( port = ":50053" // đây là cổng của server grpc core ) // server is used to implement helloworld.GreeterServer. type server struct { pb.UnimplementedGreeterServer } // SayHello implements helloworld.GreeterServer func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { log.Printf("Received: %v", in.GetName()) return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil } func main() { lis, err := net.Listen("tcp", port) if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() pb.RegisterGreeterServer(s, &server{}) if err := s.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) } } |
Then you run the command go run main.go
and wait to be the go server
.
Go Server
Similar to the core server
, in the go server
directory there is also a proto directory (but only using service client), which also creates the go module
.
- Create the main.go directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package main import ( "context" "log" "os" "time" "google.golang.org/grpc" pb "go_server/proto" ) const ( address = "localhost:50053" defaultName = "world" ) func main() { // Set up a connection to the server. conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := pb.NewGreeterClient(conn) // Contact the server and print out its response. name := defaultName if len(os.Args) > 1 { name = os.Args[1] } ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.GetMessage()) } |
Then run the command go run main.go
and wait for the result
- Here is the result shown on the
core server
side - Here is the output of the
go server
after running the run command
summary
- That’s it, I will show you how to connect between golang-golang servers via grpc. In the next part, I will show you how to connect between golang-nodejs.
- Print every
hello world
that is like that, but with large systems, often having new functions like facebook, or many teams implementing in many different languages, this is a very cool thing. The following post uses nodejs as an example (you can use it with php, ruby …)