Commit 6baf3b34 authored by hujiebin's avatar hujiebin

Merge branch 'feature/consul-redis' into 'master'

Feature/consul redis

See merge request !2
parents 3f0994f8 653ab059
This diff is collapsed.
...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code ...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS] [REDIS]
REDIS_HOST=47.244.34.27:6379 REDIS_HOST=47.244.34.27:6379
REDIS_PASSWORD=8QZ9JD1zLvPR3yHf REDIS_PASSWORD=8QZ9JD1zLvPR3yHf
REDIS_CLUSTER_HOST=47.244.34.27:6379
REDIS_CLUSTER_PASSWORD=8QZ9JD1zLvPR3yHf
[JWT] [JWT]
SECRET=hilo1632 SECRET=hilo1632
ISSUER_API=hiloApi ISSUER_API=hiloApi
......
...@@ -4,7 +4,10 @@ go 1.17 ...@@ -4,7 +4,10 @@ go 1.17
require ( require (
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/color v1.9.0 // indirect github.com/fatih/color v1.9.0 // indirect
github.com/go-redis/redis/v8 v8.3.3 // indirect
github.com/golang/protobuf v1.4.3 // indirect github.com/golang/protobuf v1.4.3 // indirect
github.com/gorilla/websocket v1.4.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/consul/api v1.7.0 // indirect github.com/hashicorp/consul/api v1.7.0 // indirect
...@@ -25,9 +28,10 @@ require ( ...@@ -25,9 +28,10 @@ require (
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 // indirect github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 // indirect
github.com/satori/go.uuid v1.2.0 // indirect github.com/satori/go.uuid v1.2.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect github.com/sirupsen/logrus v1.7.0 // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect go.opentelemetry.io/otel v0.13.0 // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0 // indirect
golang.org/x/text v0.3.2 // indirect golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/grpc v1.42.0 // indirect google.golang.org/grpc v1.42.0 // indirect
google.golang.org/protobuf v1.25.0 // indirect google.golang.org/protobuf v1.25.0 // indirect
......
This diff is collapsed.
...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code ...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS] [REDIS]
REDIS_HOST=47.244.34.27:6379 REDIS_HOST=47.244.34.27:6379
REDIS_PASSWORD=8QZ9JD1zLvPR3yHf REDIS_PASSWORD=8QZ9JD1zLvPR3yHf
REDIS_CLUSTER_HOST=47.244.34.27:6379
REDIS_CLUSTER_PASSWORD=8QZ9JD1zLvPR3yHf
[JWT] [JWT]
SECRET=hilo1632 SECRET=hilo1632
ISSUER_API=hiloApi ISSUER_API=hiloApi
......
...@@ -4,8 +4,9 @@ import ( ...@@ -4,8 +4,9 @@ import (
"context" "context"
"flag" "flag"
"fmt" "fmt"
"github.com/go-redis/redis/v8"
"hilo-userProxy/common" "hilo-userProxy/common"
"hilo-userProxy/common/consul" "hilo-userProxy/common/config"
"net" "net"
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
...@@ -22,7 +23,6 @@ import ( ...@@ -22,7 +23,6 @@ import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
consulapi "github.com/hashicorp/consul/api"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"google.golang.org/grpc" "google.golang.org/grpc"
...@@ -85,6 +85,7 @@ func getShortToken(token string) string { ...@@ -85,6 +85,7 @@ func getShortToken(token string) string {
return s[len(s)-1] return s[len(s)-1]
} }
} }
func serverWebsocket(w http.ResponseWriter, r *http.Request) { func serverWebsocket(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil) ws, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
...@@ -744,48 +745,65 @@ var listenPort = flag.String("port", "8081", "listen port") ...@@ -744,48 +745,65 @@ var listenPort = flag.String("port", "8081", "listen port")
func main() { func main() {
flag.Parse() flag.Parse()
client, err := consulapi.NewClient(consulapi.DefaultConfig()) //非默认情况下需要设置实际的参数 // init redis cluster
if client == nil { rdbCluster := redis.NewClient(&redis.Options{
mylogrus.MyLog.Fatalln("Fail to get consul client.") Addr: config.GetConfigRedis().REDIS_CLUSTER_HOST,
} Password: config.GetConfigRedis().REDIS_CLUSTER_PASSWORD,
})
myIp, myNodeName := consul.GetAgentInfo(client)
myLocalIp, err := common.GetClientIpV2() myLocalIp, err := common.GetClientIpV2()
if err != nil { if err != nil {
mylogrus.MyLog.Fatal(err) mylogrus.MyLog.Fatal(err)
} }
mylogrus.MyLog.Infof("myIp is %s, myNodeName: %s, localIp is %s", myIp, myNodeName, myLocalIp)
cataLog := client.Catalog()
if cataLog == nil {
mylogrus.MyLog.Fatalln("No catalog.")
}
services, _, err := cataLog.Service(userCenterConsulName, "", nil)
if err != nil {
mylogrus.MyLog.Fatalln(err)
}
if len(services) == 0 {
mylogrus.MyLog.Fatalln("userCenter not found.")
}
var addrs []string
bd := &Builder{addrs: map[string][]string{"/api": {userCenterAddr}}} bd := &Builder{addrs: map[string][]string{"/api": {userCenterAddr}}}
for _, s := range services { if rdbCluster != nil {
addrs = append(addrs, fmt.Sprintf("%s:%d", s.ServiceAddress, s.ServicePort)) redisKey := fmt.Sprintf("service:userCenter")
} ipPorts, err := rdbCluster.ZRangeByScore(context.Background(), redisKey, &redis.ZRangeBy{
if len(addrs) > 0 { Min: fmt.Sprintf("%d", time.Now().Add(-time.Second*15).Unix()), // 3倍心跳
bd = &Builder{addrs: map[string][]string{"/api": addrs}} Max: "+inf",
}).Result()
if err != nil {
failMsg := fmt.Sprintf("get service fail,svc:%v,err:%v", "userCenter", err)
mylogrus.MyLog.Errorf(failMsg)
} else if len(ipPorts) > 0 {
bd = &Builder{addrs: map[string][]string{"/api": ipPorts}}
userCenterAddr = "uc:///api" userCenterAddr = "uc:///api"
mylogrus.MyLog.Infof("userCenterAddr:%v,addr:%v", userCenterAddr, ipPorts)
}
} }
mylogrus.MyLog.Infof("userCenterAddr:%v,addr:%v", userCenterAddr, addrs)
// 服务发现 // 服务发现
resolver.Register(bd) resolver.Register(bd)
go func() { go func() {
consul.RegisterWatcher(userCenterConsulName, func(addr []string) { //consul.RegisterWatcher(userCenterConsulName, func(addr []string) {
if len(addr) > 0 { // if len(addr) > 0 {
bd.UpdateState(addr) // 更新新的注册名 // bd.UpdateState(addr) // 更新新的注册名
// }
//})
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
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()
if err != nil {
failMsg := fmt.Sprintf("get service fail,svc:%v,err:%v", "userCenter", err)
mylogrus.MyLog.Errorf(failMsg)
break
}
if len(ipPorts) <= 0 {
failMsg := fmt.Sprintf("get service empty,svc:%v,err:%v", "userCenter", err)
mylogrus.MyLog.Errorf(failMsg)
break
} else {
bd.UpdateState(ipPorts) // 更新新的注册名
}
}
} }
})
}() }()
// Set up a connection to the userCenter. // Set up a connection to the userCenter.
......
...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code ...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS] [REDIS]
REDIS_HOST=r-eb3btxn8vfdsuwdbuf.redis.dubai.rds.aliyuncs.com:6379 REDIS_HOST=r-eb3btxn8vfdsuwdbuf.redis.dubai.rds.aliyuncs.com:6379
REDIS_PASSWORD= REDIS_PASSWORD=
REDIS_CLUSTER_HOST=r-eb3yt6k8zgxs62kjjs.redis.dubai.rds.aliyuncs.com:6379
REDIS_CLUSTER_PASSWORD=
[JWT] [JWT]
SECRET=hilo1504 SECRET=hilo1504
ISSUER_API=hiloApi ISSUER_API=hiloApi
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment