这里是我的docker-compose.yml
无法连接用PHP Elasticsearch内泊坞
nginx:
build: ./nginx/
ports:
- 80:80
links:
- php
volumes_from:
- app
php:
image: php:7.0-fpm
expose:
- 9000
volumes_from:
- app
links:
- elastic
app:
image: php:7.0-fpm
volumes:
- .:/var/www/html
command: "true"
elastic:
image: elasticsearch:2.3
volumes:
- ./elasticsearch/data:/usr/share/elasticsearch/data
- ./elasticsearch/logs:/usr/share/elasticsearch/logs
expose:
- "9200"
ports:
- "9200:9200"
当我尝试去localhost:9200
它的工作访问Elasticsearch。
但是当我尝试使用PHP,我得到以下错误创建索引:
Fatal error: Uncaught Elasticsearch\Common\Exceptions\NoNodesAvailableException: No alive nodes found in your cluster in
下面是客户端代码:
<?php
namespace App\Elasticsearch;
use Elasticsearch\ClientBuilder;
class Client
{
public static function getClient()
{
return ClientBuilder::create()
->build();
}
}
代码实例化一个Elasticsearch对象:
<?php
require 'vendor/autoload.php';
use App\Elasticsearch\Client;
use App\Elasticsearch\Indices\UserIndex;
$es = Client::getClient();
如果我是var_dump($es)
,它将转储Elasticsearch客户端对象。
但是,当我尝试创建索引时,它会引发错误。
<?php
namespace App\Elasticsearch\Indices;
use App\Elasticsearch\Indices\AbstractIndex;
use Elasticsearch\Client;
class UserIndex extends AbstractIndex
{
public function __construct(Client $client)
{
$this->client = $client;
}
}
// Create User Index
$userIndex = new UserIndex($es);
var_dump($userIndex->createIndex('users'));
更新
此页面。我试图
$es = Client::getClient();
try {
// Create User Index
$userIndex = new UserIndex($es);
var_dump($userIndex->createIndex('users'));
} catch (Exception $e) {
var_dump($es->transport->getLastConnection()->getLastRequestInfo());
}
,现在即
["curl"] array(2) { ["error"] "Failed to connect to localhost port 9200: Connection refused" ["errno"] 7
看起来像一个端口的问题确实(有点在https://github.com/elastic/elasticsearch-php/issues/408#issuecomment -207045443) – VonC
也有点像https://github.com/elastic/elasticsearch-php/issues/425#issuecomment-231548912 – VonC
你如何从PHP获取弹性? – opHASnoNAME