这是我的docker-compose.yml
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时,它可以工作.
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
最佳答案
您尝试连接到localhost,但您需要连接到“弹性”主机.
尝试从PHP连接到elasticsearch,如下所示:
原文链接:https://www.f2er.com/docker/436704.html尝试从PHP连接到elasticsearch,如下所示:
$hosts = [
'elastic',// elastic host was added to you hosts file automatically
];
$client = ClientBuilder::create()
->setHosts($hosts)
->build();
Containers for the linked service will be reachable at a hostname identical to the alias,or the service name if no alias was specified.