grpc进程间调用和tcp调用对比

  1. 进程间源码

greeter_client.go
// Package main implements a client for Greeter service.
package main

import (
"context"
"log"
"math/rand"
"net"
"os"
"strconv"
"time"

"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

const (
defaultName = "world"
client_file = "/tmp/client_unix"
server_file = "/tmp/server_unix"
)

func UnixConnect(addr string, t time.Duration) (net.Conn, error) {
unix_addr, err := net.ResolveUnixAddr("unix", server_file)
conn, err := net.DialUnix("unix", nil, unix_addr)
return conn, err
}

func main() {

os.Remove(client_file)

// Set up a connection to the server.
conn, err := grpc.Dial(server_file, grpc.WithInsecure(), grpc.WithDialer(UnixConnect))

if err != nil {
log.Fatal("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)

log.Println("start gprc client say hello circle 100 times")
// Contact the server and print out its response.

time1 := time.Now().UnixNano()

for i := 0; i < 1000000; i++ {

name := defaultName
num := rand.Int()
name += strconv.Itoa(num)
_, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
if err != nil {
log.Fatal("could not greet: ", err)
}
//log.Println(r)
//log.Printf("the num Greeting:%d, %s", i, r.Message)

}
time2 := time.Now().UnixNano()
time3 := time2 - time1
time4 := time3 / int64(time.Millisecond)
log.Printf("gRPC test \n start time1: %d \n end time2: %d \n nm time3: %d \n ms time4: %d ms", time1, time2, time3, time4)

greeter_server.go

package main

import (
"context"
"log"
"net"
"os"

"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

const (
//port = ":50051"
server_file = "/tmp/server_unix"
)

// 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{})
//log.Printf("server listening at %v", lis.Addr())
//if err := s.Serve(lis); err != nil {
// log.Fatalf("failed to serve: %v", err)
//}
//log.Println("main is end")

log.Println("start grpc server")
os.Remove(server_file)
//lis, err := net.Listen("tcp", port)
server_addr, err := net.ResolveUnixAddr("unix", server_file)
if err != nil {
log.Fatal("fialed to resolve unix addr")
}

lis, err := net.ListenUnix("unix", server_addr)
if err != nil {
log.Fatal("failed to listen: %v", err)
}

s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
log.Printf("start grpc server server_addr %v \n",server_addr)
s.Serve(lis)
log.Println("grpc server end")
}

//type server struct{
// pb.UnimplementedGreeterServer
//}

//func (s server) SayHello(ctx context.Context, in pb.HelloRequest) (*pb.HelloReply, error) {
// return &pb.HelloReply{Message: "Hello " + in.Name}, nil
//}

func GrpcServer() {
log.Println("start grpc server")
os.Remove(server_file)
//lis, err := net.Listen("tcp", port)
server_addr, err := net.ResolveUnixAddr("unix", server_file)
if err != nil {
log.Fatal("fialed to resolve unix addr")
}

lis, err := net.ListenUnix("unix", server_addr)
if err != nil {
log.Fatal("failed to listen: %v", err)
}

s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
log.Printf("start grpc server server_addr %v \n",server_addr)
s.Serve(lis)
log.Println("grpc server end")
2.grpc TCP 调用
greeter_client.go
package main

import (
"context"
"log"
"math/rand"
"strconv"
"time"

"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

const (
address = "localhost:50051"
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(), 5*time.Second)
//defer cancel()

time1 := time.Now().UnixNano()
//timeStart := time.Now().Unix()
for i := 0; i < 1000000; i++ {
name := defaultName
num := rand.Int()
name += strconv.Itoa(num)
_, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
//log.Printf("the num Greeting:%d, %s", i, r.GetMessage())
//time.Sleep(100*time.Millisecond)
}
time2 := time.Now().UnixNano()
time3 := time2 - time1
time4 := time3 / int64(time.Millisecond)
log.Printf("gRPC TCP/IP test\n start time1: %d \n end time2: %d \n nm time3: %d \n ms time4: %d ms", time1, time2, time3, time4)
}
greeter_server.go
package main

import (
"context"
"log"
"net"

"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

const (
port = ":50051"
)

// 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{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}

上一篇:[转载]Linux下tomcat的catalina.out文件过大,以及目录更改解决办法


下一篇:Java学习笔记-spring-Bean作用于