douzhi1879 2019-09-09 08:58
浏览 164

如何在Thrift版本0.10.0的Go中从Thrift Server获取客户端IP?

I'm writing a thrift service in Golang and I would like to understand how I can get the client's IP address in the handler functions implementation.

I have tried it in Java, which can be obtained by overwrite TProcessor#process(in,out), but it seems that this way doesn't work in Golang.

serverTransport, err := thrift.NewTServerSocket(net.JoinHostPort(ip, strconv.FormatInt(port, 10)))
    //serverTransport, err := thrift.NewTServerSocket(net.JoinHostPort("127.0.0.1", "9999"))
    if err != nil {
        log.Error(err.Error())
        os.Exit(1)
    }
    protocolFactory := thrift.NewTCompactProtocolFactory()
    transportFactory := thrift.NewTFramedTransportFactoryMaxLength(thrift.NewTTransportFactory(), 524288000) 

    processor := iface.NewNMPDataServiceProcessor(new(impl.NMPDataService))

    server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
    server.Serve()

    defer server.Stop()

The Handler implementation of NMPDataService interface:

package impl

import (
    "NMPService/Framework/logger"
    "NMPService/NmpService/thrift/iface"
    "NMPService/NmpService/utils"
    "syscall"
    "errors"
    "fmt"
    "io"
    "io/ioutil"
    "os"
    "path/filepath"
    "regexp"
    "strconv"
    "strings"
    "time"

    "github.com/yeka/zip"
)

var log = logger.GetBeeLogger()

// NMPDataService .
type NMPDataService struct {
}

const (
    // MaxDownloadFileLimit ...
    MaxDownloadFileLimit int64 = 15728640
    TempFolder string = "tmp"
)

// ExistsFolder method
// Parameters:
//  - Path
func (handler *NMPDataService) ExistsFolder(path string) (bool, error) {
    log.Info("Check exist folder, path: %s", path)
    if strings.Contains(path, "\\") {
        path = strings.ReplaceAll(path, "\\", "/")
    }
    fileInfo, err := os.Stat(path)
    if os.IsNotExist(err) {
        log.Error("folder %s not exist", path)
        return false, nil
    }

    if !fileInfo.IsDir() {
        msg := "the path is not folder."
        log.Error(msg)
        return false, NewRPCException(1, msg, errors.New(msg))
    }

    if err != nil {
        log.Error("Check exist folder error.")
        return false, NewRPCException(1, "Check exist folder error", err)
    }

    return true, nil
}

Above is my code on Server side. I want to get client IP information from server.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
    • ¥15 C# datagridview 单元格显示进度及值
    • ¥15 thinkphp6配合social login单点登录问题
    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配