grpc-php-client writes log to grpc-golang-server
but when you need to write log to php, set it to timeout
client:
<?php
require dirname(__FILE__)."/vendor/autoload.php";
include_once dirname(__FILE__)."/Apibeatlog/ApiBeatClient.php";
include_once dirname(__FILE__)."/Apibeatlog/LogRequestData.php";
include_once dirname(__FILE__)."/Apibeatlog/LogResponseData.php";
include_once dirname(__FILE__)."/GPBMetadata/Apibeat.php";
function LogInfo($i)
{
//1.client
$client = new Apibeatlog\ApiBeatClient("localhost:1234", [
"credentials" => Grpc\ChannelCredentials::createInsecure(),
"timeout" => 1000,
]);
//2.request
$request = new Apibeatlog\LogRequestData();
$request->setSource("1111111111");
$request->setMessage("22222");
$request->setData(json_encode(array("i"=>$i)));
$request->setService("apigateway");
$request->setHost("127.0.0.1");
set_time_limit(1);
//3.
list($reply, $status) = $client->LogInfo($request)->wait();
//4.
if ($status->code == 0) {
$code = $reply->getCode();
$msg = $reply->getMsg();
//TODO
} else {
$code = -1;
$msg = "grpc request failed";
}
echo "code:".$code."<br>";
echo "msg:".$msg."<br>";
//set_time_limit(0);
}
try {
echo LogInfo(1)."<br>";
} catch (Exception $e) {
echo "Caught exception: ", $e->getMessage(), "<br>";
}
echo "1";
try {
echo LogInfo(2)."<br>";
} catch (Exception $e) {
echo "Caught exception: ", $e->getMessage(), "<br>";
}
echo "2";
?>
server
func (s *ApiBeatRpcServer) LogInfo(ctx context.Context, in *apibeatlog.LogRequestData) (res *apibeatlog.LogResponseData, err error) {
var data models.LogData
if len(in.Message) > models.MAX_MESSAGE_SIZE {
data.Message = string([]byte(in.Message)[:models.MAX_MESSAGE_SIZE])
} else {
data.Message = in.Message
}
data.Source = in.Source
data.Host = in.Host
data.Service = in.Service
var f interface{}
err = json.Unmarshal([]byte(in.Data), &f)
if err != nil || f == nil {
data.Data = nil
} else {
data.Data = f.(map[string]interface{})
}
fmt.Println(in)
time.Sleep(time.Second*5) //
//go kafka.SendLog(config.Config.Topic, config.Config.Key, data)
return &apibeatlog.LogResponseData {
Code: strconv.Itoa(models.ERROR_OK),
Msg:"ok",
}, nil
}