使用 Go 的 gRPC 更新 Redis 数据 (更新)

需要三个东西:设定文件、服务器程序和客户端程序。

$ tree
.
├── redis_update
│   └── redis_update.proto
├── redis_update_client
│   └── main.go
└── redis_update_server
    └── main.go

设定文件

redis_update/redis_update.proto
使用Python的gRPC更新Redis数据 (更新)

服务器程序

// ---------------------------------------------------------------
//
//  redis_update_server/main.go
//
//                  Feb/11/2020 
// ---------------------------------------------------------------
package main

import (
    "context"
    "log"
    "net"
    "fmt"
    "os"
    "encoding/json"
    "time"
    "strconv"
    "strings"

    "google.golang.org/grpc"
    pb "../redis_update"
)

const (
    port = ":50051"
)

type server struct {
    pb.UnimplementedGreeterServer
}

// ---------------------------------------------------------------
func get_current_date_proc () string {
    now := time.Now ()
    fmt.Printf ("%s\t" ,now)
    fmt.Printf ("%d-%d-%d\n" ,now.Year (),now.Month(),now.Day())
    today := strconv.Itoa (now.Year()) + "-" +
        fmt.Sprintf ("%d",now.Month()) + "-" +
        strconv.Itoa (now.Day())

    return  today
}

// ---------------------------------------------------------------
func json_update_proc (json_str string,population_in int) string {

    var unit_aa map[string]interface{}
    json.Unmarshal ([]byte(json_str), &unit_aa)
    fmt.Printf ("%s\t",unit_aa["name"])
    fmt.Printf ("%f\t",unit_aa["population"])
    fmt.Printf ("%s\n",unit_aa["date_mod"])

    unit_aa["population"] = population_in
    unit_aa["date_mod"] = get_current_date_proc ()

    output, _ := json.Marshal(unit_aa)

    json_str = string(output)

    return  json_str
}

// ---------------------------------------------------------------
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 socket_read_proc (conn net.Conn,key_in string) string {
    str_received := ""
    _, err := conn.Write([]byte("get " + key_in + "\r\n"))
    if err != nil {
        fmt.Println(err)
        return str_received
        }

    buf := make([]byte, 1024)
    nn, err := conn.Read(buf[:])
    if err != nil {
        fmt.Println(err)
        return str_received
        }

    str_received = string(buf[0:nn])

    return  str_received
}

// ---------------------------------------------------------------
func redis_socket_read_proc (conn net.Conn,key_in string) string {
    json_str := ""
    str_received := socket_read_proc (conn,key_in)

    lines := strings.Split(str_received,"\n")

    if (! strings.Contains(lines[0],"END")) {
        json_str = lines[1]
        }

    return  json_str
}

// ---------------------------------------------------------------
func redis_update_proc (key_in string,population int32) string {
    str_json := ""

    hostname := "localhost"
    port := "6379"

    conn, err := net.Dial ("tcp", hostname + ":" + port)
    if err != nil {
        fmt.Println(err)
        return str_json
        }

    str_json = redis_socket_read_proc (conn,key_in)

    fmt.Fprintf (os.Stderr,"str_json = " + str_json + "\n")
    str_json_new := json_update_proc (str_json,int(population))
    fmt.Println (str_json_new)
    redis_socket_write_proc (conn,key_in,str_json_new)

    return key_in
}
// ---------------------------------------------------------------
func (s *server) RedisUpdate(ctx context.Context, in *pb.RedisRequest) (*pb.RedisReply, error) {
//  log.Printf("SayHello Received: %v", in.GetName())
    fmt.Fprintf (os.Stderr,"*** check aaa ***\n")
    key := in.GetKey()
    population := in.GetPopulation()
    fmt.Fprintf (os.Stderr,"key = " + key + "\n")
    fmt.Fprintf (os.Stderr,"population = %d\n" , population)
    rkey := redis_update_proc (key,population)
    return &pb.RedisReply{Key: rkey }, 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_update_client/main.go
//
//                  Feb/11/2020
//
// ---------------------------------------------------------------
package main

import (
    "context"
    "fmt"
    "log"
    "os"
    "strconv"
    "time"

    "google.golang.org/grpc"
    pb "../redis_update"
)

const (
    address     = "localhost:50051"
    defaultKey = "t0001"
    defaultPopulation = int32(12345)
)

// ---------------------------------------------------------------
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
    population := defaultPopulation
    if len(os.Args) > 1 {
        key = os.Args[1]
        ppt,_ := strconv.Atoi (os.Args[2])
        population = int32(ppt)
    }
    ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    defer cancel()
    r, err := c.RedisUpdate(ctx, &pb.RedisRequest{Key: key,Population: population})
    if err != nil {
        log.Fatalf("could not greet: %v", err)
    }

    rvalue := r.GetKey()

    fmt.Printf ("%s\n",rvalue)
}

// ---------------------------------------------------------------

我将编写gRPC代码。

脚本

protoc -I redis_update redis_update/redis_update.proto --go_out=plugins=grpc:redis_update

启动服务器程序

go run redis_update_server/main.go

执行客户端程序

$ go run redis_update_client/main.go t1858 3298700
t1858