diff --git a/README.md b/README.md index 4f9f63c..48559fc 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Important: Now need go1.9+ version support. Document: https://www.kancloud.cn/devfeel/dotweb/346608 -CookBook: https://www.kancloud.cn/devfeel/dotweb/439314 +Guide: https://github.com/devfeel/dotweb/blob/master/docs/GUIDE.md [![Gitter](https://badges.gitter.im/devfeel/dotweb.svg)](https://gitter.im/devfeel-dotweb/wechat) [![GoDoc](https://godoc.org/github.com/devfeel/dotweb?status.svg)](https://godoc.org/github.com/devfeel/dotweb) diff --git a/example/main.go b/example/main.go index 0862b40..24138ed 100644 --- a/example/main.go +++ b/example/main.go @@ -5,10 +5,10 @@ import ( "fmt" "github.com/devfeel/dotweb" "github.com/devfeel/dotweb/framework/exception" - "github.com/devfeel/dotweb/session" "net/http" "strconv" "time" + "github.com/devfeel/dotweb/session" ) func main() { @@ -46,7 +46,7 @@ func main() { //runtime mode app.HttpServer.SetSessionConfig(session.NewDefaultRuntimeConfig()) //redis mode - //app.HttpServer.SetSessionConfig(session.NewDefaultRedisConfig("192.168.8.175:6379", "")) + //app.HttpServer.SetSessionConfig(session.NewDefaultRedisConfig("redis://192.168.8.175:6379/0")) app.HttpServer.SetEnabledDetailRequestData(true) diff --git a/framework/redis/redisutil.go b/framework/redis/redisutil.go index 9ba5d3e..e81cab2 100644 --- a/framework/redis/redisutil.go +++ b/framework/redis/redisutil.go @@ -516,6 +516,14 @@ func (rc *RedisClient) SUnionStore(destination string, key ...interface{})(int, //****************** 全局操作 *********************** +// Ping 测试一个连接是否可用 +func (rc *RedisClient) Ping()(string,error){ + conn := rc.pool.Get() + defer conn.Close() + val, err:=redis.String(conn.Do("PING")) + return val, err +} + // DBSize 返回当前数据库的 key 的数量 func (rc *RedisClient) DBSize()(int64, error){ conn := rc.pool.Get() diff --git a/framework/redis/redisutil_test.go b/framework/redis/redisutil_test.go index b8ac1e5..3b71963 100644 --- a/framework/redis/redisutil_test.go +++ b/framework/redis/redisutil_test.go @@ -1 +1,17 @@ package redisutil + +import ( + "testing" +) + +const redisServerURL = "redis://192.168.8.175:6379/0" + +func TestRedisClient_Ping(t *testing.T) { + redisClient := GetRedisClient(redisServerURL) + val, err := redisClient.Ping() + if err != nil{ + t.Error(err) + }else{ + t.Log(val) + } +} \ No newline at end of file diff --git a/session/session.go b/session/session.go index 2c7ae74..3be600c 100644 --- a/session/session.go +++ b/session/session.go @@ -7,6 +7,7 @@ import ( "net/url" "strconv" "time" + "fmt" ) const ( @@ -51,12 +52,16 @@ func GetSessionStore(config *StoreConfig) SessionStore { case SessionMode_Runtime: return NewRuntimeStore(config) case SessionMode_Redis: - return NewRedisStore(config) + store, err := NewRedisStore(config) + if err != nil{ + panic(fmt.Sprintf("redis session [%v] ping error -> %v", config.StoreName, err.Error())) + }else{ + return store + } default: panic("not support session store -> " + config.StoreName) } return nil - } //create new store with default config and use runtime store diff --git a/session/store_redis.go b/session/store_redis.go index 9b53fa2..bf02f57 100644 --- a/session/store_redis.go +++ b/session/store_redis.go @@ -23,12 +23,15 @@ func getRedisKey(key string) string { } //create new redis store -func NewRedisStore(config *StoreConfig) *RedisStore { - return &RedisStore{ +func NewRedisStore(config *StoreConfig) (*RedisStore, error){ + store := &RedisStore{ lock: new(sync.RWMutex), serverIp: config.ServerIP, maxlifetime: config.Maxlifetime, } + redisClient := redisutil.GetRedisClient(store.serverIp) + _, err:=redisClient.Ping() + return store, err } // SessionRead get session state by sessionId diff --git a/version.MD b/version.MD index 324e107..4233894 100644 --- a/version.MD +++ b/version.MD @@ -1,5 +1,9 @@ ## dotweb版本记录: +#### Version 1.4.9.5 +* framewrok.RedisClient增加Ping接口,用于检查服务是否可用 +* GetSessionStore增加Redis ServerUrl的ping检查,若不可用直接panic异常信息 +* 2018-05-23 13:30 #### Version 1.4.9.4 * 调整: dotweb.Classic移除自动UseRequestLog逻辑