Skip to content

Commit d812ea4

Browse files
committed
add rpc helloworld
1 parent 2d0d18e commit d812ea4

File tree

7 files changed

+360
-1
lines changed

7 files changed

+360
-1
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ go编程最佳实践
3737
## 问题
3838

3939
go-and-json
40-
[https://eager.io/blog/go-and-json/](https://eager.io/blog/go-and-json/)
40+
[https://eager.io/blog/go-and-json/](https://eager.io/blog/go-and-json/)
41+
42+
43+

grpc/README.MD

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Golang RPC 相关知识
2+
3+
### 如何根据protoc生成代码
4+
5+
指定文件目录的方式
6+
`protoc B.proto --go_out=. --proto_path=$GOPATH/src/B `
7+
8+
已经在文件夹中
9+
`protoc B.proto --go_out=.`
10+
11+
生成RPC服务
12+
`protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld`
13+
14+
### rpc protobuf 入门
15+
16+
http://lameleg.com/tech/rpc-protobuf.html
17+
18+
### 问题
19+
20+
安装protoc-gen-go
21+
https://github.com/golang/protobuf/issues/365#issuecomment-305240878
22+
23+
https://github.com/golang/protobuf#installation
24+
25+
终端代理解决go依赖被和谐的问题
26+
27+
https://github.com/Quinton/blog/issues/2
28+
https://github.com/mrdulin/blog/issues/18
29+
https://droidyue.com/blog/2016/04/04/set-shadowsocks-proxy-for-terminal/index.html
30+
31+
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"os"
7+
"time"
8+
9+
pb "../helloworld"
10+
"google.golang.org/grpc"
11+
)
12+
13+
const (
14+
address = "localhost:5051"
15+
defaultName = "world"
16+
)
17+
18+
func main() {
19+
// Set up a connection to the server.
20+
conn, err := grpc.Dial(address, grpc.WithInsecure())
21+
if err != nil {
22+
log.Fatalf("did not connect: %v", err)
23+
}
24+
defer conn.Close()
25+
c := pb.NewGreeterClient(conn)
26+
27+
// Contact the server and print out its response.
28+
name := defaultName
29+
if len(os.Args) > 1 {
30+
name = os.Args[1]
31+
}
32+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
33+
defer cancel()
34+
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
35+
if err != nil {
36+
log.Fatalf("could not greet: %v", err)
37+
}
38+
log.Printf("Greeting: %s", r.Message)
39+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"net"
7+
8+
pb "../helloworld"
9+
10+
"google.golang.org/grpc"
11+
"google.golang.org/grpc/reflection"
12+
)
13+
14+
const (
15+
port = ":5051"
16+
)
17+
18+
// server is used to implement helloworld.GreeterServer.
19+
type server struct{}
20+
21+
// SayHello implements helloworld.GreeterServer
22+
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
23+
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
24+
}
25+
26+
func main() {
27+
lis, err := net.Listen("tcp", port)
28+
if err != nil {
29+
log.Fatalf("failed to listen: %v", err)
30+
}
31+
s := grpc.NewServer()
32+
pb.RegisterGreeterServer(s, &server{})
33+
// Register reflection service on gRPC server.
34+
reflection.Register(s)
35+
if err := s.Serve(lis); err != nil {
36+
log.Fatalf("failed to serve: %v", err)
37+
}
38+
}

grpc/helloworld/helloworld/helloworld.pb.go

+199
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
syntax = "proto3";
2+
3+
option java_multiple_files = true;
4+
option java_package = "io.grpc.examples.helloworld";
5+
option java_outer_classname = "HelloWorldProto";
6+
7+
package helloworld;
8+
9+
// The greeting service definition.
10+
service Greeter {
11+
// Send a greeting
12+
rpc SayHello (HelloRequest) returns (HelloReply) {}
13+
}
14+
15+
// The request message containing the user's name.
16+
message HelloRequest {
17+
string name = 1;
18+
}
19+
20+
// The response message containing the greetings
21+
message HelloReply {
22+
string message = 1;
23+
}

interview/gofunc.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"sync"
7+
)
8+
9+
func main() {
10+
runtime.GOMAXPROCS(1)
11+
wg := sync.WaitGroup{}
12+
wg.Add(20)
13+
for i := 0; i < 10; i++ {
14+
go func() {
15+
fmt.Println("i: ", i)
16+
wg.Done()
17+
}()
18+
}
19+
for i := 0; i < 10; i++ {
20+
go func(i int) {
21+
fmt.Println("i: ", i)
22+
wg.Done()
23+
}(i)
24+
}
25+
wg.Wait()
26+
}

0 commit comments

Comments
 (0)