Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-userProxy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hujiebin
hilo-userProxy
Compare Revisions
master...feature/consul-redis
Source
feature/consul-redis
Select Git revision
...
Target
master
Select Git revision
Compare
Commits (2)
feat:userProxy接入
· f6381cc9
hujiebin
authored
Oct 23, 2023
f6381cc9
Update go.sum
· 7a5a8970
hujiebin
authored
Oct 23, 2023
7a5a8970
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
107 additions
and
497 deletions
+107
-497
config.go
common/config/config.go
+8
-462
debug.ini
debug.ini
+2
-0
go.mod
go.mod
+7
-3
go.sum
go.sum
+36
-0
local.ini
local.ini
+2
-0
main.go
main.go
+50
-32
release.ini
release.ini
+2
-0
No files found.
common/config/config.go
View file @
7a5a8970
This diff is collapsed.
Click to expand it.
debug.ini
View file @
7a5a8970
...
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS]
REDIS_HOST
=
47.244.34.27:6379
REDIS_PASSWORD
=
8QZ9JD1zLvPR3yHf
REDIS_CLUSTER_HOST
=
47.244.34.27:6379
REDIS_CLUSTER_PASSWORD
=
8QZ9JD1zLvPR3yHf
[JWT]
SECRET
=
hilo1632
ISSUER_API
=
hiloApi
...
...
go.mod
View file @
7a5a8970
...
...
@@ -4,7 +4,10 @@ go 1.17
require (
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/go-redis/redis/v8 v8.3.3 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/consul/api v1.7.0 // indirect
...
...
@@ -25,9 +28,10 @@ require (
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
golang.org/x/text v0.3.2 // indirect
go.opentelemetry.io/otel v0.13.0 // indirect
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0 // 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/grpc v1.42.0 // indirect
google.golang.org/protobuf v1.25.0 // indirect
...
...
go.sum
View file @
7a5a8970
This diff is collapsed.
Click to expand it.
local.ini
View file @
7a5a8970
...
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS]
REDIS_HOST
=
47.244.34.27:6379
REDIS_PASSWORD
=
8QZ9JD1zLvPR3yHf
REDIS_CLUSTER_HOST
=
47.244.34.27:6379
REDIS_CLUSTER_PASSWORD
=
8QZ9JD1zLvPR3yHf
[JWT]
SECRET
=
hilo1632
ISSUER_API
=
hiloApi
...
...
main.go
View file @
7a5a8970
...
...
@@ -4,8 +4,9 @@ import (
"context"
"flag"
"fmt"
"github.com/go-redis/redis/v8"
"hilo-userProxy/common"
"hilo-userProxy/common/con
sul
"
"hilo-userProxy/common/con
fig
"
"net"
"net/http"
_
"net/http/pprof"
...
...
@@ -22,7 +23,6 @@ import (
"github.com/golang/protobuf/proto"
"github.com/gorilla/websocket"
consulapi
"github.com/hashicorp/consul/api"
uuid
"github.com/satori/go.uuid"
log
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
...
...
@@ -85,6 +85,7 @@ func getShortToken(token string) string {
return
s
[
len
(
s
)
-
1
]
}
}
func
serverWebsocket
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
ws
,
err
:=
upgrader
.
Upgrade
(
w
,
r
,
nil
)
if
err
!=
nil
{
...
...
@@ -744,48 +745,65 @@ var listenPort = flag.String("port", "8081", "listen port")
func
main
()
{
flag
.
Parse
()
client
,
err
:=
consulapi
.
NewClient
(
consulapi
.
DefaultConfig
())
//非默认情况下需要设置实际的参数
if
client
==
nil
{
mylogrus
.
MyLog
.
Fatalln
(
"Fail to get consul client."
)
}
// init redis cluster
rdbCluster
:=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
config
.
GetConfigRedis
()
.
REDIS_CLUSTER_HOST
,
Password
:
config
.
GetConfigRedis
()
.
REDIS_CLUSTER_PASSWORD
,
})
myIp
,
myNodeName
:=
consul
.
GetAgentInfo
(
client
)
myLocalIp
,
err
:=
common
.
GetClientIpV2
()
if
err
!=
nil
{
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
}}}
for
_
,
s
:=
range
services
{
addrs
=
append
(
addrs
,
fmt
.
Sprintf
(
"%s:%d"
,
s
.
ServiceAddress
,
s
.
ServicePort
))
}
if
len
(
addrs
)
>
0
{
bd
=
&
Builder
{
addrs
:
map
[
string
][]
string
{
"/api"
:
addrs
}}
if
rdbCluster
!=
nil
{
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
)
}
else
if
len
(
ipPorts
)
>
0
{
bd
=
&
Builder
{
addrs
:
map
[
string
][]
string
{
"/api"
:
ipPorts
}}
userCenterAddr
=
"uc:///api"
mylogrus
.
MyLog
.
Infof
(
"userCenterAddr:%v,addr:%v"
,
userCenterAddr
,
ipPorts
)
}
}
mylogrus
.
MyLog
.
Infof
(
"userCenterAddr:%v,addr:%v"
,
userCenterAddr
,
addrs
)
// 服务发现
resolver
.
Register
(
bd
)
go
func
()
{
consul
.
RegisterWatcher
(
userCenterConsulName
,
func
(
addr
[]
string
)
{
if
len
(
addr
)
>
0
{
bd
.
UpdateState
(
addr
)
// 更新新的注册名
//consul.RegisterWatcher(userCenterConsulName, func(addr []string) {
// if len(addr) > 0 {
// 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.
...
...
release.ini
View file @
7a5a8970
...
...
@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS]
REDIS_HOST
=
r-eb3btxn8vfdsuwdbuf.redis.dubai.rds.aliyuncs.com:6379
REDIS_PASSWORD
=
REDIS_CLUSTER_HOST
=
r-eb3yt6k8zgxs62kjjs.redis.dubai.rds.aliyuncs.com:6379
REDIS_CLUSTER_PASSWORD
=
[JWT]
SECRET
=
hilo1504
ISSUER_API
=
hiloApi
...
...