0%

网关的一种实现方式

动手开发自己的API网关

基于:

  • Golang
  • Gin
  • ReverseProxy

Gin 路由热加载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

type ProxyServer struct {
App *gin.Engine
upstreamProxy map[string]*httputil.ReverseProxy
reverseProxyGroup singleflight.Group
bindingHttpServer *http.Server
apiConfig *model.ApiConfigV1
}

func NewProxyServer(conf *model.ApiConfigV1) *ProxyServer {
obj := &ProxyServer{
App: gin.New(),
apiConfig: conf,
upstreamProxy: make(map[string]*httputil.ReverseProxy),
}

obj.initApp()
obj.initRoute()
return obj
}

ps := NewProxyServer(conf)
ps.bindingHttpServer = obj.GetBindingHttpServer()
ps.bindingHttpServer.Handler = ps.App
obj.proxyServer = ps // https://jiankunking.com/golang-copy-on-write-and-atomic.html

欢迎关注我的其它发布渠道