WH01243 3 周之前
父节点
当前提交
43a99c3b9d
共有 6 个文件被更改,包括 42 次插入69 次删除
  1. 0 23
      rpc/a/init.go
  2. 1 1
      rpc/config.json
  3. 39 39
      rpc/main.go
  4. 1 1
      rpc/service/chatServer.go
  5. 1 1
      rpc/service/service.go
  6. 0 4
      rpc/service/wxRobot.go

+ 0 - 23
rpc/a/init.go

@@ -1,23 +0,0 @@
-package a
-
-import (
-	"time"
-
-	"app.yhyue.com/moapp/jybase/go-xweb/httpsession"
-	"app.yhyue.com/moapp/jybase/go-xweb/xweb"
-)
-
-func init() {
-	//开启redissession
-	httpsession.IsRedisSessionStore = true
-	httpsession.RedisNotLoginKey = "userId"
-	xweb.Config.Profiler = true
-	xweb.RootApp().BasePath = "/wxRobot"
-	xweb.RootApp().AppConfig.StaticFileVersion = false
-	xweb.RootApp().AppConfig.CheckXsrf = false
-	xweb.RootApp().AppConfig.EnableHttpCache = false
-	xweb.RootApp().AppConfig.Mode = xweb.Product
-	xweb.RootApp().AppConfig.ReloadTemplates = true
-	xweb.RootApp().AppConfig.SessionTimeout = 7 * 24 * time.Hour
-	xweb.RootApp().Logger.SetOutputLevel(1) //输出日志,改为4则不输出任何日志
-}

+ 1 - 1
rpc/config.json

@@ -39,6 +39,6 @@
   "redisServer": "other=172.20.45.129:1712,session=172.20.45.129:1713,newother=172.20.45.129:1712",
   "replyLanguage": "拒收请回复R",
   "replyAutomatically": "已收到您的拒收反馈,感谢您的配合。",
-  "webPort": "50052",
+  "webPort": "50053",
   "grpcWebPort": "50051"
 }

+ 39 - 39
rpc/main.go

@@ -1,26 +1,26 @@
 package main
 
 import (
+	"app.yhyue.com/moapp/jybase/go-xweb/httpsession"
+	"app.yhyue.com/moapp/jybase/go-xweb/xweb"
 	"context"
 	"fmt"
+	"google.golang.org/grpc"
+	"google.golang.org/grpc/health"
+	"google.golang.org/grpc/health/grpc_health_v1"
+	"google.golang.org/grpc/keepalive"
 	"log"
 	"net"
 	"net/http"
 	"os"
 	"os/signal"
+	. "rpc/chat"
 	"rpc/config"
+	_ "rpc/filter"
 	"rpc/service"
 	"sync"
 	"syscall"
 	"time"
-
-	"google.golang.org/grpc"
-	"google.golang.org/grpc/health"
-	"google.golang.org/grpc/health/grpc_health_v1"
-	"google.golang.org/grpc/keepalive"
-	_ "rpc/a"
-	. "rpc/chat"
-	_ "rpc/filter"
 )
 
 type server struct {
@@ -47,38 +47,46 @@ func main() {
 	ctx, cancel := context.WithCancel(context.Background())
 	defer cancel()
 
+	go func() {
+		srv.initGRPCServer()
+		// 启动服务
+		srv.startServices(ctx)
+		// 设置定时任务
+		srv.setupTimedTasks(ctx)
+		// 等待终止信号
+		srv.waitForShutdown()
+
+		// 优雅关闭
+		srv.gracefulShutdown()
+	}()
+
 	// 初始化服务
 	srv.initHTTPServer()
-	srv.initGRPCServer()
-
-	// 启动服务
-	srv.startServices(ctx)
-
-	// 设置定时任务
-	srv.setupTimedTasks(ctx)
 
-	// 等待终止信号
-	srv.waitForShutdown()
-
-	// 优雅关闭
-	srv.gracefulShutdown()
 }
 
 func (s *server) initHTTPServer() {
-	mux := http.NewServeMux()
-	// 可以在这里添加HTTP路由
-	// mux.HandleFunc("/health", healthHandler)
-
-	s.httpServer = &http.Server{
-		Addr:    ":" + config.DbConf.WebPort,
-		Handler: mux,
-	}
+	// 1. 初始化xweb配置
+	httpsession.IsRedisSessionStore = true
+	httpsession.RedisNotLoginKey = "userId"
+	xweb.Config.Profiler = true
+	xweb.RootApp().BasePath = "/wxRobot"
+	xweb.RootApp().AppConfig.StaticFileVersion = false
+	xweb.RootApp().AppConfig.CheckXsrf = false
+	xweb.RootApp().AppConfig.EnableHttpCache = false
+	xweb.RootApp().AppConfig.Mode = xweb.Product
+	xweb.RootApp().AppConfig.ReloadTemplates = true
+	xweb.RootApp().AppConfig.SessionTimeout = 7 * 24 * time.Hour
+	xweb.RootApp().Logger.SetOutputLevel(1)
+
+	// 2. 注册控制器
+	xweb.AddAction(&service.WXroBot{})
+	mux1 := http.NewServeMux()
+	xweb.RunBase(":"+config.DbConf.WebPort, mux1)
 }
-
 func (s *server) initGRPCServer() {
 	// 创建健康检查服务
 	s.healthServer = health.NewServer()
-
 	s.grpcServer = grpc.NewServer(
 		grpc.KeepaliveParams(keepalive.ServerParameters{
 			Time:    60 * time.Second,
@@ -100,14 +108,6 @@ func (s *server) initGRPCServer() {
 }
 
 func (s *server) startServices(ctx context.Context) {
-	// 启动HTTP服务
-	go func() {
-		log.Printf("HTTP服务监听 :%s", config.DbConf.WebPort)
-		if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
-			log.Fatalf("HTTP服务启动失败: %v", err)
-		}
-	}()
-
 	// 启动gRPC服务
 	lis, err := net.Listen("tcp", fmt.Sprintf(":%s", config.DbConf.GrpcWebPort))
 	if err != nil {
@@ -130,7 +130,7 @@ func (s *server) setupTimedTasks(ctx context.Context) {
 	}{
 		{1 * time.Minute, "sendTalk"},
 		{1 * time.Hour, "getContacts"},
-		{120 * time.Second, "heartbeat"},
+		{10 * time.Second, "heartbeat"},
 	}
 
 	for _, task := range tasks {

+ 1 - 1
rpc/service/chatServer.go

@@ -202,7 +202,7 @@ func (s *ChatServer) getClientsSnapshot() []string {
 
 func (s *ChatServer) executeTask(ctx context.Context) {
 	// 为Task操作添加超时控制
-	taskCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
+	taskCtx, cancel := context.WithTimeout(ctx, 60*time.Second)
 	defer cancel()
 
 	done := make(chan struct{})

+ 1 - 1
rpc/service/service.go

@@ -22,7 +22,7 @@ var (
 func Task() {
 	log.Println("定时任务查询")
 	allUserMap := InitUser()
-	//go ScheduledTasks(allUserMap, 0)
+	go ScheduledTasks(allUserMap, 0)
 	go RecurringTask(allUserMap)
 }
 

+ 0 - 4
rpc/service/wxRobot.go

@@ -15,10 +15,6 @@ type WXroBot struct {
 	updateTaskHandler xweb.Mapper `xweb:"/update/task"` //修改任务
 }
 
-func init() {
-	xweb.AddAction(&WXroBot{})
-}
-
 type TaskRequest struct {
 	TaskID        int64                    `json:"taskId,omitempty"`
 	TaskName      string                   `json:"taskName"`