【问题标题】:ELK integration with LaravelELK 与 Laravel 的集成
【发布时间】:2022-12-21 13:56:06
【问题描述】:

我是 ELK 的初学者,我在 Laravel 中开发了一个应用程序,后端是 Mysql。

现在,我想将 Elastic 搜索与现有应用程序集成,如果用户在应用程序上更新任何内容,那么它应该立即反映在 Elastic 搜索中。

请指导我。

【问题讨论】:

    标签: laravel elasticsearch logstash laravel-8 elk


    【解决方案1】:

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

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

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

      希望这能给你一些背景知识。

    【讨论】:

    • 感谢您的回复@pindropviolence。我想将它设置到本地主机中。问题是 ES 如何知道用户是否对应用程序进行了任何更新。
    • 嗨 - 用于单调您的数据库。带有 JDBC 输入插件 (elastic.co/guide/en/logstash/current/plugins-inputs-jdbc.html) 的 logstash 管道实例将能够监控您的数据库、表。如果有任何交易,那么将由 Logstash 发送到 ES。您还可以使用 logstash 做许多其他事情,例如处理、gorking 等。对于一个简单的用例,如果您想监视您的应用程序,然后使用 filebeat 读取应用程序日志文件并将它们发送到 ES。在 filebeat.yaml 中,您需要将其配置为读取将发送到 ES 的应用程序日志文件。
    【解决方案2】:

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

    当使用 Model Eloquent 查询创建或更新条目时,特定的 Model Observer 会运行,您可以为观察者函数的弹性搜索设置调度作业。

    从作业执行中,您将在 Elastic 搜索作业中获得该记录的条目,并从那里添加或更新特定数据以及同步的 MySQL 数据库。

    例子 : 你需要在里面设置观察者appProvidersAppServiceProvider.php使用此方法。

    <?php
    
    namespace AppProviders;
    
    use AppModelsBlog;
    use AppObserversBlogObserver;
    use ElasticElasticsearchClient;
    use ElasticElasticsearchClientBuilder;
    use IlluminateSupportServiceProvider;
    
    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);
        }
    }
    

    假设你有博客模式.

    博客.php

    <?php
    
    namespace AppModels;
    
    use CarbonCarbon;
    use IlluminateDatabaseEloquentFactoriesHasFactory;
    use IlluminateDatabaseEloquentModel;
    
    /**
     * 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',
        ];
    }
    

    你有你的appObserversBlogObserver.php

    <?php
    
    namespace AppObservers;
    
    use AppJobsIndexBlogElasticsearchJob;
    use AppModelsBlog;
    
    class BlogObserver
    {
        /**
         * Handle the Blog "created" event.
         *
         * @param  AppModelsBlog  $blog
         * @return void
         */
        public function created(Blog $blog)
        {
            dispatch(new IndexBlogElasticsearchJob($blog));
        }
    
        /**
         * Handle the Blog "updated" event.
         *
         * @param  AppModelsBlog  $blog
         * @return void
         */
        public function updated(Blog $blog)
        {
            dispatch(new IndexBlogElasticsearchJob($blog));
        }
    
        /**
         * Handle the Blog "restored" event.
         *
         * @param  AppModelsBlog  $blog
         * @return void
         */
        public function restored(Blog $blog)
        {
            if ($blog->is_active) {
                dispatch(new IndexBlogElasticsearchJob($blog));
            }
        }
    }
    

    您有内部弹性搜索更改的作业文件appJobsIndexBlogElasticsearchJob.php

    <?php
    
    namespace AppJobs;
    
    use AppModelsBlog;
    use ElasticElasticsearchClient;
    use IlluminateBusQueueable;
    use IlluminateContractsQueueShouldQueue;
    use IlluminateFoundationBusDispatchable;
    use IlluminateQueueInteractsWithQueue;
    use IlluminateQueueSerializesModels;
    
    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      • 2016-01-06
      • 2016-12-26
      • 1970-01-01
      相关资源
      最近更新 更多