翻译进度
4
分块数量
3
参与人数

选择器

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。

选择器

连接池维护连接链表,并决定连接节点何时从活跃状态转换为消亡状态(反之亦然)。可是,选择哪个连接是没有逻辑性的。这个工作属于选择器类。

选择器的任务是从提供的连接列表中返回单个连接。和连接池一样,这里有几个实现好的选择器可供选择。

Explorer 翻译于 6天前

RoundRobinSelector (Default)

This selector returns connections in a round-robin fashion. Node #1 is selected on the first request, Node #2 on the second request, etc. This ensures an even load of traffic across your cluster. Round-robin'ing happens on a per-request basis (e.g. sequential requests go to different nodes).

The RoundRobinSelector is default, but if you wish to explicitily configure it you can do:

$client = ClientBuilder::create()
            ->setSelector('\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector')
            ->build();

Note that the implementation is specified via a namespace path to the class.

StickyRoundRobinSelector

This selector is "sticky", in that it prefers to reuse the same connection repeatedly. For example, Node #1 is chosen on the first request. Node #1 will continue to be re-used for each subsequent request until that node fails. Upon failure, the selector will round-robin to the next available node, then "stick" to that node.

This is an ideal strategy for many PHP scripts. Since PHP scripts are shared-nothing and tend to exit quickly, creating new connections for each request is often a sub-optimal strategy and introduces a lot of overhead. Instead, it is better to "stick" to a single connection for the duration of the script.

By default, this selector will randomize the hosts upon initialization, which will still guarantee an even distribution of load across the cluster. It changes the round-robin dynamics from per-request to per-script.

If you are using [_future_mode], the "sticky" behavior of this selector will be non-ideal, since all parallel requests will go to the same node instead of multiple nodes in your cluster. When using future mode, the default RoundRobinSelector should be preferred.

If you wish to use this selector, you may do so with:

$client = ClientBuilder::create()
            ->setSelector('\Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector')
            ->build();

Note that the implementation is specified via a namespace path to the class.

随机选择器

无论什么状态下,该选择器只返回一个随机的节点。常被用于测试。

你可以这样来使用随机选择器:

$client = ClientBuilder::create()
            ->setSelector('\Elasticsearch\ConnectionPool\Selectors\RandomSelector')
            ->build();

请注意,使用时需要指定类的完整命名空间。

自定义选择器

你可以自定义选择器。但是必须实现 SelectorInterface 类的方法

namespace MyProject\Selectors;

use Elasticsearch\Connections\ConnectionInterface;
use Elasticsearch\ConnectionPool\Selectors\SelectorInterface

class MyCustomSelector implements SelectorInterface
{

    /**
     * 选择一个连接
     *
     * @param array $connections Array of Connection objects
     *
     * @return ConnectionInterface
     */
    public function select($connections)
    {
        // 详细代码
    }

}

你可以通过对象注入或者实例化来使用自定义选择器:

$mySelector = new MyCustomSelector();

$client = ClientBuilder::create()
            ->setSelector($mySelector)                             // object injection
            ->setSelector('\MyProject\Selectors\FirstSelector')    // or namespace
            ->build();
若相惜 翻译于 1周前

本文章首发在 Laravel China 社区
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

参与译者:3
讨论数量: 0
发起讨论


暂无话题~