php-golang-rpc spiral/goridge库和php spiral/goridge2.4.5实践

golang 代码:

package main

import (

    "fmt"

    "net"

    "net/rpc"

    "github.com/spiral/goridge/v2"

)

type App struct{}

func (*App) Hi(name string, r *string) error {

    *r = fmt.Sprintf("hello %s!", name)

    return nil

}

type Ceshi struct{}

func (*Ceshi) Demo(name string, r *string) error {

    *r = fmt.Sprintf("hello %s!", name)

    return nil

}

func main() {

    ln, err := net.Listen("tcp", ":6001")

    if err != nil {

        panic(err)

    }

    rpc.Register(new(App))

    rpc.Register(new(Ceshi))

    for {

        conn, err := ln.Accept()

        if err != nil {

            continue

        }

        go rpc.ServeCodec(goridge.NewCodec(conn))

    }

}

/****************************************************************/

php代码:

use Spiral\Goridge\RPC;
use Spiral\Goridge\Relay;

$rpc = new RPC(Relay::create('tcp://127.0.0.1:6001'));
echo $rpc->call("Ceshi.Demo", "ceshi");

你可能感兴趣的:(php,golang,rpc)