麋鹿与Laravel的整合

发布于 2025-01-20 03:47:12 字数 117 浏览 5 评论 0原文

我是麋鹿的初学者,我已经在MySQL后端开发了Laravel的应用程序。

现在,我想将弹性搜索与现有应用程序集成在一起。如果用户在应用程序上更新任何内容,则应立即在弹性搜索上反思。

请指导我。

I am very beginner to ELK, I have developed an application in Laravel with Mysql backend.

Now, I would like to integrate the Elastic search with the existing app & if the user updates anything on the app then it should reflect immediately on the Elastic search.

Please guide me on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2025-01-27 03:47:12

将 ELK 集成到您的应用程序中需要几个步骤,甚至您必须决定如何/在哪里运行 es 和 kb 等。有一些可用的选项。您可以在虚拟机中创建自我管理实例,在这种情况下您可以使用 kb 、 es 等的 docker 镜像。
Elastic 提供 ECK(K8s 上的 Elastic Cloud),您可以在 K8s 环境中安装 ECK 操作员,这也将为您启动 kibana 和 elasticsearch。

假设您的应用程序在本地主机中运行:

  1. 配置并部署elasticsearch,编辑elasticsearch.yaml。
  2. 配置和部署 kibana 编辑 kibana.yaml。
  3. 配置beats/logstash(filebeat.yaml)以将日志转发到elasticsearch端点以进行索引。
  4. 一旦您的日志转发器运行并将日志发送到 ES ,您就可以以 kb 为单位可视化它们。

希望这能为您提供一些背景知识。

ELK integration into your application will require few steps before even that you must decide how/where you want to run the es and kb etc. There a some options available. You can create your self managed instances in VMs in that case you may use the docker images for kb , es etc.
Elastic offers ECK (Elastic Cloud on K8s) you could install the ECK operator in a K8s environment and that will also spin up kibana and elasticsearch for you.

Assuming that your app is running in localhost:

  1. Configure and deploy elasticsearch edit elasticsearch.yaml.
  2. Configure and deploy kibana edit kibana.yaml.
  3. Configure beats / logstash (filebeat.yaml) to forward the logs to elasticsearch endpoint for indexing.
  4. Once your log forwarder is running and sending logs to ES , you can visualize them in kb.

Hope this gives you some background.

清风无影 2025-01-27 03:47:12

您可以通过创建模型观察者来做到这一点。

在当时使用模型雄辩查询创建或更新条目时,特定的模型观察者运行,您可以从观察者函数设置调度作业以供弹性搜索。

从作业执行中,您将在弹性搜索作业中获得该记录的输入,从那里您将添加或更新特定数据以及MySQL DB同步。

例子 :
您需要在 app \ Providers \ AppServiceProvider.php 中设置观察者以使用此方法。

<?php

namespace App\Providers;

use App\Models\Blog;
use App\Observers\BlogObserver;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\ClientBuilder;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {

        $this->app->singleton(Client::class, function () {
            return ClientBuilder::create()
                ->setHosts(['127.0.0.1:9200'])
                // ->setHosts(config('elasticsearch.hosts'))
                ->build();
        });
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Blog::observe(BlogObserver::class);
    }
}

假设您有博客模型

blog.php

<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Blog
 * @property integer $id
 * @property boolean $is_active
 * @property Carbon $publish_date
 * @property string $title
 * @property string $content
 * @property array $category
 * @property string $author
 */
class Blog extends Model
{
    use HasFactory;

    protected $fillable = [
        'is_active',
        'publish_date',
        'title',
        'content',
        'category',
        'author',
    ];

    protected $hidden = [
        'is_active',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'category' => 'array',
    ];

    protected $dates = [
        'publish_date',
    ];
}

您拥有您的 app \ observers \ blogobserver.php

<?php

namespace App\Observers;

use App\Jobs\IndexBlogElasticsearchJob;
use App\Models\Blog;

class BlogObserver
{
    /**
     * Handle the Blog "created" event.
     *
     * @param  \App\Models\Blog  $blog
     * @return void
     */
    public function created(Blog $blog)
    {
        dispatch(new IndexBlogElasticsearchJob($blog));
    }

