目录
环境
elasearch 版本:8.4.1
docker pull elasticsearch:8.4.1
laravel:5.5 实则就是运行在dokcer的
php:7.3 用的docker环境
遇到的错误
No alive nodes found in your cluster
实则es的底层报错就是:Failed to connect to 127.0.0.1 port 9200: Connection refused
解决问题:
laravel 需要用你的es docker name 来做配置的,不能用127.0.0.1:9200
.env
解决方案已经放最前面了,下面是过程
解决过程
composer下载包
首先,跟大家描述下如何搭建es包
执行:composer require elasticsearch/elasticsearch
我的包版本
"elasticsearch/elasticsearch": "^7.17",
添加依赖注入
文件位置:app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
$this->app->singleton('elasticsearch', function ($app) {
$config = config('services.elasticsearch');
$hosts = $config['hosts'];
$clientBuilder = ClientBuilder::create();
$clientBuilder->setHosts($hosts);
// 如果你有额外的配置需求,可以在这里添加
return $clientBuilder->build();
});
}
}
配置config文件位置
config/services.php
添加elasticsearch
http这个下标是用不到的目前
<?php
return [
/*
|--------------------------------------------------------------------------
| Third Party Services
|--------------------------------------------------------------------------
|
| This file is for storing the credentials for third party services such
| as Stripe, Mailgun, SparkPost and others. This file provides a sane
| default location for this type of information, allowing packages
| to have a conventional place to find your various credentials.
|
*/
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
'ses' => [
'key' => env('SES_KEY'),
'secret' => env('SES_SECRET'),
'region' => 'us-east-1',
],
'sparkpost' => [
'secret' => env('SPARKPOST_SECRET'),
],
'stripe' => [
'model' => App\User::class,
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
'elasticsearch' => [
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://localhost:9200'),
],
'http' => [
'headers' => [
'Authorization' => 'Basic ' . base64_encode(env('ELASTICSEARCH_USERNAME') . ':' . env('ELASTICSEARCH_PASSWORD')),
],
],
],
];
.env配置
ELASTICSEARCH_HOST=http://127.0.0.1:9200
ELASTICSEARCH_USERNAME=elastic
ELASTICSEARCH_PASSWORD=_CZ*lJN9Iqy_tYksUjmL
创建一个es数据
$client = app('elasticsearch');
$params = [
'index' => $index,
// 'type' => '_doc', // 在 Elasticsearch 7.x 之后,type 已经被移除,这里只是为了示例
// 'id' => '1',
'body' => [
'title' => $title,
'content' => $content,
// 其他字段
],
];
dd($client->index($params));
报错:
Elasticsearch \ Common \ Exceptions \ NoNodesAvailableException
No alive nodes found in your cluster
通过右边文件,找到底层代码
增加一句代码,看到真实的报错
下面这个就是真实的报错了 cURL error 7: Failed to connect to 127.0.0.1 port 9200: Connection refused
实则你也可以在vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php 方法
里面找到真实的报错
这时候,其实就是我们宿主机(mac)请求docker的问题
这时候我把.env中的 127.0.0.1:9200 改成我的 docker名字就可以解决了
查看我的docker 名字
改 laravel 的 .env文件
ELASTICSEARCH_HOST=es8.4.1
ELASTICSEARCH_USERNAME=elastic
ELASTICSEARCH_PASSWORD=_CZ*lJN9Iqy_tYksUjmL
再调用创建代码,就可以了,已经解决
$client = app('elasticsearch');
$params = [
'index' => $index,
// 'type' => '_doc', // 在 Elasticsearch 7.x 之后,type 已经被移除,这里只是为了示例
// 'id' => '1',
'body' => [
'title' => $title,
'content' => $content,
// 其他字段
],
];
dd($client->index($params));