initial import
This commit is contained in:
22
example/rpc/client/direct/Dockerfile
Normal file
22
example/rpc/client/direct/Dockerfile
Normal file
@@ -0,0 +1,22 @@
|
||||
FROM golang:1.13-alpine AS builder
|
||||
|
||||
LABEL stage=gobuilder
|
||||
|
||||
ENV CGO_ENABLED 0
|
||||
ENV GOOS linux
|
||||
ENV GOPROXY https://goproxy.cn,direct
|
||||
|
||||
WORKDIR $GOPATH/src/zero
|
||||
COPY . .
|
||||
RUN go build -ldflags="-s -w" -o /app/unarydirect example/rpc/client/direct/client.go
|
||||
|
||||
|
||||
FROM alpine
|
||||
|
||||
RUN apk add --no-cache tzdata
|
||||
ENV TZ Asia/Shanghai
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app/unarydirect /app/unarydirect
|
||||
|
||||
CMD ["./unarydirect"]
|
||||
10
example/rpc/client/direct/Makefile
Normal file
10
example/rpc/client/direct/Makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
version := v$(shell /bin/date "+%y%m%d%H%M%S")
|
||||
|
||||
build:
|
||||
cd $(GOPATH)/src/zero && docker build -t registry.cn-hangzhou.aliyuncs.com/xapp/unarydirect:$(version) . -f example/rpc/client/direct/Dockerfile
|
||||
|
||||
push: build
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/xapp/unarydirect:$(version)
|
||||
|
||||
deploy: push
|
||||
kubectl -n adhoc set image deployment/unarydirect-deployment unarydirect=registry-vpc.cn-hangzhou.aliyuncs.com/xapp/unarydirect:$(version)
|
||||
50
example/rpc/client/direct/client.go
Normal file
50
example/rpc/client/direct/client.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"zero/core/discov"
|
||||
"zero/example/rpc/remote/unary"
|
||||
"zero/rpcx"
|
||||
)
|
||||
|
||||
const timeFormat = "15:04:05"
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
client := rpcx.MustNewClient(rpcx.RpcClientConf{
|
||||
Etcd: discov.EtcdConf{
|
||||
Hosts: []string{"localhost:2379"},
|
||||
Key: "rpcx",
|
||||
},
|
||||
})
|
||||
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
conn, ok := client.Next()
|
||||
if !ok {
|
||||
time.Sleep(time.Second)
|
||||
break
|
||||
}
|
||||
|
||||
greet := unary.NewGreeterClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
resp, err := greet.Greet(ctx, &unary.Request{
|
||||
Name: "kevin",
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("%s X %s\n", time.Now().Format(timeFormat), err.Error())
|
||||
} else {
|
||||
fmt.Printf("%s => %s\n", time.Now().Format(timeFormat), resp.Greet)
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
23
example/rpc/client/direct/unarydirect.yaml
Normal file
23
example/rpc/client/direct/unarydirect.yaml
Normal file
@@ -0,0 +1,23 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: unarydirect-deployment
|
||||
namespace: adhoc
|
||||
labels:
|
||||
app: unarydirect
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: unarydirect
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: unarydirect
|
||||
spec:
|
||||
containers:
|
||||
- name: unarydirect
|
||||
image: registry-vpc.cn-hangzhou.aliyuncs.com/xapp/unarydirect:v1
|
||||
imagePullPolicy: Always
|
||||
imagePullSecrets:
|
||||
- name: aliyun
|
||||
59
example/rpc/client/stream/client.go
Normal file
59
example/rpc/client/stream/client.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"zero/core/discov"
|
||||
"zero/example/rpc/remote/stream"
|
||||
"zero/rpcx"
|
||||
)
|
||||
|
||||
const name = "kevin"
|
||||
|
||||
var key = flag.String("key", "rpcx", "the key on etcd")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
client, err := rpcx.NewClientNoAuth(discov.EtcdConf{
|
||||
Hosts: []string{"localhost:2379"},
|
||||
Key: *key,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
conn, ok := client.Next()
|
||||
if !ok {
|
||||
log.Fatal("no server")
|
||||
}
|
||||
|
||||
greet := stream.NewStreamGreeterClient(conn)
|
||||
stm, err := greet.Greet(context.Background())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
resp, err := stm.Recv()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("=>", resp.Greet)
|
||||
}
|
||||
}()
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
fmt.Println("<=", name)
|
||||
if err = stm.Send(&stream.StreamReq{
|
||||
Name: name,
|
||||
}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
44
example/rpc/client/unary/client.go
Normal file
44
example/rpc/client/unary/client.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"zero/core/conf"
|
||||
"zero/example/rpc/remote/unary"
|
||||
"zero/rpcx"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "config.json", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c rpcx.RpcClientConf
|
||||
conf.MustLoad(*configFile, &c)
|
||||
client := rpcx.MustNewClient(c)
|
||||
ticker := time.NewTicker(time.Millisecond * 500)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
conn, ok := client.Next()
|
||||
if !ok {
|
||||
log.Fatal("no server")
|
||||
}
|
||||
|
||||
greet := unary.NewGreeterClient(conn)
|
||||
resp, err := greet.Greet(context.Background(), &unary.Request{
|
||||
Name: "kevin",
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("X", err.Error())
|
||||
} else {
|
||||
fmt.Println("=>", resp.Greet)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
5
example/rpc/client/unary/config.json
Normal file
5
example/rpc/client/unary/config.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"Server": "localhost:3457",
|
||||
"App": "adhoc",
|
||||
"Token": "E0459CF7-EA85-4E0C-BB48-C81448811511"
|
||||
}
|
||||
8
example/rpc/client/unary/config_etcd.json
Normal file
8
example/rpc/client/unary/config_etcd.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Hosts": [
|
||||
"127.0.0.1:2379"
|
||||
],
|
||||
"Key": "sms",
|
||||
"App": "adhoc",
|
||||
"Token": "E0459CF7-EA85-4E0C-BB48-C81448811511"
|
||||
}
|
||||
16
example/rpc/proxy/Dockerfile
Normal file
16
example/rpc/proxy/Dockerfile
Normal file
@@ -0,0 +1,16 @@
|
||||
FROM golang:1.11 AS builder
|
||||
|
||||
ENV CGO_ENABLED 0
|
||||
ENV GOOS linux
|
||||
|
||||
WORKDIR $GOPATH/src/zero
|
||||
COPY . .
|
||||
RUN go build -ldflags="-s -w" -o /app/unaryproxy example/rpc/proxy/proxy.go
|
||||
|
||||
|
||||
FROM alpine
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app/unaryproxy /app/unaryproxy
|
||||
|
||||
CMD ["./unaryproxy"]
|
||||
50
example/rpc/proxy/proxy.go
Normal file
50
example/rpc/proxy/proxy.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
|
||||
"zero/core/logx"
|
||||
"zero/core/service"
|
||||
"zero/example/rpc/remote/unary"
|
||||
"zero/rpcx"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var (
|
||||
listen = flag.String("listen", "0.0.0.0:3456", "the address to listen on")
|
||||
server = flag.String("server", "dns:///unaryserver:3456", "the backend service")
|
||||
)
|
||||
|
||||
type GreetServer struct {
|
||||
*rpcx.RpcProxy
|
||||
}
|
||||
|
||||
func (s *GreetServer) Greet(ctx context.Context, req *unary.Request) (*unary.Response, error) {
|
||||
conn, err := s.TakeConn(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
remote := unary.NewGreeterClient(conn)
|
||||
return remote.Greet(ctx, req)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
proxy := rpcx.MustNewServer(rpcx.RpcServerConf{
|
||||
ServiceConf: service.ServiceConf{
|
||||
Log: logx.LogConf{
|
||||
Mode: "console",
|
||||
},
|
||||
},
|
||||
ListenOn: *listen,
|
||||
}, func(grpcServer *grpc.Server) {
|
||||
unary.RegisterGreeterServer(grpcServer, &GreetServer{
|
||||
RpcProxy: rpcx.NewRpcProxy(*server),
|
||||
})
|
||||
})
|
||||
proxy.Start()
|
||||
}
|
||||
46
example/rpc/proxy/unaryproxy.yaml
Normal file
46
example/rpc/proxy/unaryproxy.yaml
Normal file
@@ -0,0 +1,46 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: unaryproxy
|
||||
namespace: kevin
|
||||
spec:
|
||||
selector:
|
||||
app: unaryproxy
|
||||
ports:
|
||||
- name: unaryproxy-port
|
||||
port: 3456
|
||||
|
||||
---
|
||||
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: unaryproxy-deployment
|
||||
namespace: kevin
|
||||
labels:
|
||||
app: unaryproxy
|
||||
spec:
|
||||
replicas: 3
|
||||
selector:
|
||||
matchLabels:
|
||||
app: unaryproxy
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: unaryproxy
|
||||
spec:
|
||||
containers:
|
||||
- name: unaryproxy
|
||||
image: registry-vpc.cn-hangzhou.aliyuncs.com/xapp/unaryproxy:v1
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 3456
|
||||
volumeMounts:
|
||||
- name: timezone
|
||||
mountPath: /etc/localtime
|
||||
imagePullSecrets:
|
||||
- name: aliyun
|
||||
volumes:
|
||||
- name: timezone
|
||||
hostPath:
|
||||
path: /usr/share/zoneinfo/Asia/Shanghai
|
||||
190
example/rpc/remote/stream/greet.pb.go
Normal file
190
example/rpc/remote/stream/greet.pb.go
Normal file
@@ -0,0 +1,190 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: greet.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package stream is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
greet.proto
|
||||
|
||||
It has these top-level messages:
|
||||
StreamReq
|
||||
StreamResp
|
||||
*/
|
||||
package stream
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
|
||||
import (
|
||||
context "golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
type StreamReq struct {
|
||||
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
|
||||
}
|
||||
|
||||
func (m *StreamReq) Reset() { *m = StreamReq{} }
|
||||
func (m *StreamReq) String() string { return proto.CompactTextString(m) }
|
||||
func (*StreamReq) ProtoMessage() {}
|
||||
func (*StreamReq) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
|
||||
func (m *StreamReq) GetName() string {
|
||||
if m != nil {
|
||||
return m.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type StreamResp struct {
|
||||
Greet string `protobuf:"bytes,1,opt,name=greet" json:"greet,omitempty"`
|
||||
}
|
||||
|
||||
func (m *StreamResp) Reset() { *m = StreamResp{} }
|
||||
func (m *StreamResp) String() string { return proto.CompactTextString(m) }
|
||||
func (*StreamResp) ProtoMessage() {}
|
||||
func (*StreamResp) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||
|
||||
func (m *StreamResp) GetGreet() string {
|
||||
if m != nil {
|
||||
return m.Greet
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*StreamReq)(nil), "stream.StreamReq")
|
||||
proto.RegisterType((*StreamResp)(nil), "stream.StreamResp")
|
||||
}
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ context.Context
|
||||
var _ grpc.ClientConn
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion4
|
||||
|
||||
// Client API for StreamGreeter service
|
||||
|
||||
type StreamGreeterClient interface {
|
||||
Greet(ctx context.Context, opts ...grpc.CallOption) (StreamGreeter_GreetClient, error)
|
||||
}
|
||||
|
||||
type streamGreeterClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewStreamGreeterClient(cc *grpc.ClientConn) StreamGreeterClient {
|
||||
return &streamGreeterClient{cc}
|
||||
}
|
||||
|
||||
func (c *streamGreeterClient) Greet(ctx context.Context, opts ...grpc.CallOption) (StreamGreeter_GreetClient, error) {
|
||||
stream, err := grpc.NewClientStream(ctx, &_StreamGreeter_serviceDesc.Streams[0], c.cc, "/stream.StreamGreeter/greet", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &streamGreeterGreetClient{stream}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type StreamGreeter_GreetClient interface {
|
||||
Send(*StreamReq) error
|
||||
Recv() (*StreamResp, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type streamGreeterGreetClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *streamGreeterGreetClient) Send(m *StreamReq) error {
|
||||
return x.ClientStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *streamGreeterGreetClient) Recv() (*StreamResp, error) {
|
||||
m := new(StreamResp)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Server API for StreamGreeter service
|
||||
|
||||
type StreamGreeterServer interface {
|
||||
Greet(StreamGreeter_GreetServer) error
|
||||
}
|
||||
|
||||
func RegisterStreamGreeterServer(s *grpc.Server, srv StreamGreeterServer) {
|
||||
s.RegisterService(&_StreamGreeter_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _StreamGreeter_Greet_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
return srv.(StreamGreeterServer).Greet(&streamGreeterGreetServer{stream})
|
||||
}
|
||||
|
||||
type StreamGreeter_GreetServer interface {
|
||||
Send(*StreamResp) error
|
||||
Recv() (*StreamReq, error)
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type streamGreeterGreetServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *streamGreeterGreetServer) Send(m *StreamResp) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *streamGreeterGreetServer) Recv() (*StreamReq, error) {
|
||||
m := new(StreamReq)
|
||||
if err := x.ServerStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
var _StreamGreeter_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "stream.StreamGreeter",
|
||||
HandlerType: (*StreamGreeterServer)(nil),
|
||||
Methods: []grpc.MethodDesc{},
|
||||
Streams: []grpc.StreamDesc{
|
||||
{
|
||||
StreamName: "greet",
|
||||
Handler: _StreamGreeter_Greet_Handler,
|
||||
ServerStreams: true,
|
||||
ClientStreams: true,
|
||||
},
|
||||
},
|
||||
Metadata: "greet.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("greet.proto", fileDescriptor0) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
// 128 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x4e, 0x2f, 0x4a, 0x4d,
|
||||
0x2d, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x2b, 0x2e, 0x29, 0x4a, 0x4d, 0xcc, 0x55,
|
||||
0x92, 0xe7, 0xe2, 0x0c, 0x06, 0xb3, 0x82, 0x52, 0x0b, 0x85, 0x84, 0xb8, 0x58, 0xf2, 0x12, 0x73,
|
||||
0x53, 0x25, 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0xc0, 0x6c, 0x25, 0x25, 0x2e, 0x2e, 0x98, 0x82,
|
||||
0xe2, 0x02, 0x21, 0x11, 0x2e, 0x56, 0xb0, 0x29, 0x50, 0x25, 0x10, 0x8e, 0x91, 0x33, 0x17, 0x2f,
|
||||
0x44, 0x8d, 0x3b, 0x88, 0x9b, 0x5a, 0x24, 0x64, 0x04, 0x55, 0x26, 0x24, 0xa8, 0x07, 0xb1, 0x47,
|
||||
0x0f, 0x6e, 0x89, 0x94, 0x10, 0xba, 0x50, 0x71, 0x81, 0x06, 0xa3, 0x01, 0x63, 0x12, 0x1b, 0xd8,
|
||||
0x61, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xbc, 0x34, 0x15, 0xe8, 0xa7, 0x00, 0x00, 0x00,
|
||||
}
|
||||
15
example/rpc/remote/stream/greet.proto
Normal file
15
example/rpc/remote/stream/greet.proto
Normal file
@@ -0,0 +1,15 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package stream;
|
||||
|
||||
message StreamReq {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message StreamResp {
|
||||
string greet = 1;
|
||||
}
|
||||
|
||||
service StreamGreeter {
|
||||
rpc greet(stream StreamReq) returns (stream StreamResp);
|
||||
}
|
||||
158
example/rpc/remote/unary/greet.pb.go
Normal file
158
example/rpc/remote/unary/greet.pb.go
Normal file
@@ -0,0 +1,158 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: greet.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package unary is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
greet.proto
|
||||
|
||||
It has these top-level messages:
|
||||
Request
|
||||
Response
|
||||
*/
|
||||
package unary
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
|
||||
import (
|
||||
context "golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
type Request struct {
|
||||
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Request) Reset() { *m = Request{} }
|
||||
func (m *Request) String() string { return proto.CompactTextString(m) }
|
||||
func (*Request) ProtoMessage() {}
|
||||
func (*Request) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
|
||||
func (m *Request) GetName() string {
|
||||
if m != nil {
|
||||
return m.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Greet string `protobuf:"bytes,1,opt,name=greet" json:"greet,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Response) Reset() { *m = Response{} }
|
||||
func (m *Response) String() string { return proto.CompactTextString(m) }
|
||||
func (*Response) ProtoMessage() {}
|
||||
func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||
|
||||
func (m *Response) GetGreet() string {
|
||||
if m != nil {
|
||||
return m.Greet
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*Request)(nil), "unary.Request")
|
||||
proto.RegisterType((*Response)(nil), "unary.Response")
|
||||
}
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ context.Context
|
||||
var _ grpc.ClientConn
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion4
|
||||
|
||||
// Client API for Greeter service
|
||||
|
||||
type GreeterClient interface {
|
||||
Greet(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
|
||||
}
|
||||
|
||||
type greeterClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewGreeterClient(cc *grpc.ClientConn) GreeterClient {
|
||||
return &greeterClient{cc}
|
||||
}
|
||||
|
||||
func (c *greeterClient) Greet(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) {
|
||||
out := new(Response)
|
||||
err := grpc.Invoke(ctx, "/unary.Greeter/greet", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for Greeter service
|
||||
|
||||
type GreeterServer interface {
|
||||
Greet(context.Context, *Request) (*Response, error)
|
||||
}
|
||||
|
||||
func RegisterGreeterServer(s *grpc.Server, srv GreeterServer) {
|
||||
s.RegisterService(&_Greeter_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _Greeter_Greet_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(Request)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(GreeterServer).Greet(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/unary.Greeter/Greet",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(GreeterServer).Greet(ctx, req.(*Request))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _Greeter_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "unary.Greeter",
|
||||
HandlerType: (*GreeterServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "greet",
|
||||
Handler: _Greeter_Greet_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "greet.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("greet.proto", fileDescriptor0) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
// 126 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x4e, 0x2f, 0x4a, 0x4d,
|
||||
0x2d, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x2d, 0xcd, 0x4b, 0x2c, 0xaa, 0x54, 0x92,
|
||||
0xe5, 0x62, 0x0f, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0x11, 0x12, 0xe2, 0x62, 0xc9, 0x4b, 0xcc,
|
||||
0x4d, 0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x02, 0xb3, 0x95, 0x14, 0xb8, 0x38, 0x82, 0x52,
|
||||
0x8b, 0x0b, 0xf2, 0xf3, 0x8a, 0x53, 0x85, 0x44, 0xb8, 0x58, 0xc1, 0x06, 0x40, 0x15, 0x40, 0x38,
|
||||
0x46, 0xc6, 0x5c, 0xec, 0xee, 0x20, 0x46, 0x6a, 0x91, 0x90, 0x06, 0x54, 0x81, 0x10, 0x9f, 0x1e,
|
||||
0xd8, 0x70, 0x3d, 0xa8, 0xc9, 0x52, 0xfc, 0x70, 0x3e, 0xc4, 0xa8, 0x24, 0x36, 0xb0, 0x1b, 0x8c,
|
||||
0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xb8, 0x6d, 0x30, 0xb0, 0x92, 0x00, 0x00, 0x00,
|
||||
}
|
||||
17
example/rpc/remote/unary/greet.proto
Normal file
17
example/rpc/remote/unary/greet.proto
Normal file
@@ -0,0 +1,17 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package unary;
|
||||
|
||||
message Request {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message Response {
|
||||
string greet = 1;
|
||||
}
|
||||
|
||||
service Greeter {
|
||||
rpc greet(Request) returns (Response);
|
||||
}
|
||||
|
||||
|
||||
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()
|
||||
}
|
||||
23
example/rpc/server/unary/Dockerfile
Normal file
23
example/rpc/server/unary/Dockerfile
Normal file
@@ -0,0 +1,23 @@
|
||||
FROM golang:1.13-alpine AS builder
|
||||
|
||||
LABEL stage=gobuilder
|
||||
|
||||
ENV CGO_ENABLED 0
|
||||
ENV GOOS linux
|
||||
ENV GOPROXY https://goproxy.cn,direct
|
||||
|
||||
WORKDIR $GOPATH/src/zero
|
||||
COPY . .
|
||||
RUN go build -ldflags="-s -w" -o /app/unaryserver example/rpc/server/unary/server.go
|
||||
|
||||
|
||||
FROM alpine
|
||||
|
||||
RUN apk add --no-cache tzdata
|
||||
ENV TZ Asia/Shanghai
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app/unaryserver /app/unaryserver
|
||||
COPY example/rpc/server/unary/etc/k8s.json /app/
|
||||
|
||||
CMD ["./unaryserver", "-f", "k8s.json"]
|
||||
11
example/rpc/server/unary/Makefile
Normal file
11
example/rpc/server/unary/Makefile
Normal file
@@ -0,0 +1,11 @@
|
||||
version := v1
|
||||
|
||||
build:
|
||||
cd $(GOPATH)/src/zero && docker build -t registry.cn-hangzhou.aliyuncs.com/xapp/unaryserver:$(version) . -f example/rpc/server/unary/Dockerfile
|
||||
docker image prune --filter label=stage=gobuilder -f
|
||||
|
||||
push: build
|
||||
docker push registry.cn-hangzhou.aliyuncs.com/xapp/unaryserver:$(version)
|
||||
|
||||
deploy: push
|
||||
kubectl -n adhoc set image deployment/unaryserver-deployment unaryserver=registry-vpc.cn-hangzhou.aliyuncs.com/xapp/unaryserver:$(version)
|
||||
13
example/rpc/server/unary/etc/config.json
Normal file
13
example/rpc/server/unary/etc/config.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"Name": "rpc.unary",
|
||||
"Log": {
|
||||
"Mode": "volume"
|
||||
},
|
||||
"ListenOn": "localhost:3456",
|
||||
"Etcd": {
|
||||
"Hosts": [
|
||||
"localhost:2379"
|
||||
],
|
||||
"Key": "rpcx"
|
||||
}
|
||||
}
|
||||
17
example/rpc/server/unary/etc/config1.json
Normal file
17
example/rpc/server/unary/etc/config1.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"Name": "rpc.unary",
|
||||
"MetricsUrl": "http://localhost:2222/add",
|
||||
"ListenOn": "localhost:3457",
|
||||
"Auth": false,
|
||||
"Etcd": {
|
||||
"Hosts": [
|
||||
"localhost:2379"
|
||||
],
|
||||
"Key": "rpcx"
|
||||
},
|
||||
"Redis": {
|
||||
"Host": "localhost:6379",
|
||||
"Type": "node",
|
||||
"Key": "apps"
|
||||
}
|
||||
}
|
||||
12
example/rpc/server/unary/etc/k8s.json
Normal file
12
example/rpc/server/unary/etc/k8s.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"Name": "rpc.unary",
|
||||
"ListenOn": "0.0.0.0:3456",
|
||||
"Auth": false,
|
||||
"Etcd": {
|
||||
"Hosts": [
|
||||
"etcd.discov:2379"
|
||||
],
|
||||
"Key": "rpcx"
|
||||
},
|
||||
"Timeout": 500
|
||||
}
|
||||
55
example/rpc/server/unary/server.go
Normal file
55
example/rpc/server/unary/server.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"zero/core/conf"
|
||||
"zero/example/rpc/remote/unary"
|
||||
"zero/rpcx"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/config.json", "the config file")
|
||||
|
||||
type GreetServer struct {
|
||||
lock sync.Mutex
|
||||
alive bool
|
||||
downTime time.Time
|
||||
}
|
||||
|
||||
func NewGreetServer() *GreetServer {
|
||||
return &GreetServer{
|
||||
alive: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (gs *GreetServer) Greet(ctx context.Context, req *unary.Request) (*unary.Response, error) {
|
||||
fmt.Println("=>", req)
|
||||
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &unary.Response{
|
||||
Greet: "hello from " + hostname,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c rpcx.RpcServerConf
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
server := rpcx.MustNewServer(c, func(grpcServer *grpc.Server) {
|
||||
unary.RegisterGreeterServer(grpcServer, NewGreetServer())
|
||||
})
|
||||
server.Start()
|
||||
}
|
||||
25
example/rpc/server/unary/unaryserver.yaml
Normal file
25
example/rpc/server/unary/unaryserver.yaml
Normal file
@@ -0,0 +1,25 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: unaryserver-deployment
|
||||
namespace: adhoc
|
||||
labels:
|
||||
app: unaryserver
|
||||
spec:
|
||||
replicas: 3
|
||||
selector:
|
||||
matchLabels:
|
||||
app: unaryserver
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: unaryserver
|
||||
spec:
|
||||
containers:
|
||||
- name: unaryserver
|
||||
image: registry-vpc.cn-hangzhou.aliyuncs.com/xapp/unaryserver:v1
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 3456
|
||||
imagePullSecrets:
|
||||
- name: aliyun
|
||||
Reference in New Issue
Block a user