【问题标题】:Launch a server executable on a port in windows using golang使用 golang 在 windows 的端口上启动服务器可执行文件
【发布时间】:2019-09-25 20:25:06
【问题描述】:

我正在尝试使用 golang 在我的 windows 机器上的不同端口上启动多个服务器。这是出于内部测试目的。

所以最终结果是有一个可执行文件,它将在多个端口上启动我的服务器的多个实例(另一个 windows 可执行文件)。我理想的方法是让“n”个 go 例程在“port+0”到“port+n”的端口上启动命令“myserver.exe .\config.json port+n”,这样我就有“ n" 服务器启动并运行。我正在想办法启动我的服务器并继续收听,直到我想要程序结束。

我将我的服务器作为 Windows 可执行文件在启动时提到的端口上提供服务。当我手动启动它时它工作正常。它可以启动为:

C:\Install\myServer.exe C:\Install\Configuration.json 8079 

上述命令启动服务器并监听8079端口。我想在不同端口上启动同一服务器的多个实例。我在golang program doesn't not work on windows serviceshttps://github.com/kardianos/service 之后编写的代码如下。我可以看到启动服务器的命令已经执行,但是当我打开 services.msc 时我没有看到任何服务,当我执行 get-service powershell 命令时也没有。我还尝试在通过我的 go 程序启动后查看是否可以监听端口 8079(在这种情况下)以查看是否得到任何响应,但出现以下错误:

panic: Get http://localhost:8079/config: dial tcp [::1]:8079: connectex: No connection could be made because the targetmachine actively refused it.

以下是代码:

var logger service.Logger

type program struct{}

func (p *program) Start(s service.Service) error {
    // Start should not block. Do the actual work async.
    go p.run()
    return nil
}
func (p *program) run() {
    /*// Do work here
    // the following sleep-print was used for testing if this method is actually being executed and it worked. 
    fmt.Println("Waiting")
    time.Sleep(3 * time.Second)
    fmt.Println("waited")
    */
    command := "C:\\Install\\myServer.exe C:\\Install\\Configuration.json "+ flag.Arg(0) 
    fmt.Println("Creating the command", command)
    c := exec.Command("cmd", command )

    var outb, errb bytes.Buffer
    c.Stdout = &outb
    c.Stderr = &errb

    fmt.Println("Starting the command")
    if err := c.Run(); err != nil { 
        fmt.Println("Error: ", err)
    }else{
        fmt.Println("out:", outb.String(), "err:", errb.String())
    }
    fmt.Println("After run issued")
    //
}
func (p *program) Stop(s service.Service) error {
    // Stop should not block. Return with a few seconds.
    return nil
}

func main() {
    flag.Parse() // get the source and destination directory

    svcConfig := &service.Config{
        Name:        "GoServiceExampleSimple",
        DisplayName: "Go Service Example",
        Description: "This is an example Go service.",
    }

    fmt.Printf("%v+", svcConfig)

    prg := &program{}
    s, err := service.New(prg, svcConfig)
    if err != nil {
        fmt.Println(err)
    }
    logger, err = s.Logger(nil)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("Main Run")
    err = s.Run()
    fmt.Println("After Main Run")
    if err != nil {
        logger.Error(err)
    }
}

以上代码的输出:

Main Run
Creating the command C:\Install\myServer.exe C:\Install\Configuration.json 8079
Starting the command
out: Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

提前谢谢:)

【问题讨论】:

    标签: windows go service server


    【解决方案1】:

    我必须在命令中使用 /c 才能在 Windows 中的 bash 中执行命令。如果没有该命令,则不会执行该命令。

    command := []string{"/c", "C:\\Install\\Build\\server.exe start " }
    out, err := ExecuteCommandInWindows(command, "run")
    if err != "" {
        fmt.Println(err, len(err))
    }else{
        fmt.Println(out)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      相关资源
      最近更新 更多