使用golang在Windows的端口上启动服务器可执行文件

问题描述 投票:0回答:1

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

所以最终结果是要有一个可执行文件,它将在多个端口上启动服务器的多个实例(另一个Windows可执行文件)。我的理想方法是让“ n”个例程在从“ 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个回答
0
投票

我必须对命令使用/ 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)
}
© www.soinside.com 2019 - 2024. All rights reserved.