Swoole 分布式通讯框架 SwooleDistributed 2.6 正式版发布

栏目: 软件资讯 · 发布时间: 8年前

内容简介:SwooleDistributed2.6正式版发布 本次发布的版本包含几个重大功能更新。 1.中间件模块 SD框架引入了中间件过程,消息的传递流程如下。 message->pack->middleware1(before)->middleware2(before)->...->route->c...

SwooleDistributed2.6正式版发布

本次发布的版本包含几个重大功能更新。

1.中间件模块

SD框架引入了中间件过程,消息的传递流程如下。

message->pack->middleware1(before)->middleware2(before)->...->route->controller->...->middleware2(after)->middleware1(after)

中间件作用于每一个端口配置,也就是说不同的端口可以单独配置中间件。
中间件的执行严格按照数据的顺序执行。

$config['ports'][] = [
    'socket_type' => PortManager::SOCK_HTTP,
    'socket_name' => '0.0.0.0',
    'socket_port' => 8081,
    'route_tool' => 'NormalRoute',
    'middlewares' => ['MonitorMiddleware', 'NormalHttpMiddleware']
];

如上图执行顺序为MonitorMiddleware(before)->NormalHttpMiddleware(before)->...->NormalHttpMiddleware(after)->MonitorMiddleware(after).

2.运行链路追踪

Array
(
    [RunStack] => Array
        (
            [0] => Server\Middlewares\MonitorMiddleware::before_handle
            [1] => Server\Middlewares\NormalHttpMiddleware::before_handle
            [2] => Server\Controllers\TestController::setRequestResponse
            [3] => Server\Controllers\TestController::http_testContext
            [4] => Server\Models\TestModel::contextTest
            [5] => Server\Tasks\TestTask::contextTest
            [6] => Server\CoreBase\TaskProxy::startTask
            [7] => Server\Models\TestModel::destroy
            [8] => Server\Controllers\TestController::destroy
            [9] => Server\Middlewares\NormalHttpMiddleware::after_handle
            [10] => Server\Middlewares\MonitorMiddleware::after_handle
        )

    [request_id] => 15081258371921825039
    [controller_name] => TestController
    [method_name] => TestController:http_testContext
    [test] => 1
    [path] => /TestController/testContext
    [execution_time] => 4.1890144348145
)

通过这个可以精确判断发生异常和错误的位置,也可以了解到SD框架的工作流程。

你可以在AppServer的__construct方法中设置$this->setDebugMode()来开启这个打印。

public function __construct()
    {
        $this->setLoader(new Loader());
        $this->setDebugMode();
        parent::__construct();
    }

3.Context上下文

上下文共享

middleware,controller,model,task,在一个消息流程中共享同一个上下文Context。

其中middleware,controller,model可以修改Context,而task只能读取不能修改。

例如MonitorMiddleware

 public function after_handle($path)
    {
        $this->context['path'] = $path;
        $this->context['execution_time'] = (microtime(true) - $this->start_run_time) * 1000;
        if (self::$efficiency_monitor_enable) {
            $this->log('Monitor');
        }
   }

这里将context附加了path和execution_time,在after_handle流程前所有对context的操作都会影响到这里的context。

通过getContext获取当前的上下文。

4.AOP

面向切片的编程。

SD的面向切片提供了基础框架,通过继承AOP类或者实现IAOP接口可以支持实现面向切片编程。可能实现方法和主流的AOP不太一样。

Controller和Model,Task等SD重要组件都已经实现AOP化。

以Model为例,实现自己的AOP代理

<?php

namespace Server\Models;

use Server\CoreBase\ChildProxy;
use Server\CoreBase\Model;
use Server\CoreBase\SwooleException;

class TestModel extends Model
{

    public function __construct()
    {
        parent::__construct(TestModelProxy::class);
    }

    public function initialization(&$context)
    {
        parent::initialization($context);
    }

    public function timerTest()
    {
        print_r("model timer\n");
    }

}

class TestModelProxy extends ChildProxy
{
    public function beforeCall($name, $arguments = null)
    {
        var_dump("before");
    }

    public function afterCall($name, $arguments = null)
    {
        var_dump("after");
    }
}

目前AOP的整体框架还在完善中,请期待后面的版本更新。


【声明】文章转载自:开源中国社区 [http://www.oschina.net]


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

年入10万,17岁草根少年的网赚实战

年入10万,17岁草根少年的网赚实战

陶秋丰 / 重庆出版集团 / 2009-3 / 28.00元

《年入10万:17岁草根少年的网赚实战》以一个17岁的在校大学生的真实故事为大家讲述草根少年的网络赚钱之旅。随着网络的普及以及网上应用的日益增多,要在网络上谋生并不难,比如网上写稿、网上兼职、威客赚钱、网上开店等,然而要利用互联网赚大钱,并成就一番事业,那么创建并运营一个独立的网站就是一个绝佳的选择。本书的作者正是经历了“网上写稿一网上各类兼职一策划并创建网站一网站推广与运营一年入10万”这一过程......一起来看看 《年入10万,17岁草根少年的网赚实战》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具