前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊kingbus的startAdminServer

聊聊kingbus的startAdminServer

作者头像
code4it
发布2020-06-17 17:43:41
3260
发布2020-06-17 17:43:41
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下kingbus的startAdminServer

startAdminServer

kingbus/server/server.go

代码语言:javascript
复制
func (s *KingbusServer) startAdminServer(urls types.URLs) error {
    if len(urls) != 1 {
        return ErrArgs
    }
    addr := urls[0].Host
    s.adminSvr = api.NewAdminServer(addr, s, s.cluster)
    return nil
}
  • startAdminServer方法主要是执行api.NewAdminServer

NewAdminServer

kingbus/api/api_server.go

代码语言:javascript
复制
//AdminServer is a server for handling api call
type AdminServer struct {
    AdminAddr string
    web       *echo.Echo
    mh        *MembershipHandler
    bs        *BinlogSyncerHandler
    bm        *BinlogServerHandler
}

//NewAdminServer creates a admin server
func NewAdminServer(addr string, svr Server, cluster Cluster) *AdminServer {
    adminServer := new(AdminServer)

    adminServer.AdminAddr = addr
    adminServer.web = echo.New()
    adminServer.web.HideBanner = true
    adminServer.web.HidePort = true

    adminServer.mh = &MembershipHandler{
        svr:     svr,
        cluster: cluster,
        timeout: adminAPITimeout,
    }
    adminServer.bs = &BinlogSyncerHandler{
        svr:     svr,
        cluster: cluster,
    }
    adminServer.bm = &BinlogServerHandler{
        svr: svr,
    }
    return adminServer
}
  • NewAdminServer创建adminServer,并设置了MembershipHandler、BinlogSyncerHandler、BinlogServerHandler

Run

kingbus/api/api_server.go

代码语言:javascript
复制
func (s *AdminServer) Run() {
    s.RegisterMiddleware()
    s.RegisterURL()
    err := s.web.Start(s.AdminAddr)
    if err != nil {
        log.Log.Infof("admin server start error,err:%s", err)
    }
}
  • Run方法执行RegisterMiddleware、RegisterURL以及s.web.Start(s.AdminAddr)

RegisterMiddleware

kingbus/api/api_server.go

代码语言:javascript
复制
//RegisterMiddleware implements register middleware in web
func (s *AdminServer) RegisterMiddleware() {
    loggerConfig := mw.LoggerConfig{
        Skipper: mw.DefaultSkipper,
        Format: `{"time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}","host":"${host}",` +
            `"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
            `"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
            `"bytes_out":${bytes_out}}` + "\n",
        CustomTimeFormat: "2006-01-02 15:04:05.00000",
        Output:           log.NewWriter(),
    }
    s.web.Use(mw.LoggerWithConfig(loggerConfig))
    s.web.Use(mw.Recover())
}
  • RegisterMiddleware方法主要执行web.Use(mw.LoggerWithConfig(loggerConfig))、web.Use(mw.Recover())

RegisterURL

kingbus/api/api_server.go

代码语言:javascript
复制
//RegisterURL implements url binding
func (s *AdminServer) RegisterURL() {
    //member handler
    s.web.GET("/members", s.mh.GetMembers)
    s.web.POST("/members", s.mh.AddMember)
    s.web.PUT("/members", s.mh.UpdateMember)
    s.web.DELETE("/members", s.mh.DeleteMember)
    s.web.GET("/cluster", s.mh.GetCluster)
    s.web.PUT("/admin/url", s.mh.UpdateAdminURL)

    //binlog syncer handler
    s.web.PUT("/binlog/syncer/start", s.bs.StartBinlogSyncer)
    s.web.PUT("/binlog/syncer/stop", s.bs.StopBinlogSyncer)
    s.web.GET("/binlog/syncer/status", s.bs.GetBinlogSyncerStatus)

    //binlog server handler
    s.web.PUT("/binlog/server/start", s.bm.StartBinlogServer)
    s.web.PUT("/binlog/server/stop", s.bm.StopBinlogServer)
    s.web.GET("/binlog/server/status", s.bm.GetBinlogServerStatus)
}
  • RegisterURL方法给web注册了members、binlog/syncer、binlog/server相关方法

小结

startAdminServer方法主要是执行api.NewAdminServer;NewAdminServer创建adminServer,并设置了MembershipHandler、BinlogSyncerHandler、BinlogServerHandler

doc

  • server.go
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-06-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • startAdminServer
  • NewAdminServer
  • Run
  • RegisterMiddleware
  • RegisterURL
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档