roadrunner-php/goridge 在docker中从php调用golang代码

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

我使用https://github.com/roadrunner-php/goridgehttps://github.com/roadrunner-server/goridge

.rr.dev.yaml

    version: "2.7"

    server:
        command: "php bin/road-runner-console baldinof:roadrunner:worker"
        env:
            - APP_RUNTIME: Baldinof\RoadRunnerBundle\Runtime\Runtime

    
    go_worker:
        command: "go run main.go"
        env:
            - GOOS: linux
            - GOARCH: amd64

main.go

    package main

    import (
            "fmt"
            "net"
            "net/rpc"

            goridgeRpc "github.com/roadrunner-server/goridge/v3/pkg/rpc"
    )

    type App struct{}

    func (s *App) Hi(name string, r *string) error {
            *r = fmt.Sprintf("Hello, %s!", name)
            return nil
    }

    func main() {
        fmt.Println("Hello")

            ln, err := net.Listen("tcp", ":6001")
            if err != nil {
                    panic(err)
            }

            _ = rpc.Register(new(App))

            for {
                    conn, err := ln.Accept()
                    if err != nil {
                            continue
                    }
                    _ = conn
                    go rpc.ServeCodec(goridgeRpc.NewCodec(conn))
            }
    }

php代码

    $tcpRPC = new Goridge\RPC\RPC(Goridge\Relay::create('tcp://127.0.0.1:6001'));

    $r = $tcpRPC->call("App.Hi",  "Antony");

    var_dump(
        $r
    ); echo __METHOD__.':'.__LINE__;die();

运行rr

bin/rr-server serve .rr.dev.yaml -d

输出

{"level":"debug","ts":1729072653.5290942,"logger":"rpc","msg":"plugin was started","address":"tcp://127.0.0.1:6001","list of the plugins with RPC methods:":["informer","resetter","app"]}
[INFO] RoadRunner server started; version: 2.12.3, buildtime: 2023-02-16T13:08:41+0000

运行控制台 php 命令

  "exception" => Spiral\Goridge\RPC\Exception\ServiceException^ {
    #message: "Error 'rpc: can't find service App.Hi' on tcp://127.0.0.1:6001"

这一切都在 docker 中。也许您需要以某种方式转发端口。但一般来说,我在容器内运行服务器启动和命令 - 在不同的控制台中

php go spiral roadrunner
1个回答
0
投票

你错过了数组括号

 <?php
 require 'vendor/autoload.php';  // Assuming you installed Goridge via Composer

 use Spiral\Goridge;
 use Spiral\RoadRunner;

 try {
   // Create a TCP relay connection
   $relay = Goridge\Relay::create('tcp://127.0.0.1:6001');
   $tcpRPC = new RoadRunner\RPC\RPC($relay);

   // Call the Go RPC method (note the use of an array for parameters)
   $r = $tcpRPC->call("App.Hi", ["Antony"]);

   // Output the result
   var_dump($r);
 } catch (\Throwable $e) {
 echo "Error: " . $e->getMessage();
 }

 echo __METHOD__ . ':' . __LINE__;
 die();
© www.soinside.com 2019 - 2024. All rights reserved.