    /**
     * Handle the Blog "updated" event.
     *
     * @param  \App\Models\Blog  $blog
     * @return void
     */
    public function updated(Blog $blog)
    {
        dispatch(new IndexBlogElasticsearchJob($blog));
    }

    /**
     * Handle the Blog "restored" event.
     *
     * @param  \App\Models\Blog  $blog
     * @return void
     */
    public function restored(Blog $blog)
    {
        if ($blog->is_active) {
            dispatch(new IndexBlogElasticsearchJob($blog));
        }
    }
}

您的工作文件 app \ app \ jobs \ indexblogelasticsearchjob.php

<?php

namespace App\Jobs;

use App\Models\Blog;
use Elastic\Elasticsearch\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class IndexBlogElasticsearchJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $blog;

    /**
     * IndexBlogElasticsearchJob constructor.
     * @param Blog $blog
     */
    public function __construct(Blog $blog)
    {
        $this->blog = $blog;
    }

    /**
     * Execute the job.
     * @param Client $client
     */
    public function handle(Client $client)
    {
        $params = [
            'index' => 'blogs',
            'id' => $this->blog->id,
            'body' => $this->blog->toArray(),
        ];

        $client->index($params);
    }
}

You can do this by creating an Observer of Models.

When an entry is created or updated with Model Eloquent query at that time the particular Model Observer run and you can set dispatch job for elastic search from observer functions.

And from job execution, you will get an entry of that record inside the Elastic search job and from there you will add or update particular data along with MySQL DB synchronized.

Example :
You need to setup Observer inside app\Providers\AppServiceProvider.php for use this methods.

<?php

namespace App\Providers;

use App\Models\Blog;
use App\Observers\BlogObserver;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\ClientBuilder;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {

        $this->app->singleton(Client::class, function () {
            return ClientBuilder::create()
                ->setHosts(['127.0.0.1:9200'])
                // ->setHosts(config('elasticsearch.hosts'))
                ->build();
        });
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Blog::observe(BlogObserver::class);
    }
}

Suppose you have Blog Model.

Blog.php

<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Blog
 * @property integer $id
 * @property boolean $is_active
 * @property Carbon $publish_date
 * @property string $title
 * @property string $content
 * @property array $category
 * @property string $author
 */
class Blog extends Model
{
    use HasFactory;

    protected $fillable = [
        'is_active',
        'publish_date',
        'title',
        'content',
        'category',
        'author',
    ];

    protected $hidden = [
        'is_active',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'category' => 'array',
    ];

    protected $dates = [
        'publish_date',
    ];
}

You have your app\Observers\BlogObserver.php

<?php

namespace App\Observers;

use App\Jobs\IndexBlogElasticsearchJob;
use App\Models\Blog;

class BlogObserver
{
    /**
     * Handle the Blog "created" event.
     *
     * @param  \App\Models\Blog  $blog
     * @return void
     */
    public function created(Blog $blog)
    {
        dispatch(new IndexBlogElasticsearchJob($blog));
    }

    /**
     * Handle the Blog "updated" event.
     *
     * @param  \App\Models\Blog  $blog
     * @return void
     */
    public function updated(Blog $blog)
    {
        dispatch(new IndexBlogElasticsearchJob($blog));
    }

    /**
     * Handle the Blog "restored" event.
     *
     * @param  \App\Models\Blog  $blog
     * @return void
     */
    public function restored(Blog $blog)
    {
        if ($blog->is_active) {
            dispatch(new IndexBlogElasticsearchJob($blog));
        }
    }
}

You have job file for elastic search changes inside app\Jobs\IndexBlogElasticsearchJob.php

<?php

namespace App\Jobs;

use App\Models\Blog;
use Elastic\Elasticsearch\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class IndexBlogElasticsearchJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $blog;

    /**
     * IndexBlogElasticsearchJob constructor.
     * @param Blog $blog
     */
    public function __construct(Blog $blog)
    {
        $this->blog = $blog;
    }

    /**
     * Execute the job.
     * @param Client $client
     */
    public function handle(Client $client)
    {
        $params = [
            'index' => 'blogs',
            'id' => $this->blog->id,
            'body' => $this->blog->toArray(),
        ];

        $client->index($params);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文