无法在Docker中使用PHP连接到Elasticsearch

前端之家收集整理的这篇文章主要介绍了无法在Docker中使用PHP连接到Elasticsearch前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_1@这是我的docker-compose.yml

@H_403_1@

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"
@H_403_1@当我尝试通过访问localhost:9200来访问Elasticsearch时,它可以工作.

@H_403_1@但是当我尝试使用PHP创建索引时,我收到以下错误

@H_403_1@

@H_403_1@Fatal error: Uncaught
Elasticsearch\Common\Exceptions\NoNodesAvailableException: No alive
nodes found in your cluster in

@H_403_1@这是客户端代码

@H_403_1@

PHP

namespace App\Elasticsearch;

use Elasticsearch\ClientBuilder;

    class Client 
    {
        public static function getClient()
        {
            return ClientBuilder::create()
                ->build();
        }
    }
@H_403_1@用于实例化Elasticsearch对象的代码

@H_403_1@

PHP

require 'vendor/autoload.PHP';

use App\Elasticsearch\Client;
use App\Elasticsearch\Indices\UserIndex;

$es = Client::getClient();
@H_403_1@如果我是var_dump($es),​​它会转储Elasticsearch客户端对象.

@H_403_1@但是当我尝试创建索引时,它会抛出错误.

@H_403_1@

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'));
@H_403_1@更新

@H_403_1@从enter link description here

@H_403_1@这一页.我试过了

@H_403_1@

$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());
}
@H_403_1@现在它显示了我的卷曲错误,即

@H_403_1@

@H_403_1@[“curl”] array(2) { [“error”] “Failed to connect to localhost port
9200: Connection refused” [“errno”] 7

最佳答案
您尝试连接到localhost,但您需要连接到“弹性”主机.
尝试从PHP连接到elasticsearch,如下所示:

@H_403_1@

$hosts = [
    'elastic',// elastic host was added to you hosts file automatically
];
$client = ClientBuilder::create()
   ->setHosts($hosts)
   ->build();
@H_403_1@Containers for the linked service will be reachable at a hostname identical to the alias,or the service name if no alias was specified.

猜你在找的Docker相关文章