php-golang-rpc 简单的jsonrpc实践

golang代码:

package main

import (

"net"

"net/rpc"

"net/rpc/jsonrpc"

)

type App struct{}

type Res struct {

Code int `json:"code"`

Msg string `json:"msg"`

Data any `json:"data"`

}

func (*App) Hi(mp mapstringany, res *Res) error {

res.Code = 200

res.Msg = "成功"

var rmp = make(mapstringany, 0)

if v, ok := mp"name".(string); ok {

rmp"name" = "my name is " + v

} else {

rmp"name" = "my name is unknown"

}

res.Data = rmp

return nil

}

func main() {

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

if err != nil {

panic(err)

}

rpc.Register(new(App))

for {

conn, err := ln.Accept()

if err != nil {

continue

}

go func(conn net.Conn) {

jsonrpc.ServeConn(conn)

}(conn)

}

}

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

php代码:

public function index()

{

//访问结构体 Calc 下 Compute 方法

res = this->Call("App.Hi",'name'=\>'ceshi222');

dd($res);

}

public function Call(method, params) {

conn = fsockopen('127.0.0.1', 6001, errno, $errstr, 3);

if (!$conn) {

return false;

}

err = fwrite(conn, json_encode(array(

'method' => $method,

'params' => array($params),

'id' => time(),

))."\n");

if ($err === false){

return false;

}

stream_set_timeout($conn, 0, 3000);

line = fgets(conn);

if ($line === false) {

return NULL;

}

return json_decode($line,true);

}

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

php封装:

PHP 端的就送 rpc 调用:rpc_client.php

复制代码
<?php

class JsonRPC {
    private $conn;
    function __construct($host, $port) {
        $this->conn = fsockopen($host, $port, $errno, $errstr, 3);
        if (!$this->conn) {
            return false;
        }
    }
    public function Call($method, $params) {
        if (!$this->conn) {
            return false;
        }
        $err = fwrite($this->conn, json_encode(array(
                    'method' => $method,
                    'params' => array($params),
                    'id' => 0,
                )) . "\n");

        if ($err === false){
            return false;
        }
        stream_set_timeout($this->conn, 0, 3000);
        $line = fgets($this->conn);
        if ($line === false) {
            return NULL;
        }
        return json_decode($line, true);
    }
}
$client = new JsonRPC("127.0.0.1", 30002);
$r = $client->Call("ServiceCompute.Add", array('X' => 30, 'Y' => 40));
var_dump($r);
相关推荐
夜雪一千6 小时前
MySQL 全局锁是什么?原理、风险、备份踩坑完整实战
android·mysql·adb
又见情义12 小时前
RK3568 Android 13 驱动适配实战:从零开始让SDK在自己的板子上跑起来
android·arm开发·驱动开发
霸道流氓气质14 小时前
MySQL 大表 DDL 变更 — 原理、方案与实践
android·数据库·mysql
明雨-开发14 小时前
Android打包时候Execution failed for task ‘:launcher:lintVitalAnalyzeRelease‘.
android
游戏开发爱好者814 小时前
开心上架是什么,一站式 Apple 开发者工作台总览
android·小程序·https·uni-app·iphone·webview
Android-Flutter15 小时前
android kotlin launch 源码分析
android·kotlin
Android-Flutter15 小时前
android kotlin 状态机 Continuation 详解
android·kotlin
Zha0Zhun16 小时前
Jetpack Compose 实现 iOS 风格 3D WheelPicker
android
深念Y17 小时前
RIO-UL00(EMUI 4.1 / Android 6.0.1 / arm64)开机自启动 sshd
android·linux·华为·安卓·chroot·sshd·emui
爱和冰阔落17 小时前
【MySQL 慢查询排查实战】列表接口逐渐变慢时,怎样从请求链路定位原因
android·数据库·mysql