diff --git a/server/chat-center/Web/rpc/client/init.go b/server/chat-center/Web/rpc/client/init.go index 18b0c9b..7ba5bb7 100644 --- a/server/chat-center/Web/rpc/client/init.go +++ b/server/chat-center/Web/rpc/client/init.go @@ -2,20 +2,66 @@ package client import ( "chat-center/conf" + "fmt" + "github.com/nacos-group/nacos-sdk-go/v2/clients" + "github.com/nacos-group/nacos-sdk-go/v2/common/constant" + "github.com/nacos-group/nacos-sdk-go/v2/vo" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "log" - "strconv" ) var ( Conn *grpc.ClientConn ) -func InitGRPCClient() { - // 建立到 gRPC 服务器的连接。 - target := ":" + strconv.Itoa(conf.GRPCPort) - conn, err := grpc.Dial(target, grpc.WithTransportCredentials(insecure.NewCredentials())) +func Init() { + InitChatRPC() +} + +func InitChatRPC() { + // 创建clientConfig + clientConfig := constant.ClientConfig{ + NamespaceId: conf.NacosNamespaceId, // 如果需要支持多namespace,我们可以创建多个client,它们有不同的NamespaceId。当namespace是public时,此处填空字符串。 + TimeoutMs: 5000, + NotLoadCacheAtStart: true, + LogDir: "/tmp/nacos/log", + CacheDir: "/tmp/nacos/cache", + LogLevel: "debug", + } + + // 创建ServerConfig + serverConfigs := []constant.ServerConfig{ + { + IpAddr: conf.NacosAddress, + ContextPath: "/nacos", + Port: uint64(conf.NacosPort), + Scheme: "http", + }, + } + + // 创建服务发现客户端 + nacosClient, err := clients.NewNamingClient( + vo.NacosClientParam{ + ClientConfig: &clientConfig, + ServerConfigs: serverConfigs, + }) + + if err != nil { + log.Fatalf("failed to create Nacos client: %v", err) + } + + // 获取 gRPC 服务实例信息 + serviceName := conf.NacosServerName + instances, err := nacosClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{ + ServiceName: serviceName, + GroupName: conf.NacosGroup, + }) + if err != nil { + log.Fatalf("failed to get service instances: %v", err) + } + + conn, err := grpc.Dial(fmt.Sprintf("%s:%d", instances.Ip, instances.Port), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { log.Fatalf("connect failed: %v", err) } diff --git a/server/chat-center/Web/rpc/server/init.go b/server/chat-center/Web/rpc/server/init.go index 03363cc..6fac023 100644 --- a/server/chat-center/Web/rpc/server/init.go +++ b/server/chat-center/Web/rpc/server/init.go @@ -2,7 +2,6 @@ package server import ( "chat-center/conf" - "fmt" "github.com/OrionLi/douyin-backend/pkg/pb" "github.com/nacos-group/nacos-sdk-go/v2/clients" "github.com/nacos-group/nacos-sdk-go/v2/common/constant" @@ -38,44 +37,39 @@ func InitGRPCServer() { func RegisterNacos() { // 创建clientConfig clientConfig := constant.ClientConfig{ - NamespaceId: conf.NacosNamespaceId, // 如果需要支持多namespace,我们可以创建多个client,它们有不同的NamespaceId。当namespace是public时,此处填空字符串。 TimeoutMs: 5000, NotLoadCacheAtStart: true, - LogDir: "/tmp/nacos/log", - CacheDir: "/tmp/nacos/cache", LogLevel: "debug", } // 创建ServerConfig serverConfigs := []constant.ServerConfig{ { - IpAddr: conf.NacosAddress, - ContextPath: "/nacos", - Port: uint64(conf.NacosPort), - Scheme: "http", + IpAddr: conf.NacosAddress, + Port: uint64(conf.NacosPort), }, } // 创建服务发现客户端 - nacosClient, err := clients.CreateNamingClient(map[string]interface{}{ - "serverConfigs": serverConfigs, - "clientConfig": clientConfig, - }) + nacosClient, err := clients.NewNamingClient( + vo.NacosClientParam{ + ClientConfig: &clientConfig, + ServerConfigs: serverConfigs, + }) + + if err != nil { + log.Fatalf("failed to create Nacos client: %v", err) + } // 注册 gRPC 服务到 Nacos instance := &vo.RegisterInstanceParam{ - Ip: conf.GRPCAddress, // 设置你的服务器的 IP 地址 - Port: uint64(conf.GRPCPort), - Metadata: map[string]string{ - "protocol": "grpc", - "healthCheckType": "TCP", // 使用 TCP 健康检查 - "healthCheckPort": fmt.Sprintf("%d", uint64(conf.GRPCPort)), - "healthCheckPath": "", // 空路径 - "healthCheckInterval": "10s", // 健康检查间隔 - }, - ClusterName: "default", - ServiceName: conf.NacosServerName, - GroupName: conf.NacosGroup, + Ip: conf.GRPCAddress, // 设置你的服务器的 IP 地址 + Port: uint64(conf.GRPCPort), + ServiceName: conf.NacosServerName, // 设置服务的名称 + GroupName: conf.NacosGroup, // 设置服务的分组 + Weight: 10, // 权重为10 + Enable: true, // 设置实例为可用状态 + Healthy: true, // 设置实例为健康状态 } _, err = nacosClient.RegisterInstance(*instance) diff --git a/server/chat-center/go.mod b/server/chat-center/go.mod index 291ddfb..063f704 100644 --- a/server/chat-center/go.mod +++ b/server/chat-center/go.mod @@ -7,6 +7,7 @@ require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/elastic/go-elasticsearch/v7 v7.17.10 github.com/gin-gonic/gin v1.9.1 + github.com/nacos-group/nacos-sdk-go v1.1.4 github.com/nacos-group/nacos-sdk-go/v2 v2.2.3 github.com/sirupsen/logrus v1.9.3 github.com/sony/sonyflake v1.2.0 @@ -24,6 +25,7 @@ require ( github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-errors/errors v1.0.1 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.14.0 // indirect diff --git a/server/chat-center/go.sum b/server/chat-center/go.sum index b4c0a62..355f55b 100644 --- a/server/chat-center/go.sum +++ b/server/chat-center/go.sum @@ -39,8 +39,6 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7 github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/OrionLi/douyin-backend v1.0.5 h1:Iz2j5g24AjQBiCTxphBfzr0ckLl7Ql/XA5EMxGpxaXQ= -github.com/OrionLi/douyin-backend v1.0.5/go.mod h1:4ATeodTy47KXveKpBOWLRpFxJethsER1nLgzcmAcEy8= github.com/OrionLi/douyin-backend v1.0.9 h1:HWuyQb7uXpFQ0w6BN2iPa8H9HMWaCwASx+ixygQmNjU= github.com/OrionLi/douyin-backend v1.0.9/go.mod h1:4ATeodTy47KXveKpBOWLRpFxJethsER1nLgzcmAcEy8= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -48,6 +46,7 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= +github.com/aliyun/alibaba-cloud-sdk-go v1.61.18/go.mod h1:v8ESoHo4SyHmuB4b1tJqDHxfTGEciD+yhvOU/5s1Rfk= github.com/aliyun/alibaba-cloud-sdk-go v1.61.1704 h1:PpfENOj/vPfhhy9N2OFRjpue0hjM5XqAp2thFmkXXIk= github.com/aliyun/alibaba-cloud-sdk-go v1.61.1704/go.mod h1:RcDobYh8k5VP6TNybz9m++gL3ijVI5wueVr0EM10VsU= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= @@ -98,6 +97,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w= +github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -182,6 +183,7 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= @@ -199,6 +201,7 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -233,6 +236,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/nacos-group/nacos-sdk-go v1.1.4 h1:qyrZ7HTWM4aeymFfqnbgNRERh7TWuER10pCB7ddRcTY= +github.com/nacos-group/nacos-sdk-go v1.1.4/go.mod h1:cBv9wy5iObs7khOqov1ERFQrCuTR4ILpgaiaVMxEmGI= github.com/nacos-group/nacos-sdk-go/v2 v2.2.3 h1:sUQx4f1bXDeeOOEQZjGAitzxYApbYY9fVDbxVCaBW+I= github.com/nacos-group/nacos-sdk-go/v2 v2.2.3/go.mod h1:UL4U89WYdnyajgKJUMpuT1Rr6iNmbjrxOO40JRgtA00= github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= @@ -273,6 +278,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/sony/sonyflake v1.2.0 h1:Pfr3A+ejSg+0SPqpoAmQgEtNDAhc2G1SUYk205qVMLQ= github.com/sony/sonyflake v1.2.0/go.mod h1:LORtCywH/cq10ZbyfhKrHYgAUGH7mOBa76enV9txy/Y= github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM= @@ -317,14 +324,18 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= +go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= +go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8= go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= +go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= @@ -509,6 +520,7 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3 golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= @@ -518,6 +530,8 @@ golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgw golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191113191852-77e3bb0ad9e7/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191115202509-3a792d9c32b2/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -658,6 +672,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/server/chat-center/logs/2023-08-24.log b/server/chat-center/logs/2023-08-24.log index bca1087..782e0f1 100644 --- a/server/chat-center/logs/2023-08-24.log +++ b/server/chat-center/logs/2023-08-24.log @@ -17,3 +17,5 @@ [info]: 2023/08/24 10:26:08 [URL:/douyin/message/chat?token=123&to_user_id=1&pre_msg_time=0 host:localhost:9421 method:GET remoteIp:[::1]:65145] [info]: 2023/08/24 10:26:46 [URL:/douyin/message/action host:localhost:9421 method:POST remoteIp:[::1]:65191] [info]: 2023/08/24 10:26:50 [URL:/douyin/message/chat?token=123&to_user_id=1&pre_msg_time=0 host:localhost:9421 method:GET remoteIp:[::1]:65191] +[info]: 2023/08/24 13:05:42 [URL:/douyin/message/chat?token=123&to_user_id=1&pre_msg_time=0 host:localhost:9421 method:GET remoteIp:[::1]:63591] +[info]: 2023/08/24 13:06:47 [URL:/douyin/message/chat?token=123&to_user_id=1&pre_msg_time=0 host:localhost:9421 method:GET remoteIp:[::1]:63730] diff --git a/server/chat-center/main.go b/server/chat-center/main.go index b3bd4d0..cbd2eb8 100644 --- a/server/chat-center/main.go +++ b/server/chat-center/main.go @@ -13,7 +13,7 @@ func main() { // 初始化配置 conf.InitConf() // 初始化grpc - client.InitGRPCClient() + client.Init() defer client.ClosConn() // 初始化gin r := gin.Default() diff --git a/server/video-center/cache/favoriteCache.go b/server/video-center/cache/favoriteCache.go new file mode 100644 index 0000000..be24421 --- /dev/null +++ b/server/video-center/cache/favoriteCache.go @@ -0,0 +1,49 @@ +package cache + +import ( + "context" + "fmt" + "github.com/go-redis/redis/v8" + "time" + "video-center/pkg/util" +) + +func ActionFavoriteCache(videoId int64, actionType int32) error { + lockKey := fmt.Sprintf("lock:fav:vid:%d", videoId) + favoriteKey := fmt.Sprintf("favorite:%d", videoId) + lock, err := RedisLock(fmt.Sprintf(lockKey, videoId), 3*time.Second) + if err != nil || !lock { + return err + } + defer RedisUnlock(fmt.Sprintf(lockKey, videoId)) + // 查询 Redis 中的值 + favoriteCount, err := RedisClient.Get(context.Background(), favoriteKey).Int64() + if err != nil { + if err != redis.Nil { + return err + } + // TODO 查询mysql中的值 + } + favoriteCount++ + RedisClient.Set(context.Background(), favoriteKey, favoriteCount, 3*time.Minute) + // TODO 异步更新mysql中的值 + return nil +} + +// RedisLock redis分布式锁 +func RedisLock(lockKey string, lockTimeout time.Duration) (bool, error) { + lockAcquired, err := RedisClient.SetNX(context.Background(), lockKey, "lock-true", lockTimeout).Result() + if err != nil { + return false, err + } + + return lockAcquired, nil +} + +// RedisUnlock redis分布式锁解锁 +func RedisUnlock(lockKey string) { + _, err := RedisClient.Del(context.Background(), lockKey).Result() + if err != nil { + util.LogrusObj.Error(", Unlock failed", err) + } +}