Esempio n. 1
0
// NewRedisClient(): create a new redisClient with bns support
// Notice:
//    - if resolve bns error, c.Servers will be empty.
// Params:
//    - bnsName: string, bns name of redis server
//    - maxIdleCon: int, max idle connections in connection pool
//    - connectTimeout: int, connect redis server timeout, in ms
//    - readTimeout: int, read redis server timeout, in ms
//    - writeTimeout: int, write redis server timeout, in ms
//    - state: *module_state2.State, module state
// Returns:
//    - *redisClient: a new redis client
func NewRedisClient(bnsName, idc string, maxIdleCon int, connectTimeout, readTimeout, writeTimeout, idleTimeout int,
	state *module_state2.State) *RedisClient {
	var err error
	// create RedisClient
	c := &RedisClient{
		bnsClient: bns.NewClient(),

		// timeout in ms
		ConnectTimeout: time.Duration(connectTimeout) * time.Millisecond,
		ReadTimeout:    time.Duration(readTimeout) * time.Millisecond,
		WriteTimeout:   time.Duration(writeTimeout) * time.Millisecond,
		IdleTimeout:    time.Duration(idleTimeout) * time.Millisecond,
		// max idle connection
		MaxIdle: maxIdleCon,

		// module state
		state: state,
	}

	// if resolve bns error, c.Servers will be empty
	if c.Servers, err = bns.GetAddr(c.bnsClient, bnsName, idc); err != nil {
		//log.Logger.Warn("get instance for %s err %s", bnsName, err.Error())
	}
	go c.checkServerInstance(bnsName, idc) // goroutine to update bns

	// set connection pool
	c.pool = &redis.Pool{
		MaxIdle:     c.MaxIdle,
		IdleTimeout: c.IdleTimeout,
		Dial:        c.dial,
	}

	return c
}
Esempio n. 2
0
func main() {
	bnsName := "group.mmsda.MMS.all:proxy"
	bnsClient := bns.NewClient()
	Servers, err := bns.GetAddr(bnsClient, bnsName, "tc")
	if err != nil {

	}
	fmt.Println(Servers)

}
Esempio n. 3
0
func GetMMSDAServer() []string {
	mmsdaIdc := beego.AppConfig.DefaultString("mmsdaIdc", "tc")
	bnsIP := MemCache.Get(MMSDAConf)
	HostAdd, ok := bnsIP.([]string)
	beego.Notice("MemCache.Get", len(HostAdd))
	if !ok || len(HostAdd) == 0 {
		bnsClient := bns.NewClient()
		bnsName := beego.AppConfig.DefaultString(MMSDAConf, "group.mmsda.MMS.all:proxy")
		HostAdd, _ = bns.GetAddr(bnsClient, bnsName, mmsdaIdc)
		MemCache.Put(MMSDAConf, HostAdd, 120*time.Second)
		beego.Notice("BNS.Get", len(HostAdd))
	}
	return HostAdd
}