Commit ce1c23a9 authored by 赵文静's avatar 赵文静

修改为端口复用

parent ee471383
Pipeline #5411 passed with stage
in 0 seconds
# 更新日志 # 更新日志
## [0.0.1-beta.3] - 2025.6.6-6.11 ## [0.0.1-beta.3] - 2025.6.6-6.14
### Changed ### Changed
- 更改为redis存储sessionId - 更改为redis存储sessionId
- 修复查询历史上的今天切片越界问题 - 修复查询历史上的今天切片越界问题
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
- 修改新闻title - 修改新闻title
- 修改sessionId记录 - 修改sessionId记录
- 修改播控 - 修改播控
- 增加一个http的入口
## [0.0.1-beta.2] - 2025.5.29 ## [0.0.1-beta.2] - 2025.5.29
### Changed ### Changed
......
POST http://localhost:19990/semantic/unisoundc2c/speech-nlu-parse POST http://localhost:13302/semantic/unisoundc2c/speech-nlu-parse
Content-Type: application/json Content-Type: application/json
{ {
"ip": "14.215.222.17", "ip": "14.215.222.17",
"mac": "ece154a865eb", "mac": "ece154a865eb",
"mid": "11011", "mid": "11011",
"query":"今天星期几", "query":"明天天气",
"requestId": "123456111" "requestId": "123456111"
} }
...@@ -45,7 +45,7 @@ Content-Type: application/json ...@@ -45,7 +45,7 @@ Content-Type: application/json
}, },
"nluRet": { "nluRet": {
"asr_recongize": "", "asr_recongize": "",
"text": "今天星期几" "text": "珠海在哪里"
} }
}, },
"version": "v0" "version": "v0"
......
...@@ -7,7 +7,6 @@ Server: ...@@ -7,7 +7,6 @@ Server:
ConsulAddr: 172.28.124.105:8500 #consul的地址 ConsulAddr: 172.28.124.105:8500 #consul的地址
Interval: 10 #健康检查间隔,单位:秒 Interval: 10 #健康检查间隔,单位:秒
Deregister: 1 #注销时间,相当于过期时间,单位:分钟 Deregister: 1 #注销时间,相当于过期时间,单位:分钟
HttpPort: 19990
UriPath: /semantic/unisoundc2c/speech-nlu-parse UriPath: /semantic/unisoundc2c/speech-nlu-parse
RabbitMq: RabbitMq:
......
...@@ -60,6 +60,7 @@ require ( ...@@ -60,6 +60,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/smartystreets/goconvey v1.8.0 // 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/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect
......
...@@ -360,6 +360,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd ...@@ -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/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 h1:Oi49ha/2MURE0WexF052Z0m+BNSGirfjg5RL+JXWq3w=
github.com/smartystreets/goconvey v1.8.0/go.mod h1:EdX8jtrTIj26jmjCOVNMVSIYAtgexqXKHOXW2Dx9JLg= 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 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA= 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 ...@@ -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-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-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-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-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-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
......
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/soheilhy/cmux"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1" "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection" "google.golang.org/grpc/reflection"
...@@ -114,13 +115,23 @@ func main() { ...@@ -114,13 +115,23 @@ func main() {
database.InitRedis() database.InitRedis()
// 初始化 gRPC // 创建监听
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", global.ServerSetting.Port)) lis, err := net.Listen("tcp", fmt.Sprintf(":%d", global.ServerSetting.Port))
if err != nil { if err != nil {
log.Fatalf("[FATAL] Listen failed: %v", err) 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( grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
middleware.StreamGSError500(), middleware.StreamGSError500(),
)), )),
...@@ -128,36 +139,43 @@ func main() { ...@@ -128,36 +139,43 @@ func main() {
middleware.UnaryGSError500(), middleware.UnaryGSError500(),
)), )),
) )
proto.RegisterTencentNluServer(server, &service.TencentNlu{}) proto.RegisterTencentNluServer(grpcServer, &service.TencentNlu{})
_ = consul.RegisterService(global.ServerSetting) //将服务注册到注册中心 _ = consul.RegisterService(global.ServerSetting)
grpc_health_v1.RegisterHealthServer(server, &consul.HealthImpl{}) //执行健康检查 grpc_health_v1.RegisterHealthServer(grpcServer, &consul.HealthImpl{})
reflection.Register(server) reflection.Register(grpcServer)
// 启动 gRPC 服务
go func() {
global.Logger.Info("service is running......")
if err := server.Serve(lis); err != nil {
log.Fatalf("[FATAL] gRPC serve error: %v", err)
}
}()
// 初始化并启动 HTTP 服务 // 初始化 HTTP 服务器
gin.SetMode(global.ServerSetting.RunMode) gin.SetMode(global.ServerSetting.RunMode)
engine := router.InitRouter() engine := router.InitRouter()
_ = engine.SetTrustedProxies(nil) //不使用信任代理功能,即不使用gin策略再对来源过滤 _ = engine.SetTrustedProxies(nil)
s := http.Server{ httpServer := &http.Server{
Addr: ":" + global.ServerSetting.HttpPort, Handler: engine,
Handler: engine, ReadTimeout: 10 * time.Second,
//ReadTimeout: global.ServerSetting.ReadTimeout, //设置连接的最大读取时间 WriteTimeout: 10 * time.Second,
//WriteTimeout: global.ServerSetting.WriteTimeout, //设置连接的最大写入时间
MaxHeaderBytes: 1 << 20, 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() { go func() {
global.Logger.Infof("HTTP service is running on port %s", "global.ServerSetting.HttpPort") global.Logger.Infof("HTTP service is running on port %d", global.ServerSetting.Port)
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed { if err := httpServer.Serve(httpLis); err != nil && err != http.ErrServerClosed {
log.Fatalf("[FATAL] HTTP serve error: %v", err) log.Fatalf("[FATAL] HTTP serve error: %v", err)
} }
}() }()
...@@ -175,11 +193,11 @@ func main() { ...@@ -175,11 +193,11 @@ func main() {
defer cancel() defer cancel()
// 先关闭HTTP服务 // 先关闭HTTP服务
if err := s.Shutdown(ctx); err != nil { if err := httpServer.Shutdown(ctx); err != nil {
log.Printf("[WARN] HTTP server shutdown error: %v", err) log.Printf("[WARN] HTTP server shutdown error: %v", err)
} }
server.GracefulStop() grpcServer.GracefulStop()
log.Println("[INFO] Server stopped") log.Println("[INFO] Server stopped")
} }
......
...@@ -9,20 +9,16 @@ import ( ...@@ -9,20 +9,16 @@ import (
) )
type ConsulSettingS struct { type ConsulSettingS struct {
RunMode string RunMode string
Name string Name string
Tag []string Tag []string
IP string IP string
Port int Port int
ConsulAddr string ConsulAddr string
ConsulToken string ConsulToken string
Interval time.Duration Interval time.Duration
Deregister time.Duration Deregister time.Duration
HttpPort string UriPath string
UriPath string
ReadTimeout time.Duration
WriteTimeout time.Duration
//DefaultContextTimeout time.Duration
} }
func RegisterService(cs *ConsulSettingS) error { func RegisterService(cs *ConsulSettingS) error {
......
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