1.下载jsonRpc包放到vendor目录下
2.启动文件
3.启动效果:启动命令php allserver.php start
4.RpcClient Rpc客户端+RpcClient Rpc服务端
访问:http://localhost/product/public/index/RpcTest/rpctest
报错:stream_socket_client(): unable to connect to tcp://127.0.0.1:2015 (由于目标计算机积极拒绝,无法连接。
)
解决:启动服务
效果:{“code”:0,“msg”:“ok”,“data”:“+ 0.000 kg\r\n”}
RpcClient Rpc客户端
<?php
namespace app\index\controller;
use think\cache\driver\Redis;
require_once 'RpcClient.php';
class Rpctest
{
public function rpctest()
{
$message=input("message");
// 服务端列表
$address_array = array(
'tcp://127.0.0.1:2015', //我们只有一个服务
);
// 配置服务端列表
$Clients = new \RpcClient("User");
$Clients::configs($address_array);
$user_client = $Clients::instance('User');
//这个User是服务端的类,在JsonRpc/Services/User.php
//$message="2B2020302E303030206B670D0A";
$weightvalue=$user_client->processing($message);
$redis=new Redis();
$redis->set("weightvalue",trim($weightvalue['data']));
//var_dump($user_client->getEmail($message));
return json_encode($weightvalue);
}
}
RpcClient Rpc服务端
<?php
/**
* 测试/vendor/jsonrpc/JsonRpc/Services/User.php
* @author walkor <worker-man@qq.com>
*/
class User
{
public static function getInfoByUid($uid)
{
return array(
'uid' => $uid,
'name'=> 'test',
'age' => 18,
'sex' => 'hmm..',
);
}
public static function getEmail($message)
{
//return 'php allserver.php start';
$str = pack("H*", $message);
return $str;
}
public static function processing($message){
//接收到16进制报文转化成正常字符
$str = @hex2bin($message);
if ($str) {
return $str;
} else {
$str = pack("H*", $message);
return $str;
}
//return $message;
}
}