12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package main
- import (
- "app.yhyue.com/moapp/jybase/iputil"
- "bp.jydev.jianyu360.cn/BaseService/gateway/core/node"
- "bp.jydev.jianyu360.cn/BaseService/gateway_node/core/logs"
- "fmt"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/net/ghttp"
- "github.com/gogf/gf/v2/os/gcfg"
- "github.com/gogf/gf/v2/os/gctx"
- "github.com/gogf/guuid"
- "os"
- "os/signal"
- "syscall"
- "time"
- )
- func init() {
- g.Cfg().GetAdapter().(*gcfg.AdapterFile).SetFileName("./etc/config.yaml") //设置配置文件
- logs.InitLogs()
- }
- func main() {
- var serverCode string = gcfg.Instance().MustGet(gctx.New(), "system.serverCode").String()
- var serverPort int = gcfg.Instance().MustGet(gctx.New(), "system.serverPort").Int()
- etcdNodes := gcfg.Instance().MustGet(gctx.New(), "system.etcdListen").Strings()
- server := g.Server()
- server.SetPort(serverPort)
- server.SetLogger(logs.GInfo)
- server.SetAccessLogEnabled(true)
- server.BindHandler("/k3s-demo/{page}", func(r *ghttp.Request) {
- content := fmt.Sprintf("%s %d", gcfg.Instance().MustGet(gctx.New(), "system.response").Strings(), time.Now().Unix())
- logs.GInfo.Info(gctx.New(), content)
- r.Response.WriteJson(map[string]interface{}{
- "Hostname": r.Request.Host,
- "Ip": r.GetRemoteIp(),
- "Method": r.Method,
- "ServerIp": iputil.InternalIp(),
- "uuid": guuid.New().String(),
- "content": content,
- })
- })
- //向网关注册表注册服务。此步骤应在服务启动后注册。服务有重试机制不受影响。
- //NewNode可传入etcd节点,默认http://127.0.0.1:2379
- closeNotify, err := node.NewNode(etcdNodes...).Register(serverCode, fmt.Sprintf("%d", serverPort))
- if err != nil {
- panic(err)
- }
- go func() {
- server.Run()
- }()
- //阻塞主进程,接受退出消息。关闭服务,从注册表中删除。
- //kill -9 信号接受不到,会有2~3秒延迟。服务有重试机制不受影响。
- quit := make(chan os.Signal, 1)
- signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
- <-quit
- closeNotify()
- }
|