How to do it...

These steps cover writing and running of your application:

  1. From your Terminal or console application, create a new directory called ~/projects/go-programming-cookbook/chapter7/grpc, and navigate to this directory.
  2. Run the following command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter7/grpc

You should see a file called go.mod containing the following:

module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter7/grpc 
  1. Copy tests from ~/projects/go-programming-cookbook-original/chapter7/grpc, or use this as an exercise to write some code of your own!
  2. Create a directory called greeter and navigate to it.
  3. Create a file called greeter.proto with the following content:
        syntax = "proto3";

package greeter;

service GreeterService{
rpc Greet(GreetRequest) returns (GreetResponse) {}
}

message GreetRequest {
string greeting = 1;
string name = 2;
}

message GreetResponse{
string response = 1;
}
  1. Navigate back up a directory to grpc.
  2. Run the following command:
$ protoc --go_out=plugins=grpc:. greeter/greeter.proto
  1. Create a new directory called server and navigate to it.
  2. Create a file called greeter.go with the following content. Ensure that you modify the greeter import to use the path you set up in step 3:
        package main

import (
"fmt"

"github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter7/grpc/greeter"
"golang.org/x/net/context"
)

// Greeter implements the interface
// generated by protoc
type Greeter struct {
Exclaim bool
}

// Greet implements grpc Greet
func (g *Greeter) Greet(ctx context.Context, r
*greeter.GreetRequest) (*greeter.GreetResponse, error) {
msg := fmt.Sprintf("%s %s", r.GetGreeting(), r.GetName())
if g.Exclaim {
msg += "!"
} else {
msg += "."
}
return &greeter.GreetResponse{Response: msg}, nil
}
  1. Create a file called server.go with the following content. Ensure that you modify the greeter import to use the path you set up in step 3:
        package main

import (
"fmt"
"net"

"github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter7/grpc/greeter"
"google.golang.org/grpc"
)

func main() {
grpcServer := grpc.NewServer()
greeter.RegisterGreeterServiceServer(grpcServer,
&Greeter{Exclaim: true})
lis, err := net.Listen("tcp", ":4444")
if err != nil {
panic(err)
}
fmt.Println("Listening on port :4444")
grpcServer.Serve(lis)
}
  1. Navigate back up a directory to grpc.
  2. Create a new directory called client and navigate to it.
  3. Create a file called client.go with the following content. Ensure that you modify the greeter import to use the path you set up in step 3:
        package main

import (
"context"
"fmt"

"github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter7/grpc/greeter"
"google.golang.org/grpc"
)

func main() {
conn, err := grpc.Dial(":4444", grpc.WithInsecure())
if err != nil {
panic(err)
}
defer conn.Close()

client := greeter.NewGreeterServiceClient(conn)

ctx := context.Background()
req := greeter.GreetRequest{Greeting: "Hello", Name:
"Reader"}
resp, err := client.Greet(ctx, &req)
if err != nil {
panic(err)
}
fmt.Println(resp)

req.Greeting = "Goodbye"
resp, err = client.Greet(ctx, &req)
if err != nil {
panic(err)
}
fmt.Println(resp)
}
  1. Navigate back up a directory to grpc.
  1. Run go run ./server, and you will see the following output:
$ go run ./server
Listening on port :4444
  1. In a separate Terminal, run go run ./client from the grpc directory, and you will see the following output:
$ go run ./client
response:"Hello Reader!"
response:"Goodbye Reader!"
  1. The go.mod file may be updated and go.sum file should now be present in the top-level recipe directory.
  2. If you copied or wrote your own tests, go up one directory and run go test. Ensure that all the tests pass.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.238.20