添加solr unsing日光浴室的多个端点



当我添加多个solr服务器作为端点并使用单个solarium客户端在任何solr服务器上激发查询时;我将复制主/从添加到solr。目前,我觉得当onecore(主服务器或从服务器)的服务器出现故障时,客户端/日光浴室必须使用可用的端点。

我得到这个错误:

Message: Solr HTTP error: HTTP request failed, Failed connect to 127.0.0.1:8983; No error

因为我在8983端口停止了solr,而solr在9000端口的slave core上运行,所以在本例中,如果localhost没有运行,我会寻找连接到localhost2的方法。这是我的代码:

$config = array(
         "endpoint" => array("localhost" => array("host"=>"127.0.0.1",
         "port"=>"8983", "path"=>"/solr", "core"=>"master",),
         "localhost2" => array("host"=>"127.0.0.1",
         "port"=>"9000", "path"=>"/solr", "core"=>"slave",)
        ) );
    $client = new SolariumClient($config);
            $ping = $client->createPing();
            $client->ping($ping,"localhost2");
            $client->getEndpoints();

当密钥localhost没有运行,而密钥localhost2在端口9000中运行时,我得到以下消息Solr HTTP错误:HTTP请求失败,连接127.0.0.1:8983失败;无错误

AN和$client->getEndpoints()的输出;

object(SolariumClient) {
    [protected] options => array(
        'adapter' => 'SolariumCoreClientAdapterCurl',
        'endpoint' => array(
            'localhost' => array(
                'host' => '*****',
                'port' => '*****',
                'path' => '/solr',
                'core' => 'master'
            ),
            'localhost2' => array(
                'host' => '*****',
                'port' => '*****',
                'path' => '/solr',
                'core' => 'slave'
            )
        )
    )
    [protected] queryTypes => array(
        'select' => 'SolariumQueryTypeSelectQueryQuery',
        'update' => 'SolariumQueryTypeUpdateQueryQuery',
        'ping' => 'SolariumQueryTypePingQuery',
        'mlt' => 'SolariumQueryTypeMoreLikeThisQuery',
        'analysis-document' => 'SolariumQueryTypeAnalysisQueryDocument',
        'analysis-field' => 'SolariumQueryTypeAnalysisQueryField',
        'terms' => 'SolariumQueryTypeTermsQuery',
        'suggester' => 'SolariumQueryTypeSuggesterQuery',
        'extract' => 'SolariumQueryTypeExtractQuery',
        'get' => 'SolariumQueryTypeRealtimeGetQuery'
    )
    [protected] pluginTypes => array(
        'loadbalancer' => 'SolariumPluginLoadbalancerLoadbalancer',
        'postbigrequest' => 'SolariumPluginPostBigRequest',
        'customizerequest' => 'SolariumPluginCustomizeRequestCustomizeRequest',
        'parallelexecution' => 'SolariumPluginParallelExecutionParallelExecution',
        'bufferedadd' => 'SolariumPluginBufferedAddBufferedAdd',
        'prefetchiterator' => 'SolariumPluginPrefetchIterator',
        'minimumscorefilter' => 'SolariumPluginMinimumScoreFilterMinimumScoreFilter'
    )
    [protected] eventDispatcher => object(SymfonyComponentEventDispatcherEventDispatcher) {
        [private] listeners => array()
        [private] sorted => array()
    }
    [protected] pluginInstances => array()
    [protected] endpoints => array(
        'localhost' => object(SolariumCoreClientEndpoint) {
            [protected] options => array(
                'host' => '*****',
                'port' => '*****',
                'scheme' => 'http',
                'path' => '/solr',
                'core' => 'master',
                'timeout' => (int) 5,
                'key' => 'localhost'
            )
        },
        'localhost2' => object(SolariumCoreClientEndpoint) {
            [protected] options => array(
                'host' => '*****',
                'port' => '*****',
                'scheme' => 'http',
                'path' => '/solr',
                'core' => 'slave',
                'timeout' => (int) 5,
                'key' => 'localhost2'
            )
        }
    )
    [protected] defaultEndpoint => 'localhost'
    [protected] adapter => null
}

这就是我如何在"端点"之间切换的方法:

$endpoints=[
    'endpoint' => [
            'foo' => [
                'host' => '127.0.0.1',
                'port' => '9000',
                'path' => 'foo',
                'core' => 'foo',
                'timeout' => 15
            ],
            'baz' => [
                'host' => '127.0.0.1',
                'port' => '7464',
                'path' => 'baz',
                'core' => 'baz',
                'timeout' => 15
            ],
    ]
];
$solrClient = new SolariumClient($config);
$solrClient->setDefaultEndPoint('baz');

而函数setDefaultEndPoint将实现神奇的

设置默认终点

`

这用于在PHP框架中建立多个solr连接。

要在配置文件夹中添加以下代码

<?php
    $config['endpoint1'] = array( // endpoint1 is a FIRST connection.
        'endpoint' => array(
            'localhost' => array(
              'host' => 'host_name', // localhost or www.host.com
              'port' => 'port_value', //Default 8983 or 8080 etc,
              'path' => '/solr/',
              'core' => 'solr_core_name' // core1 or movie1 or etc,
            )
        )
    );
    $config['endpoint2'] = array( // endpoint2 is a secound connection.
        'endpoint' => array(
          'localhost' => array(
              'host' => 'host_name', // localhost or www.host.com
              'port' => 'port_value', //Default 8983 or 8080 etc,
              'path' => '/solr/',
              'core' => 'solr_core_name' // core1 or movie1 or etc,
          )
        )
    );
?>

并在__construct()方法中添加连接链接

public function __construct() {
   parent::__construct();
   $this->config->load('solarium');
   $this->endpoint1 = new SolariumClient($this->config->item('endpoint1')); 
   $this->endpoint2 = new SolariumClient($this->config->item('endpoint2')); 
}

如果对设置solr和thier连接有任何疑问,请参阅此Github存储库。

最新更新