zoukankan      html  css  js  c++  java
  • soheilhy/cmux 网络端口复用服务

    同一个端口可以进行不同的操作还是很有用的,比如一个端口同时提供ssh,http,rpc 服务
    soheilhy/cmux 是一个不错的选择,以下是一个简单的试用,代码来自官方文档

    代码

    main.go

     
    package main
    import (
        "context"
        "fmt"
        "io"
        "log"
        "net"
        "net/http"
        "net/rpc"
        "strings"
        "github.com/soheilhy/cmux"
        "golang.org/x/net/websocket"
        "google.golang.org/grpc"
        grpchello "google.golang.org/grpc/examples/helloworld/helloworld"
    )
    type exampleHTTPHandler struct{}
    func (h *exampleHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "example http response")
    }
    func serveHTTP(l net.Listener) {
        s := &http.Server{
            Handler: &exampleHTTPHandler{},
        }
        if err := s.Serve(l); err != cmux.ErrListenerClosed {
            panic(err)
        }
    }
    func EchoServer(ws *websocket.Conn) {
        if _, err := io.Copy(ws, ws); err != nil {
            panic(err)
        }
    }
    func serveWS(l net.Listener) {
        s := &http.Server{
            Handler: websocket.Handler(EchoServer),
        }
        if err := s.Serve(l); err != cmux.ErrListenerClosed {
            panic(err)
        }
    }
    type ExampleRPCRcvr struct{}
    func (r *ExampleRPCRcvr) Cube(i int, j *int) error {
        *j = i * i
        return nil
    }
    func serveRPC(l net.Listener) {
        s := rpc.NewServer()
        if err := s.Register(&ExampleRPCRcvr{}); err != nil {
            panic(err)
        }
        for {
            conn, err := l.Accept()
            if err != nil {
                if err != cmux.ErrListenerClosed {
                    panic(err)
                }
                return
            }
            go s.ServeConn(conn)
        }
    }
    type grpcServer struct {
        // must embedd this
        grpchello.UnimplementedGreeterServer
    }
    func (s *grpcServer) SayHello(ctx context.Context, in *grpchello.HelloRequest) (
        *grpchello.HelloReply, error) {
        return &grpchello.HelloReply{Message: "Hello " + in.Name + " from cmux"}, nil
    }
    func serveGRPC(l net.Listener) {
        grpcs := grpc.NewServer()
        grpchello.RegisterGreeterServer(grpcs, &grpcServer{})
        if err := grpcs.Serve(l); err != cmux.ErrListenerClosed {
            panic(err)
        }
    }
    type mylogin struct {
    }
    func (my mylogin) ServeHTTP(res http.ResponseWriter, req *http.Request) {
        res.Header().Add("Content-Type", "text/html")
        res.Write([]byte("dalong demo"))
    }
    func main() {
        l, err := net.Listen("tcp", ":50051")
        if err != nil {
            log.Panic(err)
        }
        m := cmux.New(l)
        // We first match the connection against HTTP2 fields. If matched, the
        // connection will be sent through the "grpcl" listener.
        grpcl := m.Match(cmux.HTTP2HeaderFieldPrefix("content-type", "application/grpc"))
        //Otherwise, we match it againts a websocket upgrade request.
        wsl := m.Match(cmux.HTTP1HeaderField("Upgrade", "websocket"))
        // Otherwise, we match it againts HTTP1 methods. If matched,
        // it is sent through the "httpl" listener.
        httpl := m.Match(cmux.HTTP1Fast())
        // If not matched by HTTP, we assume it is an RPC connection.
        rpcl := m.Match(cmux.Any())
        // Then we used the muxed listeners.
        go serveGRPC(grpcl)
        go serveWS(wsl)
        // go serveHTTP(httpl)
        go serveRPC(rpcl)
        go func() {
            http.Serve(httpl, &mylogin{})
        }()
        if err := m.Serve(); !strings.Contains(err.Error(), "use of closed network connection") {
            panic(err)
        }
    }
     

    简单说明:
    以上代码使用了同一个端口提供http,websocket,rpc 服务(grpc,以及rpc)

    运行效果

    说明

    类似的有一个比较好玩的linux 内核对于tcp reuseport 特性的支持,可以复用端口(比如软件升级)同时提升系统的性能,实际上我们可以
    集成go_reuseport
    核心代码部分只需要简单的修改

     
    l, err := reuseport.Listen("tcp", ":50051")
        if err != nil {
            log.Panic(err)
        }

    参考资料

    https://github.com/soheilhy/cmux
    https://github.com/rongfengliang/cmux-learning
    https://github.com/kavu/go_reuseport
    https://segmentfault.com/a/1190000020524323
    http://www.blogjava.net/yongboy/archive/2015/02/12/422893.html

  • 相关阅读:
    liunx 用户切换 su sudo
    tomcat 虚拟目录
    如何用vue封装一个防用户删除的平铺页面的水印组件
    webpack入门学习手记(一)
    理解跨域及常用解决方案
    封装一个优雅的element ui表格组件
    使用Koa.js离不开这十个中间件
    深入理解let和var的区别
    编辑器IDE之VSCode
    WTF!! Vue数组splice方法无法正常工作
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/14145432.html
Copyright © 2011-2022 走看看