php grpc client如何设置超时时间?
grpc-php-client 写日志到 grpc-golang-server
但是需要给php写日志时候设置给超时
客户端:
<?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";
?>
服务端
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论