Skip to content

Commit b2642fe

Browse files
committed
修改负载机制
1 parent 32dc0d9 commit b2642fe

File tree

4 files changed

+302
-8
lines changed

4 files changed

+302
-8
lines changed

etcd/discovery/version_balancer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type rrPicker struct {
4848

4949
func (p *rrPicker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
5050
p.mu.Lock()
51+
defer p.mu.Unlock()
5152
version := info.Ctx.Value("version")
5253
var subConns []balancer.SubConn
5354
for conn, node := range p.node {
@@ -62,7 +63,6 @@ func (p *rrPicker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
6263
}
6364
index := rand.Intn(len(subConns))
6465
sc := subConns[index]
65-
p.mu.Unlock()
6666
return balancer.PickResult{SubConn: sc}, nil
6767
}
6868

etcd/etcd-grpc/client_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ func TestClient(t *testing.T) {
3232
}
3333
defer conn.Close()
3434
apiClient := api.NewApiClient(conn)
35-
for i := 0; i < 100; i++ {
36-
ctx := context.WithValue(context.Background(), "version", "v1")
37-
_, err := apiClient.ApiTest(ctx, &api.Request{Input: fmt.Sprint(i)})
35+
for {
36+
ctx := context.WithValue(context.Background(), "version", "v2")
37+
_, err := apiClient.ApiTest(ctx, &api.Request{Input: "v1v1v1v1v1"})
3838
if err != nil {
3939
fmt.Println(err)
4040
}
4141
time.Sleep(time.Second)
4242
}
43-
select {}
4443
}

etcd/etcd-grpc/server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ func (a ApiService) ApiTest(ctx context.Context, request *api.Request) (*api.Res
2222
return &api.Response{Output: "v1v1v1v1v1v1v1v1v1v1"}, nil
2323
}
2424

25+
var Addr = "0.0.0.0:8089"
26+
2527
func main() {
26-
listener, err := net.Listen("tcp", "0.0.0.0:8089")
28+
listener, err := net.Listen("tcp", Addr)
2729
if err != nil {
2830
log.Fatalf("net.Listen err: %v", err)
2931
}
@@ -41,7 +43,7 @@ func main() {
4143
}()
4244
s, err := register.NewRegister(
4345
register.SetName("hwholiday.srv.msg"),
44-
register.SetAddress("0.0.0.0:8089"),
46+
register.SetAddress(Addr),
4547
register.SetVersion("v1"),
4648
register.SetSrv(srv),
4749
register.SetEtcdConf(clientv3.Config{
@@ -58,12 +60,13 @@ func main() {
5860
c <- syscall.SIGQUIT
5961
}
6062
}()
63+
fmt.Println("启动成功 === > ", Addr)
6164
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
6265
for a := range c {
6366
switch a {
6467
case syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
6568
fmt.Println("退出")
66-
_ = s.Close()
69+
fmt.Println(s.Close())
6770
return
6871
default:
6972
return

grpc/etcd-grpc/api/api.pb.go

Lines changed: 292 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)