Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["direct_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//library/net/netutil/breaker:go_default_library",
"//library/net/rpc/warden:go_default_library",
"//library/net/rpc/warden/proto/testproto:go_default_library",
"//library/net/rpc/warden/resolver:go_default_library",
"//library/time:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["direct.go"],
importpath = "go-common/library/net/rpc/warden/resolver/direct",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/conf/env:go_default_library",
"//library/naming:go_default_library",
"//library/net/rpc/warden/resolver:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,6 @@
### business/warden/resolver/direct
##### Version 1.0.0
1. 实现了基本的服务发现直连功能

View File

@@ -0,0 +1,14 @@
#### business/warden/resolver/direct
##### 项目简介
warden 的直连服务模块用于通过IP地址列表直接连接后端服务
连接字符串格式 direct://default/192.168.1.1:8080,192.168.1.2:8081
##### 编译环境
- **请只用 Golang v1.9.x 以上版本编译执行**
##### 依赖包
- [grpc](google.golang.org/grpc)

View File

@@ -0,0 +1,77 @@
package direct
import (
"context"
"fmt"
"strings"
"go-common/library/conf/env"
"go-common/library/naming"
"go-common/library/net/rpc/warden/resolver"
)
const (
// Name is the name of direct resolver
Name = "direct"
)
var _ naming.Resolver = &Direct{}
// New return Direct
func New() *Direct {
return &Direct{}
}
// Build build direct.
func Build(id string) *Direct {
return &Direct{id: id}
}
// Direct is a resolver for conneting endpoints directly.
// example format: direct://default/192.168.1.1:8080,192.168.1.2:8081
type Direct struct {
id string
}
// Build direct build.
func (d *Direct) Build(id string) naming.Resolver {
return &Direct{id: id}
}
// Scheme return the Scheme of Direct
func (d *Direct) Scheme() string {
return Name
}
// Watch a tree
func (d *Direct) Watch() <-chan struct{} {
ch := make(chan struct{}, 1)
ch <- struct{}{}
return ch
}
//Unwatch a tree
func (d *Direct) Unwatch(id string) {
}
//Fetch fetch isntances
func (d *Direct) Fetch(ctx context.Context) (insMap map[string][]*naming.Instance, found bool) {
var ins []*naming.Instance
addrs := strings.Split(d.id, ",")
for _, addr := range addrs {
ins = append(ins, &naming.Instance{
Addrs: []string{fmt.Sprintf("%s://%s", resolver.Scheme, addr)},
})
}
if len(ins) > 0 {
found = true
}
insMap = map[string][]*naming.Instance{env.Zone: ins}
return
}
//Close close Direct
func (d *Direct) Close() error {
return nil
}

View File

@@ -0,0 +1,85 @@
package direct
import (
"context"
"fmt"
"os"
"testing"
"time"
"go-common/library/net/netutil/breaker"
"go-common/library/net/rpc/warden"
pb "go-common/library/net/rpc/warden/proto/testproto"
"go-common/library/net/rpc/warden/resolver"
xtime "go-common/library/time"
)
type testServer struct {
name string
}
func (ts *testServer) SayHello(context.Context, *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: ts.name, Success: true}, nil
}
func (ts *testServer) StreamHello(ss pb.Greeter_StreamHelloServer) error {
panic("not implement error")
}
func createServer(name, listen string) *warden.Server {
s := warden.NewServer(&warden.ServerConfig{Timeout: xtime.Duration(time.Second)})
ts := &testServer{name}
pb.RegisterGreeterServer(s.Server(), ts)
go func() {
if err := s.Run(listen); err != nil {
panic(fmt.Sprintf("run warden server fail! err: %s", err))
}
}()
return s
}
func TestMain(m *testing.M) {
resolver.Register(New())
ctx := context.TODO()
s1 := createServer("server1", "127.0.0.1:18081")
s2 := createServer("server2", "127.0.0.1:18082")
defer s1.Shutdown(ctx)
defer s2.Shutdown(ctx)
os.Exit(m.Run())
}
func createTestClient(t *testing.T, connStr string) pb.GreeterClient {
client := warden.NewClient(&warden.ClientConfig{
Dial: xtime.Duration(time.Second * 10),
Timeout: xtime.Duration(time.Second * 10),
Breaker: &breaker.Config{
Window: xtime.Duration(3 * time.Second),
Sleep: xtime.Duration(3 * time.Second),
Bucket: 10,
Ratio: 0.3,
Request: 20,
},
})
conn, err := client.Dial(context.TODO(), connStr)
if err != nil {
t.Fatalf("create client fail!err%s", err)
}
return pb.NewGreeterClient(conn)
}
func TestDirect(t *testing.T) {
cli := createTestClient(t, "direct://default/127.0.0.1:18083,127.0.0.1:18082")
count := 0
for i := 0; i < 10; i++ {
if resp, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
t.Fatalf("TestDirect: SayHello failed!err:=%v", err)
} else {
if resp.Message == "server2" {
count++
}
}
}
if count != 10 {
t.Fatalf("TestDirect: get server2 times must be 10")
}
}