V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
xxjwxc
V2EX  ›  Go 编程语言

golang gRPC 和 HTTP 共用端口

  •  
  •   xxjwxc ·
    xxjwxc · 2021-02-24 17:49:49 +08:00 · 2764 次点击
    这是一个创建于 1129 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前言

    接口需要提供给其他业务组访问,但是 RPC 协议不同无法内调,对方问能否走 HTTP 接口 有时候,我们服务都需要同时支持 grpc 跟 http(restful)协议,但是我们又不想多端口开放

    原理

    原理主要是通过共用listener 模式 通过Accept()入口来区分协议内容

    golang 实现

    package main
    
    import (
    	"github.com/soheilhy/cmux"
    )
    
    func Run(){
    	// 起服务
    	// Create the main listener.
    	l, err := net.Listen("tcp", ":23456")
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	// Create a cmux.
    	m := cmux.New(l)
    
    	// First grpc, then HTTP, and otherwise Go RPC/TCP.
    	grpcL := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
    	go grpcS.Serve(grpcL)// Use the muxed listeners for your servers.
    
    	httpL := m.Match(cmux.HTTP1Fast())
    	httpS := &http.Server{
    		Handler: &helloHTTP1Handler{},
    	}
    	go httpS.Serve( httpL)
    
    	// Start serving!
    	m.Serve()	
    }
    

    gin+grpc 实现

    package main
    
    import (
    	"github.com/soheilhy/cmux"
    )
    
    func Run(){
    	// 起服务
    	l, err := net.Listen("tcp", ":23456")
    
    	m := cmux.New(l)
    
    	// grpc
    	grpcL := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
    	grpcS := grpc.NewServer()
    	grpchello.RegisterGreeterServer(grpcS, &server{})
    	go grpcS.Serve(grpcL)
    	
    	// http
    	router := gin.Default()
    	go func (){
    		httpL := m.Match(cmux.HTTP1Fast())
    		router.RunListener( httpL)
    	}()
    
    
    	// Start serving!
    	m.Serve()	
    }
    

    goplugins

    更多:

    xxjwxc goplugins gmsec

    8 条回复    2021-03-16 12:55:25 +08:00
    fucUup
        1
    fucUup  
       2021-02-24 20:46:05 +08:00 via Android
    Cmux 这个开源库比较多 bug,小心
    echowuhao
        2
    echowuhao  
       2021-02-24 22:06:54 +08:00
    放到不同路径下不更简单么
    mooyo
        3
    mooyo  
       2021-02-25 00:23:35 +08:00 via iPhone
    为啥不通过域名或者路径来区分呢
    xxjwxc
        4
    xxjwxc  
    OP
       2021-02-25 11:07:53 +08:00
    @fucUup 是的,问题还是有点多。不过可以优化
    julyclyde
        5
    julyclyde  
       2021-02-25 12:05:45 +08:00
    gRPC 不就是 HTTP/2 么?
    leeyuzhe
        6
    leeyuzhe  
       2021-02-25 13:46:11 +08:00
    对啊,路径区分多简单
    coderxy
        7
    coderxy  
       2021-02-25 14:54:20 +08:00
    grpc gateway ?
    LanLiang
        8
    LanLiang  
       2021-03-16 12:55:25 +08:00
    为啥不考虑多端口+路径路由方式?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2749 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 12:45 · PVG 20:45 · LAX 05:45 · JFK 08:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.