php接口请求超时设置,php grpc client如何设置超时时间?

这篇博客展示了如何使用PHP的gRPC客户端向Go服务发送日志请求,并设置超时限制。在服务端,日志处理被模拟为一个长时间运行的操作。当超时时,PHP客户端会捕获异常。博客还包含了详细的代码示例,展示如何建立连接、创建请求、发起调用以及处理响应。
摘要由CSDN通过智能技术生成

grpc-php-client 写日志到 grpc-golang-server

但是需要给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."
";

echo "msg:".$msg."
";

//set_time_limit(0);

}

try {

echo LogInfo(1)."
";

} catch (Exception $e) {

echo 'Caught exception: ', $e->getMessage(), "
";

}

echo "永远能看到下面的业务1";

try {

echo LogInfo(2)."
";

} catch (Exception $e) {

echo 'Caught exception: ', $e->getMessage(), "
";

}

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

}

GRPC请求处理时间超时设置可以通过在创建`ManagedChannel`或`Server`时设置`ClientInterceptors`或`ServerInterceptors`来实现。 在客户端,您可以使用`ManagedChannelBuilder`创建一个`ManagedChannel`对象,并通过`withDeadlineAfter`方法设置请求处理时间超时时间。例如: ```java ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port) .usePlaintext() .intercept(ClientInterceptors.intercept(new YourClientInterceptor())) .build(); // 设置请求处理时间超时为5秒 channel.withDeadlineAfter(5, TimeUnit.SECONDS); ``` 在这个例子中,我们使用`ClientInterceptors.intercept`方法添加了一个客户端拦截器`YourClientInterceptor`,并通过`withDeadlineAfter`方法将请求处理时间超时设置为5秒。 在服务器端,您可以使用`ServerBuilder`创建一个`Server`对象,并通过`ServerInterceptors`设置请求处理时间超时时间。例如: ```java Server server = ServerBuilder.forPort(port) .addService(new YourService()) .intercept(ServerInterceptors.intercept(new YourServerInterceptor())) .build(); // 设置请求处理时间超时为5秒 server = server.withDeadlineAfter(5, TimeUnit.SECONDS); ``` 在这个例子中,我们使用`ServerInterceptors.intercept`方法添加了一个服务器拦截器`YourServerInterceptor`,并通过`withDeadlineAfter`方法将请求处理时间超时设置为5秒。 请注意,您需要在客户端和服务器端都设置超时时间,以确保请求超时时间内完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值