店滴云单模块,单服务可以以微服务的方式分割了
随着企业业务规模的扩大和技术的不断进步,传统的单体应用架构已经难以满足企业对灵活性和可扩展性的需求。在这种背景下,微服务架构以其独特的优势逐渐成为了企业信息化建设的首选。近日,店滴云宣布其单模块已成功实现微服务化分割,为企业带来了更加高效、灵活的服务体验。
![]()
微服务架构是一种将单个应用拆分为一组小型服务的方法,每个服务都运行在自己的进程中,并通过轻量级通信机制进行交互。这种架构风格使得企业能够更加灵活地部署、扩展和管理应用,同时也提高了系统的可维护性和可靠性。
店滴云单模块的微服务化分割,意味着原本庞大而复杂的单体应用被拆解为多个独立的小型服务。每个服务都专注于完成特定的业务功能,并拥有自己的技术栈和数据模型。这样的设计使得每个服务都可以独立地进行开发、测试和部署,从而大大提高了开发效率和响应速度。
同时,微服务化分割还带来了更好的资源利用率和可扩展性。由于每个服务都是独立的,企业可以根据实际需求对不同的服务进行独立的扩展或缩减,避免了资源的浪费和成本的增加。此外,每个服务都可以使用最适合的技术栈和编程语言进行开发,进一步提高了系统的灵活性和可维护性。
店滴云单模块的微服务化分割不仅简化了系统的复杂性,还为企业带来了更多的商业机会。通过将服务进行拆分,企业可以更加精准地定位用户需求,提供更加个性化的服务。此外,这种架构风格也使得企业可以更加灵活地与其他合作伙伴进行集成和协作,共同打造更加完善的生态系统。
使用教程:
# HTTP
## 配置
在根目录下的 .env
RPC_SERVER_IP = 127.0.0.1
RPC_SERVER_PORT = 8080
## 启动
建议在linux下使用
windows: swoole-cli rpc.php
linux: php rpc.php
## 扩展插件服务
common\\rpc\\AddonsRpcService.php
## module 配置
以插件diandi_hotel为例,配置rpc模块
addons\\diandi_hotel\\rpc
### 代码示例
```php
namespace addons\\diandi_hotel\\rpc;
use common\\rpc\\BaseAbstractServiceModule;
use common\\rpc\\PdoPoolContainer;
use EasySwoole\\Rpc\\Manager;
use Swoole\\Coroutine\\Channel;
use Swoole\\Database\\MysqliPool;
class Device extends BaseAbstractServiceModule
{
public static string $moduleName = 'Device';
function moduleName(): string
{
return 'Device';
}
function ceshi()
{
$pool = PdoPoolContainer::getInstance()->get('pdoPool');
$pdo = $pool->get();
$statement = $pdo->prepare("select * from dd_user where id = ?");
if (!$statement) {
throw new \\RuntimeException('Prepare failed');
}
$result = $statement->execute([11]);
if (!$result) {
throw new \\RuntimeException('Execute failed');
}
$result = $statement->fetchAll();
// if ($a + $b !== (int)$result[0][0]) {
// throw new \\RuntimeException('Bad result');
// }
$pool->put($pdo);
$this->response()->setMsg(['a'=>1221,'b'=>$result]);
}
}
```
## 数据库连接池
最后的连接池释放很重要:$pool->put($pdo);
```php
$pool = PdoPoolContainer::getInstance()->get('pdoPool');
$pdo = $pool->get();
$statement = $pdo->prepare("select * from dd_user where id = ?");
if (!$statement) {
throw new \\RuntimeException('Prepare failed');
}
$result = $statement->execute([11]);
if (!$result) {
throw new \\RuntimeException('Execute failed');
}
$result = $statement->fetchAll();
// if ($a + $b !== (int)$result[0][0]) {
// throw new \\RuntimeException('Bad result');
// }
$pool->put($pdo);
```
### 缓存连接池
最后的连接池释放很重要:$pool->put($redis);
```php
$pool = PdoPoolContainer::getInstance()->get('redisPool');
$redis = $pool->get();
$result = $redis->set('foo', 'bar');
if (!$result) {
throw new RuntimeException('Set failed');
}
$result = $redis->get('foo');
if ($result !== 'bar') {
throw new RuntimeException('Get failed');
}
$pool->put($redis);
```
### 请求地址
http://127.0.0.1:8080/addons/device/ceshi
### addons rpc服务名称
### device rpc模块名称
### ceshi rpc模块方法名称
# 其他服务
可以在 rpc模块方法 ceshi 中调用
```php
use Simps\\MQTT\\Message\\SubAck;
use Simps\\MQTT\\Protocol\\ProtocolInterface;
$codes = [0];
$message_id = 8520;
$ack = new SubAck();
$ack->setCodes($codes)
->setMessageId($message_id);
$ack_data = $ack->getContents();
$ack_data = (string) $ack;
// MQTT5
$ack->setProtocolLevel(ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0)
->setCodes($codes)
->setMessageId($message_id)
->setProperties([
'will_delay_interval' => 60,
'message_expiry_interval' => 60,
]);
$ack_data = $ack->getContents();
$ack_data = (string) $ack;
```