我正在构建需要以发布/订阅方式向所有订阅的消费者发送事件的服务,例如。向所有当前连接的客户端发送一个事件。
我使用 Protobuf 来实现以下原型定义:
service EventsService {
rpc ListenForEvents (AgentProcess) returns (stream Event) {}
}
Run Code Online (Sandbox Code Playgroud)
服务器和客户端都是用 Go 编写的。
我的问题是,当客户端启动连接时,流的寿命不长,例如。当服务器从ListenForEvents方法返回时:
service EventsService {
rpc ListenForEvents (AgentProcess) returns (stream Event) {}
}
Run Code Online (Sandbox Code Playgroud)
然后客户端几乎立即收到EOF错误,这意味着服务器可能关闭了连接。
怎么做才能让客户端长期订阅服务器呢?主要问题是,当客户端调用服务器上的方法时,我可能没有任何内容可以发送给客户端ListenForEvents,这就是为什么我希望该流长期存在,以便以后能够发送消息。
PHP中的接口应该返回相同的类型吗?我的朋友告诉我它应该返回相同的类型.(为了良好的实践)我知道PHP是动态类型语言,这是不可能的.
例如:
interface idReturn
{
//Return INT
public function getById($id);
}
interface arrayReturn
{
//Return array
public function getByData($data);
}
Run Code Online (Sandbox Code Playgroud)
这是好的做法吗?
我在服务层的应用程序中遇到依赖性问题.
我有以下课程:
<?php
class UserService{
private $userRepository;
private $vocationService;
private $roleService;
public function __construct(UserRepository $userRepository, VocationService $vocationService, RoleService $roleService)
{
$this->userRepository = $userRepository;
$this->vocationService = $vocationService;
$this->roleService = $roleService;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在注入的只有三个依赖项.假设,我想添加下一个依赖项,例如:NextService.我的构造函数会再次增长.
如果我想在构造函数中传递更多依赖项,该怎么办?
也许我应该通过传递IoC容器然后得到理想的类来解决这个问题?这是一个例子:
<?php
class UserService{
private $userRepository;
private $vocationService;
private $roleService;
public function __construct(ContainerInterface $container)
{
$this->userRepository = $container->get('userRepo');
$this->vocationService = $container->get('vocService');
$this->roleService = $container->get('roleService');
}
}
Run Code Online (Sandbox Code Playgroud)
但现在我的UserService类依赖于我注入的IoC容器.
如何通过良好实践解决问题?
问候,亚当
php dependency-injection inversion-of-control symfony symfony-2.5