initial import
This commit is contained in:
19
example/tracing/portal/etc/config.json
Normal file
19
example/tracing/portal/etc/config.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"Name": "portal.rpc",
|
||||
"ListenOn": "localhost:3456",
|
||||
"Etcd": {
|
||||
"Hosts": [
|
||||
"localhost:2379"
|
||||
],
|
||||
"Key": "portal"
|
||||
},
|
||||
"UserRpc": {
|
||||
"Etcd": {
|
||||
"Hosts": [
|
||||
"localhost:2379"
|
||||
],
|
||||
"Key": "user"
|
||||
}
|
||||
},
|
||||
"Timeout": 500
|
||||
}
|
||||
67
example/tracing/portal/server.go
Normal file
67
example/tracing/portal/server.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
|
||||
"zero/core/conf"
|
||||
"zero/example/tracing/remote/portal"
|
||||
"zero/example/tracing/remote/user"
|
||||
"zero/rpcx"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/config.json", "the config file")
|
||||
|
||||
type (
|
||||
Config struct {
|
||||
rpcx.RpcServerConf
|
||||
UserRpc rpcx.RpcClientConf
|
||||
}
|
||||
|
||||
PortalServer struct {
|
||||
userRpc *rpcx.RpcClient
|
||||
}
|
||||
)
|
||||
|
||||
func NewPortalServer(client *rpcx.RpcClient) *PortalServer {
|
||||
return &PortalServer{
|
||||
userRpc: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (gs *PortalServer) Portal(ctx context.Context, req *portal.PortalRequest) (*portal.PortalResponse, error) {
|
||||
conn, ok := gs.userRpc.Next()
|
||||
if !ok {
|
||||
return nil, errors.New("internal error")
|
||||
}
|
||||
|
||||
greet := user.NewUserClient(conn)
|
||||
resp, err := greet.GetGrade(ctx, &user.UserRequest{
|
||||
Name: req.Name,
|
||||
})
|
||||
if err != nil {
|
||||
return &portal.PortalResponse{
|
||||
Response: err.Error(),
|
||||
}, nil
|
||||
} else {
|
||||
return &portal.PortalResponse{
|
||||
Response: resp.Response,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
client := rpcx.MustNewClient(c.UserRpc)
|
||||
server := rpcx.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||
portal.RegisterPortalServer(grpcServer, NewPortalServer(client))
|
||||
})
|
||||
server.Start()
|
||||
}
|
||||
Reference in New Issue
Block a user