main.go 2.65 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11
package main

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	"google.golang.org/grpc"
	"google.golang.org/grpc/keepalive"
	"google.golang.org/grpc/resolver"
	"google.golang.org/grpc/resolver/manual"
	"hilo-algoCenter/algo"
hujiebin's avatar
hujiebin committed
12
	"hilo-algoCenter/common/config"
hujiebin's avatar
hujiebin committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	"hilo-algoCenter/common/mylogrus"
	"hilo-algoCenter/protocol/userCenter"
	"time"
)

const (
	default_redis_address  = "47.244.34.27:6379"
	default_redis_password = "8QZ9JD1zLvPR3yHf"
	redis_section          = 0
)

var kacp = keepalive.ClientParameters{
	Time:                10 * time.Second, // send pings every 10 seconds if there is no activity
	Timeout:             time.Second,      // wait 1 second for ping ack before considering the connection dead
	PermitWithoutStream: true,             // send pings even without active streams
}

func main() {
hujiebin's avatar
hujiebin committed
31
	// init redis
hujiebin's avatar
hujiebin committed
32
	rdb := redis.NewClient(&redis.Options{
hujiebin's avatar
hujiebin committed
33 34
		Addr:     config.GetConfigRedis().REDIS_HOST,
		Password: config.GetConfigRedis().REDIS_PASSWORD,
hujiebin's avatar
hujiebin committed
35 36 37 38 39 40 41 42 43 44
		DB:       redis_section,
	})

	result, err := rdb.Ping(context.Background()).Result()
	if err != nil {
		mylogrus.MyLog.Fatal(err)
	} else if result != "PONG" {
		mylogrus.MyLog.Fatalf("Invalid ping response %s", result)
	}

hujiebin's avatar
hujiebin committed
45 46 47 48 49 50 51 52 53 54
	// init redis cluster
	rdbCluster := redis.NewClient(&redis.Options{
		Addr:     config.GetConfigRedis().REDIS_CLUSTER_HOST,
		Password: config.GetConfigRedis().REDIS_CLUSTER_PASSWORD,
	})
	redisKey := fmt.Sprintf("service:userCenter")
	ipPorts, err := rdbCluster.ZRangeByScore(context.Background(), redisKey, &redis.ZRangeBy{
		Min: fmt.Sprintf("%d", time.Now().Add(-time.Second*15).Unix()), // 3倍心跳
		Max: "+inf",
	}).Result()
hujiebin's avatar
hujiebin committed
55
	if err != nil {
hujiebin's avatar
hujiebin committed
56 57 58
		failMsg := fmt.Sprintf("get service fail,svc:%v,err:%v", "userCenter", err)
		mylogrus.MyLog.Errorf(failMsg)
	} else if len(ipPorts) > 0 {
hujiebin's avatar
hujiebin committed
59
	}
hujiebin's avatar
hujiebin committed
60 61
	if len(ipPorts) <= 0 {
		ipPorts = []string{"127.0.0.1:50040"}
hujiebin's avatar
hujiebin committed
62 63
	}

hujiebin's avatar
hujiebin committed
64 65
	addresses := make([]resolver.Address, len(ipPorts))
	for i, s := range ipPorts {
hujiebin's avatar
hujiebin committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
		addresses[i].Addr = s
		mylogrus.MyLog.Infof("address : %s", s)
	}

	r := manual.NewBuilderWithScheme("hilo")
	r.InitialState(resolver.State{Addresses: addresses})

	userCenterAddr := fmt.Sprintf("%s:///usercenter", r.Scheme())

	// Set up addresses connection to the userCenter.
	conn, err := grpc.Dial(userCenterAddr,
		grpc.WithInsecure(),
		grpc.WithBlock(),
		grpc.WithKeepaliveParams(kacp),
		grpc.WithResolvers(r),
		grpc.WithDefaultServiceConfig("{ \"loadBalancingPolicy\": \"round_robin\" }"))

	if err != nil {
		mylogrus.MyLog.Fatalf("did not connect: %v", err)
	}
	//defer conn.Close()
	userClient := userCenter.NewUserClient(conn)
	if userClient == nil {
		mylogrus.MyLog.Fatalln("userClient null")
	}
	algo.Start(userClient, rdb)

	select {}
}