initial import
This commit is contained in:
17
example/rpc/server/stream/etc/config.json
Normal file
17
example/rpc/server/stream/etc/config.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"Name":"test",
|
||||
"MetricsUrl": "http://localhost:2222/add",
|
||||
"ListenOn": "localhost:3456",
|
||||
"Etcd": {
|
||||
"Hosts": [
|
||||
"localhost:2379"
|
||||
],
|
||||
"Key": "rpcx"
|
||||
},
|
||||
"Redis": {
|
||||
"Host": "localhost:6379",
|
||||
"Type": "node",
|
||||
"Key": "apps"
|
||||
},
|
||||
"Auth": false
|
||||
}
|
||||
50
example/rpc/server/stream/server.go
Normal file
50
example/rpc/server/stream/server.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"zero/core/conf"
|
||||
"zero/example/rpc/remote/stream"
|
||||
"zero/rpcx"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type StreamGreetServer int
|
||||
|
||||
func (gs StreamGreetServer) Greet(s stream.StreamGreeter_GreetServer) error {
|
||||
ctx := s.Context()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println("cancelled by client")
|
||||
return ctx.Err()
|
||||
default:
|
||||
req, err := s.Recv()
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("=>", req.Name)
|
||||
greet := "hello, " + req.Name
|
||||
fmt.Println("<=", greet)
|
||||
s.Send(&stream.StreamResp{
|
||||
Greet: greet,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
var c rpcx.RpcServerConf
|
||||
conf.MustLoad("etc/config.json", &c)
|
||||
|
||||
server := rpcx.MustNewServer(c, func(grpcServer *grpc.Server) {
|
||||
stream.RegisterStreamGreeterServer(grpcServer, StreamGreetServer(0))
|
||||
})
|
||||
server.Start()
|
||||
}
|
||||
Reference in New Issue
Block a user