请求流程:
//并发执行
go func() {
tcp_proxy_router.TcpServerRun()
}()
tcp_proxy_router.TcpServerStop()
tcp_server.go
首先是定义TcpServer结构体
type TcpServer struct {
Addr string //监听地址
Handler TCPHandler //实际逻辑回调设置handler
err error
BaseCtx context.Context //上下文
//读写超时设置
WriteTimeout time.Duration
ReadTimeout time.Duration
KeepAliveTimeout time.Duration //连接一直保持 发数据包的时间
mu sync.Mutex //锁
inShutdown int32 //是否关闭
doneChan chan struct{}
l *onceCloseListener //单次启动时,listen需要执行的一次性操作的设置
}
然后定义TCP开启和关闭方法
ListenAndServe
func (srv *TcpServer) ListenAndServe() error {
//验证服务是否关闭
//原子方法验证是否为0
//atomic.LoadInt32(&srv.inShutdown) != 0
if srv.shuttingDown() {
return ErrServerClosed
}
if srv.doneChan == nil {
srv.doneChan = make(chan struct{})
}
addr := srv.Addr
if addr == "" {
return errors.New("need addr")
}
//调用核心的官方方法,并传入到Serve中
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
return srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
}
Serve
func (srv *TcpServer) Serve(l net.Listener) error {
srv.l = &onceCloseListener{Listener: l}
//退出时执行listener关闭
defer srv.l.Close()
if srv.BaseCtx == nil {
srv.BaseCtx = context.Background()
}
BaseCtx := srv.BaseCtx
ctx := context.WithValue(BaseCtx, ServiceContextKey, srv)
//轮询Listener的Accept方法,获取客户端发来的conn
for {
//轮询Listener的Accept方法,获取客户端发来的conn
rw, err := l.Accept()
if err != nil {
select {
case <-srv.getDoneChan():
return ErrServerClosed
default:
}
fmt.Printf("accept fail, err: %v\n", err)
continue
}
//拿到便马上创建连接,见下文
c := srv.newConn(rw)
//这里便跳转到了tcp_conn.go中的serve方法进行协程处理,见下文
go c.serve(ctx)
}
return nil
}
tcp_conn.go
跳转到tcp_conn.go中的newConn、serve方法
func (srv *TcpServer) newConn(rwc net.Conn) *conn {
c := &conn{
server: srv,
rwc: rwc,
}
// 设置超时时间参数返回
if d := c.server.ReadTimeout; d != 0 {
c.rwc.SetReadDeadline(time.Now().Add(d))
}
if d := c.server.WriteTimeout; d != 0 {
c.rwc.SetWriteDeadline(time.Now().Add(d))
}
if d := c.server.KeepAliveTimeout; d != 0 {
if tcpConn, ok := c.rwc.(*net.TCPConn); ok {
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(d)
}
}
return c
}
server中,使用recover拦截错误信息的原因:因为是用go开启的协程,所以里面的error是用的panic,所以使用了recover进行捕获
func (c conn) serve(ctx context.Context) {
defer func() {
//recover拦截错误信息并打印
if err := recover(); err != nil && err != ErrAbortHandler {
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
fmt.Printf("tcp: panic serving %v: %v\n%s", c.remoteAddr, err, buf)
}
c.close()
}()
//获取连接地址、上下文、handler
c.remoteAddr = c.rwc.RemoteAddr().String()
ctx = context.WithValue(ctx, LocalAddrContextKey, c.rwc.LocalAddr())
if c.server.Handler == nil {
panic("handler empty")
}
//接着实现Handler中的ServeTCP即可
c.server.Handler.ServeTCP(ctx, c.rwc)
}
func TcpServerRun() {
// 获取TCP服务列表,将tcp的端口全部打开
serviceList := dao.ServiceManagerHandler.GetTcpServiceList()
for _, serviceItem := range serviceList {
tempItem := serviceItem
//通过将tempItem传入协程 开启所有的tcp服务
//具体协程内容见下一个代码块
go func(serviceDetail *dao.ServiceDetail) {
//配置回调handler
//......
//配置上下文
//......
//配置tcp服务器TCPServer
tcpServer := &tcp_server.TcpServer{
Addr: addr,
Handler: routerHandler,
BaseCtx: baseCtx,
}
//放入切片中
tcpServerList = append(tcpServerList, tcpServer)
log.Printf(" [INFO] tcp_proxy_run %v\n", addr)
//开启监听
if err := tcpServer.ListenAndServe(); err != nil && err != tcp_server.ErrServerClosed {
log.Fatalf(" [INFO] tcp_proxy_run %v err:%v\n", addr, err)
}
}(tempItem)
}
}
协程内部
//通过将tempItem传入协程 开启所有的tcp服务
go func(serviceDetail *dao.ServiceDetail) {
//设置tcp服务器
//获取端口
addr := fmt.Sprintf(":%d", serviceDetail.TCPRule.Port)
//获取负载均衡
rb, err := dao.LoadBalancerHandler.GetLoadBalancer(serviceDetail)
if err != nil {
log.Fatalf(" [INFO] GetTcpLoadBalancer %v err:%v\n", addr, err)
return
}
//构建路由及设置中间件
router := tcp_proxy_middleware.NewTcpSliceRouter()
router.Group("/").Use(
tcp_proxy_middleware.TCPFlowCountMiddleware(),
tcp_proxy_middleware.TCPFlowLimitMiddleware(),
tcp_proxy_middleware.TCPWhiteListMiddleware(),
tcp_proxy_middleware.TCPBlackListMiddleware(),
)
//构建回调handler
routerHandler := tcp_proxy_middleware.NewTcpSliceRouterHandler(
//传入负载均衡策略,路由
func(c *tcp_proxy_middleware.TcpSliceRouterContext) tcp_server.TCPHandler {
return reverse_proxy.NewTcpLoadBalanceReverseProxy(c, rb)
}, router)
baseCtx := context.WithValue(context.Background(), "service", serviceDetail)
// 配置TCPServer
tcpServer := &tcp_server.TcpServer{
Addr: addr,
Handler: routerHandler,
BaseCtx: baseCtx,
}
//放入切片中
tcpServerList = append(tcpServerList, tcpServer)
log.Printf(" [INFO] tcp_proxy_run %v\n", addr)
//开启监听
if err := tcpServer.ListenAndServe(); err != nil && err != tcp_server.ErrServerClosed {
log.Fatalf(" [INFO] tcp_proxy_run %v err:%v\n", addr, err)
}
}(tempItem)
注意这里还要定义切片,然后将tcpServer放入切片中(36行)因为之后我们还需要利用这个切片数组来关闭tcp服务
遍历tcp切片列表关闭即可
func TcpServerStop() {
for _, tcpServer := range tcpServerList {
tcpServer.Close()
log.Printf(" [INFO] tcp_proxy_stop %v stopped\n", tcpServer.Addr)
}
}
TCP反向代理的源码实现
首先是TCP反向代理结构体
// TcpReverseProxy TCP反向代理
type TcpReverseProxy struct {
ctx context.Context //单次请求单独设置
Addr string
KeepAlivePeriod time.Duration //设置
DialTimeout time.Duration //设置超时时间
DialContext func(ctx context.Context, netWork, address string) (net.Conn, error)
OnDialError func(src net.Conn, dstDialErr error)
ProxyProtocolVersion int
}
接着设置New方法
func NewTcpLoadBalanceReverseProxy(c *tcp_proxy_middleware.TcpSliceRouterContext, lb load_balance.LoadBalance) *TcpReverseProxy {
return func() *TcpReverseProxy {
nextAddr, err := lb.Get("")
if err != nil {
log.Fatal("get next addr fail")
}
//设置上TCP反向代理结构体
return &TcpReverseProxy{
ctx: c.Ctx,
Addr: nextAddr,
KeepAlivePeriod: time.Second,
DialTimeout: time.Second,
}
}()
}
然后就是编写核心方法ServeTCP
// ServeTCP 传入上游 conn,在这里完成下游连接与数据交换
func (dp *TcpReverseProxy) ServeTCP(ctx context.Context, src net.Conn) {
//设置连接超时
var cancel context.CancelFunc
if dp.DialTimeout >= 0 {
ctx, cancel = context.WithTimeout(ctx, dp.dialTimeout())
}
//开启与下游的连接,见下文
dst, err := dp.dialContext()(ctx, "tcp", dp.Addr)
if cancel != nil {
cancel()
}
if err != nil {
dp.onDialError()(src, err)
return
}
defer func() { go dst.Close() }() //记得退出下游连接
//设置dst的 keepAlive 参数,在数据请求之前
if ka := dp.keepAlivePeriod(); ka > 0 {
if c, ok := dst.(*net.TCPConn); ok {
c.SetKeepAlive(true)
c.SetKeepAlivePeriod(ka)
}
}
errc := make(chan error, 1)
//开启协程进行上游下游数据交换,见下文
go dp.proxyCopy(errc, src, dst)
go dp.proxyCopy(errc, dst, src)
<-errc
}
//开启与下游的连接
func (dp *TcpReverseProxy) dialContext() func(ctx context.Context, netWork, address string) (net.Conn, error) {
if dp.DialContext != nil {
return dp.DialContext
}
return (&net.Dialer{
Timeout: dp.DialTimeout, //连接超时
KeepAlive: dp.KeepAlivePeriod, //设置连接的检测时长
}).DialContext
}
//上游下游数据交换
func (dp *TcpReverseProxy) proxyCopy(errc chan<- error, dst, src net.Conn) {
_, err := io.Copy(dst, src)
errc <- err
}
结束!
TCP代理特点:
thrift:
只使用过一次,记得不太清了,等着以后想起来再回过头填坑