From ce1c23a992d1aba6ee9d1c078e4ac671183abd51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=96=87=E9=9D=99?= <1319697849@qq.com> Date: Sat, 14 Jun 2025 16:45:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E5=A4=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +- c2c.http | 6 ++-- conf/config.yaml | 1 - go.mod | 1 + go.sum | 3 ++ main.go | 68 ++++++++++++++++++++++++++---------------- pkg/consul/register.go | 24 +++++++-------- 7 files changed, 62 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf23fc7..c333a32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # 更新日志 -## [0.0.1-beta.3] - 2025.6.6-6.11 +## [0.0.1-beta.3] - 2025.6.6-6.14 ### Changed - 更改为redis存储sessionId - 修复查询历史上的今天切片越界问题 @@ -8,6 +8,7 @@ - 修改新闻title - 修改sessionId记录 - 修改播控 +- 增加一个http的入口 ## [0.0.1-beta.2] - 2025.5.29 ### Changed diff --git a/c2c.http b/c2c.http index ed91e8f..f2a9e5c 100644 --- a/c2c.http +++ b/c2c.http @@ -1,11 +1,11 @@ -POST http://localhost:19990/semantic/unisoundc2c/speech-nlu-parse +POST http://localhost:13302/semantic/unisoundc2c/speech-nlu-parse Content-Type: application/json { "ip": "14.215.222.17", "mac": "ece154a865eb", "mid": "11011", - "query":"今天星期几", + "query":"明天天气", "requestId": "123456111" } @@ -45,7 +45,7 @@ Content-Type: application/json }, "nluRet": { "asr_recongize": "", - "text": "今天星期几" + "text": "珠海在哪里" } }, "version": "v0" diff --git a/conf/config.yaml b/conf/config.yaml index b0580c0..62a70e7 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -7,7 +7,6 @@ Server: ConsulAddr: 172.28.124.105:8500 #consul的地址 Interval: 10 #健康检查间隔,单位:秒 Deregister: 1 #注销时间,相当于过期时间,单位:分钟 - HttpPort: 19990 UriPath: /semantic/unisoundc2c/speech-nlu-parse RabbitMq: diff --git a/go.mod b/go.mod index 6e992b5..a848c31 100644 --- a/go.mod +++ b/go.mod @@ -60,6 +60,7 @@ require ( github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/smartystreets/goconvey v1.8.0 // indirect + github.com/soheilhy/cmux v0.1.5 // indirect github.com/spf13/afero v1.9.5 // indirect github.com/spf13/cast v1.5.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index 82aa6b1..8f9d51c 100644 --- a/go.sum +++ b/go.sum @@ -360,6 +360,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd github.com/smartystreets/assertions v1.13.1 h1:Ef7KhSmjZcK6AVf9YbJdvPYG9avaF0ZxudX+ThRdWfU= github.com/smartystreets/goconvey v1.8.0 h1:Oi49ha/2MURE0WexF052Z0m+BNSGirfjg5RL+JXWq3w= github.com/smartystreets/goconvey v1.8.0/go.mod h1:EdX8jtrTIj26jmjCOVNMVSIYAtgexqXKHOXW2Dx9JLg= +github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js= +github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0= github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM= github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA= @@ -502,6 +504,7 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= diff --git a/main.go b/main.go index 65776f3..afc50c8 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/gin-gonic/gin" grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" + "github.com/soheilhy/cmux" "google.golang.org/grpc" "google.golang.org/grpc/health/grpc_health_v1" "google.golang.org/grpc/reflection" @@ -114,13 +115,23 @@ func main() { database.InitRedis() - // 初始化 gRPC + // 创建监听 lis, err := net.Listen("tcp", fmt.Sprintf(":%d", global.ServerSetting.Port)) if err != nil { log.Fatalf("[FATAL] Listen failed: %v", err) } - server := grpc.NewServer( + // 创建 cmux 多路复用器 + m := cmux.New(lis) + + // 匹配 gRPC 连接 + grpcLis := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc")) + + // 匹配 HTTP 连接 + httpLis := m.Match(cmux.HTTP1Fast()) + + // 初始化 gRPC 服务器 + grpcServer := grpc.NewServer( grpc.StreamInterceptor(grpc_middleware.ChainStreamServer( middleware.StreamGSError500(), )), @@ -128,36 +139,43 @@ func main() { middleware.UnaryGSError500(), )), ) - proto.RegisterTencentNluServer(server, &service.TencentNlu{}) + proto.RegisterTencentNluServer(grpcServer, &service.TencentNlu{}) - _ = consul.RegisterService(global.ServerSetting) //将服务注册到注册中心 - grpc_health_v1.RegisterHealthServer(server, &consul.HealthImpl{}) //执行健康检查 - reflection.Register(server) - - // 启动 gRPC 服务 - go func() { - global.Logger.Info("service is running......") - if err := server.Serve(lis); err != nil { - log.Fatalf("[FATAL] gRPC serve error: %v", err) - } - }() + _ = consul.RegisterService(global.ServerSetting) + grpc_health_v1.RegisterHealthServer(grpcServer, &consul.HealthImpl{}) + reflection.Register(grpcServer) - // 初始化并启动 HTTP 服务 + // 初始化 HTTP 服务器 gin.SetMode(global.ServerSetting.RunMode) engine := router.InitRouter() - _ = engine.SetTrustedProxies(nil) //不使用信任代理功能,即不使用gin策略再对来源过滤 + _ = engine.SetTrustedProxies(nil) - s := http.Server{ - Addr: ":" + global.ServerSetting.HttpPort, - Handler: engine, - //ReadTimeout: global.ServerSetting.ReadTimeout, //设置连接的最大读取时间 - //WriteTimeout: global.ServerSetting.WriteTimeout, //设置连接的最大写入时间 + httpServer := &http.Server{ + Handler: engine, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } + // 启动 cmux 多路复用器 + go func() { + if err := m.Serve(); err != nil { + log.Fatalf("[FATAL] cmux serve error: %v", err) + } + }() + + // 启动 gRPC 服务 + go func() { + global.Logger.Info("gRPC service is running...") + if err := grpcServer.Serve(grpcLis); err != nil { + log.Fatalf("[FATAL] gRPC serve error: %v", err) + } + }() + + // 启动 HTTP 服务 go func() { - global.Logger.Infof("HTTP service is running on port %s", "global.ServerSetting.HttpPort") - if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed { + global.Logger.Infof("HTTP service is running on port %d", global.ServerSetting.Port) + if err := httpServer.Serve(httpLis); err != nil && err != http.ErrServerClosed { log.Fatalf("[FATAL] HTTP serve error: %v", err) } }() @@ -175,11 +193,11 @@ func main() { defer cancel() // 先关闭HTTP服务 - if err := s.Shutdown(ctx); err != nil { + if err := httpServer.Shutdown(ctx); err != nil { log.Printf("[WARN] HTTP server shutdown error: %v", err) } - server.GracefulStop() + grpcServer.GracefulStop() log.Println("[INFO] Server stopped") } diff --git a/pkg/consul/register.go b/pkg/consul/register.go index 42e0c0c..f708ad2 100644 --- a/pkg/consul/register.go +++ b/pkg/consul/register.go @@ -9,20 +9,16 @@ import ( ) type ConsulSettingS struct { - RunMode string - Name string - Tag []string - IP string - Port int - ConsulAddr string - ConsulToken string - Interval time.Duration - Deregister time.Duration - HttpPort string - UriPath string - ReadTimeout time.Duration - WriteTimeout time.Duration - //DefaultContextTimeout time.Duration + RunMode string + Name string + Tag []string + IP string + Port int + ConsulAddr string + ConsulToken string + Interval time.Duration + Deregister time.Duration + UriPath string } func RegisterService(cs *ConsulSettingS) error { -- GitLab