Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-common
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-common
Commits
fe176bc1
Commit
fe176bc1
authored
Feb 17, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:新的代码
parent
ae46e864
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
131 additions
and
16 deletions
+131
-16
go.mod
go.mod
+1
-0
go.sum
go.sum
+2
-0
finance.go
rpc/finance.go
+0
-15
user.go
rpc/user.go
+107
-0
utils_test.go
script/utils/utils_test.go
+1
-1
version.go
utils/version.go
+20
-0
No files found.
go.mod
View file @
fe176bc1
...
...
@@ -17,6 +17,7 @@ require (
github.com/hashicorp/go-hclog v0.12.0 // indirect
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.0 // indirect
github.com/hashicorp/serf v0.9.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
...
...
go.sum
View file @
fe176bc1
...
...
@@ -86,6 +86,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
...
...
rpc/finance.go
View file @
fe176bc1
...
...
@@ -9,8 +9,6 @@ import (
"git.hilo.cn/hilo-common/resource/mysql"
"github.com/hashicorp/consul/api"
"math/rand"
"net"
"net/http"
)
const
(
...
...
@@ -20,21 +18,8 @@ const (
)
var
financeServerHost
=
[]
string
{
defaultFinanceServerAddr
}
var
financeTransport
*
http
.
Transport
func
init
()
{
financeTransport
=
&
http
.
Transport
{
Proxy
:
http
.
ProxyFromEnvironment
,
MaxIdleConnsPerHost
:
defaultMaxIdleConnsPerHost
,
MaxIdleConns
:
defaultMaxIdleConns
,
DialContext
:
(
&
net
.
Dialer
{
Timeout
:
defaultDialTimeout
,
KeepAlive
:
defaultKeepAliveTimeout
,
})
.
DialContext
,
IdleConnTimeout
:
defaultIdleConnTimeout
,
ExpectContinueTimeout
:
defaultExpectContinueTimeout
,
DisableKeepAlives
:
false
,
}
go
func
()
{
address
:=
api
.
DefaultConfig
()
.
Address
// 用consul api的default config
if
err
:=
consul
.
RegisterWatcher
(
"services"
,
nil
,
address
,
func
(
serviceStatus
map
[
string
]
map
[
string
][]
string
)
{
...
...
rpc/user.go
0 → 100644
View file @
fe176bc1
package
rpc
import
(
"encoding/json"
"fmt"
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/mylogrus"
"git.hilo.cn/hilo-common/resource/consul"
"git.hilo.cn/hilo-common/resource/mysql"
"github.com/hashicorp/consul/api"
"math/rand"
)
const
(
defaultUserConsulName
=
"hiloUser"
defaultUserServerScheme
=
"http"
defaultUserServerAddr
=
"127.0.0.1:9040"
// 默认内网转发,本地回环
)
var
UserServerHost
=
[]
string
{
defaultUserServerAddr
}
func
init
()
{
go
func
()
{
address
:=
api
.
DefaultConfig
()
.
Address
// 用consul api的default config
if
err
:=
consul
.
RegisterWatcher
(
"services"
,
nil
,
address
,
func
(
serviceStatus
map
[
string
]
map
[
string
][]
string
)
{
if
statusAddrs
,
ok
:=
serviceStatus
[
defaultUserConsulName
];
ok
{
healthAddrs
,
_
:=
statusAddrs
[
api
.
HealthPassing
]
l
:=
len
(
healthAddrs
)
if
l
>
0
{
mylogrus
.
MyLog
.
Infof
(
"consul service update state:%v-%v"
,
defaultUserConsulName
,
healthAddrs
)
UserServerHost
=
healthAddrs
}
else
{
mylogrus
.
MyLog
.
Warnf
(
"consul service update local state:%v-%v"
,
defaultUserConsulName
,
defaultUserServerAddr
)
UserServerHost
=
[]
string
{
defaultUserServerAddr
}
// 有其他问题都用默认的
}
for
status
:=
range
statusAddrs
{
if
status
==
api
.
HealthPassing
{
continue
}
mylogrus
.
MyLog
.
Warnf
(
"consul service wrong state:%v-%v-%v"
,
defaultUserConsulName
,
status
,
statusAddrs
[
status
])
}
}
});
err
!=
nil
{
mylogrus
.
MyLog
.
Errorf
(
"启动 consul 的watch监控失败"
)
}
}()
}
type
CvUserLevel
struct
{
UserId
mysql
.
ID
`json:"userId"`
// 用户id
WealthUserGrade
uint32
`json:"wealthUserGrade"`
//财富等级
CharmUserGrade
uint32
`json:"charmUserGrade"`
//魅力等级
}
// 获取用户等级
func
GetUserLevel
(
model
*
domain
.
Model
,
userId
mysql
.
ID
)
(
CvUserLevel
,
error
)
{
level
,
err
:=
MGetUserLevel
(
model
,
[]
mysql
.
ID
{
userId
})
if
err
!=
nil
{
return
CvUserLevel
{},
nil
}
return
level
[
userId
],
nil
}
// 批量获取用户等级
func
MGetUserLevel
(
model
*
domain
.
Model
,
userIds
[]
mysql
.
ID
)
(
map
[
mysql
.
ID
]
CvUserLevel
,
error
)
{
type
Response
struct
{
Code
int
`json:"code"`
Message
string
`json:"message"`
Data
map
[
mysql
.
ID
]
CvUserLevel
}
var
res
=
make
(
map
[
mysql
.
ID
]
CvUserLevel
)
if
len
(
userIds
)
<=
0
{
return
res
,
nil
}
var
userIdsStr
[]
string
for
_
,
userId
:=
range
userIds
{
userIdsStr
=
append
(
userIdsStr
,
fmt
.
Sprintf
(
"%d"
,
userId
))
}
_url
:=
fmt
.
Sprintf
(
"%v://%v/inner/user/levels"
,
defaultUserServerScheme
,
getUserHost
())
resp
,
err
:=
HttpGet
(
model
,
_url
,
nil
,
map
[
string
][]
string
{
"ids"
:
userIdsStr
,
})
if
err
!=
nil
{
model
.
Log
.
Errorf
(
"MGetUserLevel fail:%v"
,
err
)
return
res
,
err
}
response
:=
new
(
Response
)
if
err
=
json
.
Unmarshal
(
resp
,
response
);
err
!=
nil
{
model
.
Log
.
Errorf
(
"MGetUserLevel json fail:%v"
,
err
)
return
res
,
err
}
for
userId
,
level
:=
range
response
.
Data
{
res
[
userId
]
=
CvUserLevel
{
UserId
:
level
.
UserId
,
WealthUserGrade
:
level
.
WealthUserGrade
,
CharmUserGrade
:
level
.
CharmUserGrade
,
}
}
return
res
,
nil
}
func
getUserHost
()
string
{
l
:=
len
(
UserServerHost
)
r
:=
rand
.
Intn
(
l
)
// 随机一个
mylogrus
.
MyLog
.
Infof
(
"getHostUser:%v---%v"
,
r
,
UserServerHost
[
r
])
return
UserServerHost
[
r
]
}
script/utils/utils_test.go
View file @
fe176bc1
...
...
@@ -10,6 +10,6 @@ func TestTestCommon(t *testing.T) {
}
func
TestJwt
(
t
*
testing
.
T
)
{
token
,
_
:=
jwt
.
GenerateToken
(
2781
,
"6373ff5c318c4057886a669b5c9b530
1"
,
"hiloApi"
)
token
,
_
:=
jwt
.
GenerateToken
(
72
,
"e95ca43da9d545c4937fa933e334896
1"
,
"hiloApi"
)
t
.
Logf
(
token
)
}
utils/version.go
0 → 100644
View file @
fe176bc1
package
utils
import
"github.com/hashicorp/go-version"
// 使用注意:需要新增包依赖:github.com/hashicorp/go-version
// 用法:
// CompareVersion(cv," > 3.2.0")
// CompareVersion(cv," = 1.0.0")
func
CompareVersion
(
v
string
,
con
string
)
(
bool
,
error
)
{
v1
,
err
:=
version
.
NewVersion
(
v
)
if
err
!=
nil
{
return
false
,
err
}
constraints
,
err
:=
version
.
NewConstraint
(
con
)
if
err
!=
nil
{
return
false
,
err
}
return
constraints
.
Check
(
v1
),
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment