設定ファイル、サーバープログラム、クライアントプログラムの3つが必要です。
$ tree
.
├── redis_create
│ └── redis_create.proto
├── redis_create_client
│ └── main.go
└── redis_create_server
└── main.go
設定ファイル
redis_create/redis_create.proto
syntax = "proto3";
package redis_create;
service Greeter {
rpc RedisCreate (RedisRequest) returns (RedisReply) {}
}
message RedisRequest {
string key = 1;
string strjson = 2;
}
message RedisReply {
string key = 1;
}
サーバープログラム
redis_create_server/main.go
// ---------------------------------------------------------------
//
// redis_create_server/main.go
//
// Feb/12/2020
// ---------------------------------------------------------------
package main
import (
"context"
"log"
"net"
"fmt"
"os"
"google.golang.org/grpc"
pb "../redis_create"
)
const (
port = ":50051"
)
type server struct {
pb.UnimplementedGreeterServer
}
// ---------------------------------------------------------------
func redis_socket_write_proc (conn net.Conn,key_in string,json_str string) {
fmt.Println (key_in)
fmt.Println (json_str)
comm_aa := "set " + key_in + " '" + json_str + "'\r\n"
conn.Write([]byte(comm_aa))
buf := make ([]byte,1024)
conn.Read (buf[:])
fmt.Println (string(buf[0:10]))
}
// ---------------------------------------------------------------
func redis_create_proc (key_in string,str_json string) string {
hostname := "localhost"
port := "6379"
conn, err := net.Dial ("tcp", hostname + ":" + port)
if err != nil {
fmt.Println(err)
return str_json
}
fmt.Fprintf (os.Stderr,"str_json = " + str_json + "\n")
redis_socket_write_proc (conn,key_in,str_json)
return key_in
}
// ---------------------------------------------------------------
func (s *server) RedisCreate(ctx context.Context, in *pb.RedisRequest) (*pb.RedisReply, error) {
fmt.Fprintf (os.Stderr,"*** check aaa ***\n")
key := in.GetKey()
str_json := in.GetStrjson()
fmt.Fprintf (os.Stderr,"key = " + key + "\n")
fmt.Fprintf (os.Stderr,"str_json = " + str_json + "\n")
redis_create_proc (key,str_json)
return &pb.RedisReply{Key: key }, 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)
}
}
// ---------------------------------------------------------------
クライアントプログラム
redis_create_client/main.go
// ---------------------------------------------------------------
//
// redis_create_client/main.go
//
// Feb/12/2020
//
// ---------------------------------------------------------------
package main
import (
"context"
"fmt"
"log"
"os"
"encoding/json"
"strconv"
"time"
"google.golang.org/grpc"
pb "../redis_create"
)
const (
address = "localhost:50051"
defaultKey = "t0001"
defaultName = "aaaa"
defaultPopulation = 1
defaultDate_mod = "2000-01-01"
)
// ---------------------------------------------------------------
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.
key := defaultKey
unit_aa := make(map[string]interface{})
unit_aa["name"] = defaultName
unit_aa["population"] = defaultPopulation
unit_aa["date_mod"] = defaultDate_mod
if len(os.Args) > 1 {
key = os.Args[1]
unit_aa["name"] = os.Args[2]
unit_aa["population"],_ = strconv.Atoi (os.Args[3])
unit_aa["date_mod"] = os.Args[4]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
output, _ := json.Marshal(unit_aa)
str_json := string(output)
r, err := c.RedisCreate(ctx, &pb.RedisRequest{Key: key,Strjson: str_json})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
rvalue := r.GetKey()
fmt.Printf ("%s\n",rvalue)
}
// ---------------------------------------------------------------
gRPC のコードを作成します。
スクリプト
protoc -I redis_create redis_create/redis_create.proto --go_out=plugins=grpc:redis_create
サーバープログラムの起動
go run redis_create_server/main.go
クライアントプログラムの実行
$ go run redis_create_client/main.go t0934 那須烏山 42938 2003-9-22
t0934