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()
|
||||
}
|
||||
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