Commit fe176bc1 authored by hujiebin's avatar hujiebin

feat:新的代码

parent ae46e864
......@@ -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
......
......@@ -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=
......
......@@ -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) {
......
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]
}
......@@ -10,6 +10,6 @@ func TestTestCommon(t *testing.T) {
}
func TestJwt(t *testing.T) {
token, _ := jwt.GenerateToken(2781, "6373ff5c318c4057886a669b5c9b5301", "hiloApi")
token, _ := jwt.GenerateToken(72, "e95ca43da9d545c4937fa933e3348961", "hiloApi")
t.Logf(token)
}
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
}
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