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"
|
||||
}
|
||||
Reference in New Issue
Block a user