多種MySQL與Elasticsearch的數(shù)據(jù)同步解決方案

es常用來解決大數(shù)據(jù)量下mysql查詢的性能問題,而他們之間的數(shù)據(jù)同步問題就很關(guān)鍵。mysql和es的數(shù)據(jù)同步方案網(wǎng)上有很多,在這里總結(jié)記錄下我使用過的三種方案。

還有一個(gè)阿里云的開源軟件 canal 也可以解決這個(gè)同步問題,他的原理與下邊的 go-mysql-elasticsearch 很像,都是通過監(jiān)控MySQL的binlog日志來實(shí)現(xiàn)同步的。但是我沒具體使用過,就不多說它了,感興趣的自己去搜一下這款工具。

第一種方案:代碼層面實(shí)現(xiàn)

項(xiàng)目開發(fā)我使用的是Laravel框架,所以采用了 Laravel Redis 隊(duì)列 + ES API 的方式來實(shí)現(xiàn)的數(shù)據(jù)同步。

原理:使用 Laravel Redis 隊(duì)列,在代碼中MySQL新增數(shù)據(jù)之后觸發(fā)異步任務(wù)調(diào)用 ES 的 API,將數(shù)據(jù)同步到ES中。

這種方案的好處就是實(shí)現(xiàn)和維護(hù)簡單,缺點(diǎn)就是與業(yè)務(wù)代碼耦合太重

PS:如何在laravel中接入es這個(gè)就不說了,不了解的可以看之前的文章:docker安裝es以及在Laravel中的接入

  1. 在es中先創(chuàng)建好相應(yīng)的索引(這是個(gè)商城項(xiàng)目,以新增商品為例)

    PUT /products/
    {
      "mappings": {
        "properties": {
          "name":{
            "type": "text",
            "analyzer": "ik_smart"
          },
          "long_name":{
            "type": "text",
            "analyzer": "ik_smart"
          },
          "brand_id":{
            "type": "integer"
          },
          "category_id":{
            "type":"integer"
          },
          "shop_id":{
            "type":"integer"
          },
          "price":{
            "type":"scaled_float",
            "scaling_factor":100
          },
          "sold_count":{
            "type":"integer"
          },
          "review_count":{
            "type":"integer"
          },
          "status":{
            "type":"integer"
          },
          "create_time" : {
              "type" : "date"
          },
          "last_time" : {
              "type" : "date"
          }
        }
      }
    }
    
  2. 修改laravel隊(duì)列驅(qū)動(dòng)為Redis

    # 在.env文件中修改
    QUEUE_CONNECTION=redis
    # 如果要修改更多默認(rèn)配置在 config/queue.php 文件中
    
  3. 在商品模型(App\Models\Product.php)中配置

    /**
    * 取出要同步到 es中的數(shù)據(jù)
    * @return array
    */
    public function toESArray()
    {
        $arr = Arr::only($this->toArray(), [
            'id',
            'name',
            'long_name',
            'brand_id',
            'category_id',
            'shop_id',
            'price',
            'sold_count',
            'review_count',
            'status',
            'create_time',
            'last_time'
        ]);
    
        return $arr;
    }
    
  4. 創(chuàng)建監(jiān)聽任務(wù)

    php artisan make:job SyncProductToES
    
  5. 編寫任務(wù)中的代碼

    <?php
    
    namespace App\Jobs;
    
    use App\Models\Product;
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Queue\SerializesModels;
    
    class SyncProductToES implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        protected $product;
    
        /**
         * Create a new job instance.
         *
         * @return void
         */
        public function __construct(Product $product)
        {
            $this->product = $product;
        }
    
        /**
         * Execute the job.
         *
         * @return void
         */
        public function handle()
        {
            $data = $this->product->toESArray();
            app('es')->index([
                'index' => 'products',
                'type'  => '_doc',
                'id'    => $data['id'],
                'body'  => $data,
            ]);
        }
    }
    
  6. 在需要數(shù)據(jù)同步的地方觸發(fā)這個(gè)任務(wù)

    $form->saved(function (Form $form) {
        $product = $form->model();
        dispatch(new SyncProductToES($product));
    });
    
  7. 啟動(dòng)隊(duì)列

    php artisan queue:work
    
  8. 將mysql中已有的數(shù)據(jù)導(dǎo)入到es中

    上述一系列操作,可以實(shí)現(xiàn)增量同步,在每次新增數(shù)據(jù)時(shí)都會(huì)寫入es。舊數(shù)據(jù)的全量同步我這里通過創(chuàng)建一個(gè) Artisan 命令來實(shí)現(xiàn)。

    創(chuàng)建命令

    php artisan make:command Elasticsearch/SyncProducts
    

    編寫代碼

    <?php
    
    namespace App\Console\Commands\Elasticsearch;
    
    use App\Models\Product;
    use Illuminate\Console\Command;
    
    class SyncProducts extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'es:sync-products';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = '將商品數(shù)據(jù)同步到 Elasticsearch';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         */
        public function handle()
        {
            // 獲取 es 對(duì)象
            $es = app('es');
    
            Product::query()
                // 使用 chunkById 避免一次性加載過多數(shù)據(jù)
                ->chunkById(100, function ($products) use ($es) {
                    $this->info(sprintf('正在同步 ID 范圍為 %s 至 %s 的商品', $products->first()->id, $products->last()->id));
                    // 初始化請(qǐng)求體
                    $req = ['body' => []];
                    // 遍歷商品
                    foreach ($products as $product) {
                        // 將商品模型轉(zhuǎn)為 es 所用的數(shù)組
                        $data = $product->toESArray();
    
                        $req['body'][] = [
                            'index' => [
                                '_index' => 'products',
                                '_type'  => '_doc',
                                '_id'    => $data['id'],
                            ],
                        ];
                        $req['body'][] = $data;
                    }
                    try {
                        // 使用 bulk 方法批量創(chuàng)建
                        $es->bulk($req);
                    } catch (\Exception $e) {
                        $this->error($e->getMessage());
                    }
                });
            $this->info('同步完成');
        }
    }
    

    測試命令

     php artisan es:sync-products
    
  9. 線上部署

    在生產(chǎn)環(huán)境中,一般需要安裝 Horizon 隊(duì)列管理工具Supervisor 進(jìn)程監(jiān)視器 來更好的管理隊(duì)列以及提高穩(wěn)定性。這兩款工具的安裝配置直接看laravel官方文檔就好,寫的很詳細(xì):https://learnku.com/docs/laravel/7.x/horizon/7514

第二種方案:使用 go-mysql-elasticsearch 工具

go-mysql-elasticsearch是一款開源的高性能的MySQL數(shù)據(jù)同步到ES的工具,由go語言開發(fā),編譯及使用非常簡單。

原理:使用mysqldump獲取當(dāng)前MySQL的數(shù)據(jù),然后再通過此時(shí)binlog的name和position獲取增量數(shù)據(jù),再根據(jù)binlog構(gòu)建restful api寫入數(shù)據(jù)到ES中。

這種方案的好處是數(shù)據(jù)同步性能非常高,而且與業(yè)務(wù)代碼完全解耦;缺點(diǎn)是增加了開發(fā)成本,使用相對(duì)復(fù)雜,需要安裝go語言的運(yùn)行環(huán)境,在多表關(guān)聯(lián)同步下操作比較繁瑣

注意事項(xiàng):(很重要,一定要看)

  1. GitHub文檔中說使用的版本要求是:MySQL < 8.0 ES < 6.0

    但經(jīng)過測試,我的版本是 MySQL:8.0.26,ES:7.12.1,也可以實(shí)現(xiàn)增量同步。只不過不能用mysqldump來同步舊數(shù)據(jù),因?yàn)镸ySQL8.0之后與之前版本相比改變挺多,目前的 go-mysql-elasticsearch 版本還不支持MySQL8.0的mysqldump

  2. MySQL binlog 格式必須是ROW模式

    必須在MySQL配置文件中修改此參數(shù),改為row:binlog_format=row

  3. 要同步的MySQL數(shù)據(jù)表必須包含主鍵,否則直接忽略。這是因?yàn)槿绻麛?shù)據(jù)表沒有主鍵,UPDATE和DELETE操作就會(huì)因?yàn)樵贓S中找不到對(duì)應(yīng)的document而無法進(jìn)行同步

  4. 在 go-mysql-elasticsearch 運(yùn)行時(shí)不能更改MySQL表結(jié)構(gòu)

  1. 安裝 go

    官網(wǎng)下載地址,自行選擇版本:https://golang.google.cn/dl/

    [root@VM-0-8-centos]# wget https://golang.google.cn/dl/go1.15.5.linux-amd64.tar.gz
    [root@VM-0-8-centos]# tar -C /usr/local -zxvf go1.15.5.linux-amd64.tar.gz
    

    或者centos下直接使用yum源安裝

    yum install -y go
    

    配置環(huán)境變量(GOPATH 是go項(xiàng)目代碼放置的目錄)

    [root@VM-0-8-centos go]# vim /etc/profile
    
    export GOROOT=/usr/local/go
    export GOPATH=/usr/local/app/go
    export PATH=$PATH:/usr/local/go/bin
    
    [root@VM-0-8-centos go]# source /etc/profile
    

    測試,查看go版本

    [root@VM-0-8-centos]# go version
    go version go1.15.5 linux/amd64
    [root@VM-0-8-centos]#
    
  2. 安裝 go-mysql-elasticsearch

    安裝依賴包

    yum install -y gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel
    

    安裝 go-mysql-elasticsearch

    PS:因?yàn)镚itHub國內(nèi)時(shí)常上不去,所以這條命令如果拉取失敗的話就翻個(gè)墻,去GitHub下載安裝包

    go get github.com/siddontang/go-mysql-elasticsearch
    

    下載完成后會(huì)存放到上邊環(huán)境變量中配置的項(xiàng)目地址中,進(jìn)入執(zhí)行 make 操作

    [root@VM-0-8-centos ~]# cd $GOPATH/src/github.com/siddontang/go-mysql-elasticsearch
    [root@VM-0-8-centos go-mysql-elasticsearch]# ls
    clear_vendor.sh  cmd  Dockerfile  elastic  etc  go.mod  go.sum  LICENSE  Makefile  README.md  river
    [root@VM-0-8-centos go-mysql-elasticsearch]# make
    

    安裝完成修改配置文件,配置文件路徑就是下載的這個(gè)安裝包的 etc 目錄下

    需要修改的地方我都加了注釋,其他的配置用默認(rèn)的即可

    [root@VM-0-8-centos go-mysql-elasticsearch]# vim etc/river.toml
    
    # MySQL address, user and password
    # user must have replication privilege in MySQL.
    my_addr = "127.0.0.1:3306"  # mysql地址與端口
    my_user = "root"         # mysql用戶名  
    my_pass = ""             # mysql密碼
    my_charset = "utf8"          # mysql字符集
    
    # Set true when elasticsearch use https
    #es_https = false
    # Elasticsearch address  
    es_addr = "127.0.0.1:9200"  # es的地址與端口 
    # Elasticsearch user and password, maybe set by shield, nginx, or x-pack
    es_user = ""                # es用戶名,沒有默認(rèn)為空即可
    es_pass = ""             # es密碼,沒有默認(rèn)為空即可
    
    # Path to store data, like master.info, if not set or empty,
    # we must use this to support breakpoint resume syncing. 
    # TODO: support other storage, like etcd. 
    data_dir = "./var"           # 數(shù)據(jù)存儲(chǔ)目錄
    
    # Inner Http status address
    stat_addr = "127.0.0.1:12800"
    stat_path = "/metrics"
    
    # pseudo server id like a slave 
    server_id = 1001
    
    # mysql or mariadb
    flavor = "mysql"
    
    # mysqldump execution path
    # if not set or empty, ignore mysqldump.
    mysqldump = "mysqldump"      # 如果設(shè)置為空,則不會(huì)同步mysql中現(xiàn)有的舊數(shù)據(jù)
    
    # if we have no privilege to use mysqldump with --master-data,
    # we must skip it.
    #skip_master_data = false
    
    # minimal items to be inserted in one bulk
    bulk_size = 128
    
    # force flush the pending requests if we don't have enough items >= bulk_size
    flush_bulk_time = "200ms"
    
    # Ignore table without primary key
    skip_no_pk_table = false
    
    # MySQL data source
    [[source]]
    schema = "test"      # 需要同步的mysql數(shù)據(jù)庫
    
    # Only below tables will be synced into Elasticsearch.
    # "t_[0-9]{4}" is a wildcard table format, you can use it if you have many sub tables, like table_0000 - table_1023
    # I don't think it is necessary to sync all tables in a database.
    tables = ["t", "t_[0-9]{4}", "tfield", "tfilter"] # 需要同步的mysql數(shù)據(jù)表
    
    # Below is for special rule mapping
    
    # Very simple example
    # 
    # desc t;
    # +-------+--------------+------+-----+---------+-------+
    # | Field | Type         | Null | Key | Default | Extra |
    # +-------+--------------+------+-----+---------+-------+
    # | id    | int(11)      | NO   | PRI | NULL    |       |
    # | name  | varchar(256) | YES  |     | NULL    |       |
    # +-------+--------------+------+-----+---------+-------+
    # 
    # The table `t` will be synced to ES index `test` and type `t`.
    # 定義mysql和es同步的對(duì)應(yīng)關(guān)系,有幾個(gè)寫幾個(gè),下邊多余的可以刪掉
    [[rule]]
    schema = "test"      # 需要同步的mysql數(shù)據(jù)庫
    table = "t"          # 需要同步的mysql數(shù)據(jù)表
    index = "test"       # 需要同步的es索引
    type = "t"           # 需要同步的es類型,es7之后類型只有一種,只能設(shè)為 _doc
    
    # Wildcard table rule, the wildcard table must be in source tables 
    # All tables which match the wildcard format will be synced to ES index `test` and type `t`.
    # In this example, all tables must have same schema with above table `t`;
    [[rule]]
    schema = "test"
    table = "t_[0-9]{4}"
    index = "test"
    type = "t"
    
    # Simple field rule 
    #
    # desc tfield;
    # +----------+--------------+------+-----+---------+-------+
    # | Field    | Type         | Null | Key | Default | Extra |
    # +----------+--------------+------+-----+---------+-------+
    # | id       | int(11)      | NO   | PRI | NULL    |       |
    # | tags     | varchar(256) | YES  |     | NULL    |       |
    # | keywords | varchar(256) | YES  |     | NULL    |       |
    # +----------+--------------+------+-----+---------+-------+
    #
    [[rule]]
    schema = "test"
    table = "tfield"
    index = "test"
    type = "tfield"
    
    # 這個(gè)配置是定義mysql中的字段對(duì)應(yīng)es中的字段,如果全都一致可以刪掉這個(gè)配置
    [rule.field]
    # Map column `id` to ES field `es_id`
    id="es_id"       # 這個(gè)就是指mysql中的id字段對(duì)應(yīng)es中的es_id字段,下邊同理
    # Map column `tags` to ES field `es_tags` with array type 
    tags="es_tags,list"
    # Map column `keywords` to ES with array type
    keywords=",list"
    
    # Filter rule 
    #
    # desc tfilter;
    # +-------+--------------+------+-----+---------+-------+
    # | Field | Type         | Null | Key | Default | Extra |
    # +-------+--------------+------+-----+---------+-------+
    # | id    | int(11)      | NO   | PRI | NULL    |       |
    # | c1    | int(11)      | YES  |     | 0       |       |
    # | c2    | int(11)      | YES  |     | 0       |       |
    # | name  | varchar(256) | YES  |     | NULL    |       |
    # +-------+--------------+------+-----+---------+-------+
    #
    [[rule]]
    schema = "test"
    table = "tfilter"
    index = "test"
    type = "tfilter"
    
    # Only sync following columns
    filter = ["id", "name"]      # 指定mysql中哪些字段需要同步
    
    # id rule
    #
    # desc tid_[0-9]{4};
    # +----------+--------------+------+-----+---------+-------+
    # | Field    | Type         | Null | Key | Default | Extra |
    # +----------+--------------+------+-----+---------+-------+
    # | id       | int(11)      | NO   | PRI | NULL    |       |
    # | tag      | varchar(256) | YES  |     | NULL    |       |
    # | desc     | varchar(256) | YES  |     | NULL    |       |
    # +----------+--------------+------+-----+---------+-------+
    #
    [[rule]]
    schema = "test"
    table = "tid_[0-9]{4}"
    index = "test"
    type = "t"
    # The es doc's id will be `id`:`tag`
    # It is useful for merge muliple table into one type while theses tables have same PK 
    id = ["id", "tag"]
    

    再提供個(gè)本次測試使用的配置文件,去掉了所有的注釋,這樣看起來簡潔一點(diǎn)

    my_addr = "172.17.0.4:3306"  
    my_user = "root"
    my_pass = "root"
    my_charset = "utf8"
    
    es_addr = "172.17.0.7:9200"
    es_user = ""
    es_pass = ""
    
    data_dir = "/docker/data"
    
    stat_addr = "127.0.0.1:12800"
    stat_path = "/metrics"
    server_id = 1001
    flavor = "mysql"
    mysqldump = ""
    bulk_size = 128
    flush_bulk_time = "200ms"
    skip_no_pk_table = false
    
    [[source]]
    schema = "lmrs"
    tables = ["lmrs_products"]
    
    [[rule]]
    schema = "lmrs"
    table = "lmrs_products"
    index = "products"
    type = "_doc"
    filter = ["id","name","long_name","brand_id","shop_id","price","sold_count","review_count","status","create_time","last_time","three_category_id"]
    
    [rule.field]
    mysql = "three_category_id"
    elastic = "category_id"
    

    啟動(dòng) go-mysql-elasticsearch,輸出以下信息證明成功

    [root@VM-0-8-centos go-mysql-elasticsearch]# ./bin/go-mysql-elasticsearch -config=./etc/river.toml
    [2021/08/01 13:37:06] [info] binlogsyncer.go:141 create BinlogSyncer with config {1001 mysql 127.0.0.1 3306 root   utf8mb4 false false <nil> false UTC false 0 0s 0s 0 false 0}
    [2021/08/01 13:37:06] [info] dump.go:180 skip dump, use last binlog replication pos (mysql-bin.000001, 2606) or GTID set <nil>
    [2021/08/01 13:37:06] [info] binlogsyncer.go:362 begin to sync binlog from position (mysql-bin.000001, 2606)
    [2021/08/01 13:37:06] [info] binlogsyncer.go:211 register slave for master server 127.0.0.1:3306
    [2021/08/01 13:37:06] [info] sync.go:25 start sync binlog at binlog file (mysql-bin.000001, 2606)
    [2021/08/01 13:37:06] [info] binlogsyncer.go:731 rotate to (mysql-bin.000001, 2606)
    [2021/08/01 13:37:06] [info] sync.go:71 rotate binlog to (mysql-bin.000001, 2606)
    [2021/08/01 13:37:06] [info] master.go:54 save position (mysql-bin.000001, 2606)
    
  3. 如果覺得上述兩步太麻煩,可以直接使用docker來安裝 go-mysql-elasticsearch,鏡像中自帶了go語言環(huán)境

    拉取鏡像

    docker pull gozer/go-mysql-elasticsearch
    

    構(gòu)建容器,其中 river.toml 配置文件與上邊的內(nèi)容一樣

    docker run -p 12345:12345 -d --name go-mysql-es -v /docker/go-mysql-es/river.toml:/config/river.toml --privileged=true gozer/go-mysql-elasticsearch
    

第三種方案:使用 Logstash 工具

Logstash 是免費(fèi)且開放的服務(wù)器端數(shù)據(jù)處理管道,能夠從多個(gè)來源采集數(shù)據(jù),轉(zhuǎn)換數(shù)據(jù),然后將數(shù)據(jù)發(fā)送到您最喜歡的“存儲(chǔ)庫”中,可與各種部署集成。 它提供了大量插件,可幫助你解析,豐富,轉(zhuǎn)換和緩沖來自各種來源的數(shù)據(jù)。 如果你的數(shù)據(jù)需要 Beats 中沒有的其他處理,則需要將 Logstash 添加到部署中。

這個(gè)工具不止可以用來做mysql到es的數(shù)據(jù)同步,它的應(yīng)用場景還有:日志搜索器( logstash采集、處理、轉(zhuǎn)發(fā)到elasticsearch存儲(chǔ),在kibana進(jìn)行展示)、elk日志分析(elasticsearch + logstash + kibana)等。

它既可以全量同步舊數(shù)據(jù),也可以增量同步新數(shù)據(jù),而且對(duì)mysql和es沒有版本方面的限制,只需對(duì)應(yīng)版本即可

  1. 安裝

    官方下載地址:https://www.elastic.co/cn/downloads/past-releases#logstash

    PS: logstash 的版本一定要和 es 保持一致,我的 es 是 7.12.1 版本,所以 logstash 也下載的 7.12.1 版本

    wget https://artifacts.elastic.co/downloads/logstash/logstash-7.12.1-linux-x86_64.tar.gz
    

    也可以直接使用docker安裝,更方便

    docker pull logstash:7.12.1
    
  2. 安裝兩個(gè)插件

    logstash-input-jdbc:連接讀取mysql中數(shù)據(jù)的插件(6.0之后的版本已經(jīng)自帶了,再次安裝會(huì)提示報(bào)錯(cuò))

    logstash-output-elasticsearch:數(shù)據(jù)輸出到es的插件

    [root@localhost]# tar -C /usr/local -zxvf logstash-7.12.1-linux-x86_64.tar.gz
    [root@localhost]# cd /usr/local/logstash-7.12.1/bin
    [root@localhost bin]# ./logstash-plugin install logstash-input-jdbc
    ...
    ERROR: Installation aborted, plugin 'logstash-input-jdbc' is already provided by 'logstash-integration-jdbc'
    [root@localhost bin]# ./logstash-plugin install logstash-output-elasticsearch
    ...
    Installation successful
    [root@localhost bin]#
    
  3. 下載 jdbc 的 mysql-connection.jar 包,版本與自己的 mysql 版本保持一致

    [root@localhost logstash-7.12.1]# mkdir pipeline
    [root@localhost logstash-7.12.1]# cd pipeline/
    [root@localhost pipeline]# wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.26/mysql-connector-java-8.0.26.jar
    
  4. 更改配置文件

    [root@localhost logstash-7.12.1]# vi config/logstash.yml
    # 加入以下內(nèi)容,下邊那個(gè)是es的地址,根據(jù)自己的情況改
    http.host: "0.0.0.0"
    xpack.monitoring.elasticsearch.hosts: ["http://172.17.0.2:9200"]
    
    [root@localhost logstash-7.12.1]# vi config/pipelines.yml
    # 加入以下內(nèi)容,路徑同樣也是根據(jù)自己實(shí)際的來
    pipeline.id: table1
    path.config: "/usr/local/logstash-7.12.1/pipeline/logstash.config"
    
  5. 創(chuàng)建上邊配置里的指定的配置文件 logstash.config

    vi pipeline/logstash.config

    input {
        stdin {}
        # 可以有多個(gè)jdbc,來同步不同的數(shù)據(jù)表
        jdbc {
            # 類型,區(qū)分開每個(gè) jdbc,以便輸出的時(shí)候做判斷
            type => "product"
            # 注意mysql連接地址一定要用ip,不能使用localhost等
            jdbc_connection_string => "jdbc:mysql://172.17.0.4:3306/lmrs"
            jdbc_user => "root"
            jdbc_password => "root"
            # 數(shù)據(jù)庫重連嘗試次數(shù)
            connection_retry_attempts => "3"
            # 數(shù)據(jù)庫連接校驗(yàn)超時(shí)時(shí)間,默認(rèn)為3600s
            jdbc_validation_timeout => "3600"
            # 這個(gè)jar包就是上邊下載那個(gè),可以是絕對(duì)路徑也可以是相對(duì)路徑,把地址寫對(duì)
            jdbc_driver_library => "/usr/local/logstash-7.12.1/pipeline/mysql-connector-java-8.0.26.jar"
            # 驅(qū)動(dòng)類名
            jdbc_driver_class => "com.mysql.jdbc.Driver"
            # 開啟分頁,默認(rèn)是 false
            jdbc_paging_enabled => "true"
            # 單次分頁查詢條數(shù)(默認(rèn)100000,字段較多的話,可以適當(dāng)調(diào)整這個(gè)數(shù)值)
            jdbc_page_size => "50000"
            # 要執(zhí)行的sql,從這查出的數(shù)據(jù)就會(huì)同步到es中
            statement => "select id,`name`,long_name,brand_id,three_category_id as category_id,shop_id,price,status,sold_count,review_count,create_time,last_time from lmrs_products"
            # 執(zhí)行的sql文件路徑,這與上邊的 statement 參數(shù) 二選一
            # statement_filepath => "/usr/local/logstash-7.12.1/pipeline/products.sql"
            # 是否將字段名轉(zhuǎn)為小寫,默認(rèn)為true(如果具備序列化或者反序列化,建議設(shè)置為false)
            lowercase_column_names => false
            # 需要記錄查詢結(jié)果某字段的值時(shí),此字段為true,否則默認(rèn)tracking_colum為timestamp的值
            use_column_value => true
            # 需要記錄的字段,同于增量同步,需要是數(shù)據(jù)庫字段
            tracking_column => id
            # 記錄字段的數(shù)據(jù)類型
            tracking_column_type => numeric
            # 上次數(shù)據(jù)存放位置
            record_last_run => true
            # 上一個(gè)sql_last_value的存放路徑,必須在文件中指定字段的初始值,手動(dòng)創(chuàng)建文件并賦予讀寫權(quán)限
            last_run_metadata_path => "/usr/local/logstash-7.12.1/pipeline/products.txt"
            # 是否清除last_run_metadata_path的記錄,需要增量同步這個(gè)字段的值必須為false
            clean_run => false
            # 設(shè)置定時(shí)任務(wù)間隔  含義:分、時(shí)、天、月、年,全部為*默認(rèn)為每分鐘跑一次任務(wù)
            schedule => "* * * * *"
        }
    }
    output {
        # 判斷類型
        if [type] == "product" {
            # es的配置
            elasticsearch {
                hosts => "172.17.0.2:9200"
                index => "products"
                document_type => "_doc"
                document_id => "%{id}"
            }
        }
    
        # 日志輸出
        stdout {
            codec => json_lines
        }
    }
    
  6. 啟動(dòng) Logstash(--config.reload.automatic 選項(xiàng)啟用自動(dòng)配置重新加載,不必在每次修改配置文件時(shí)停止并重新啟動(dòng) Logstash)

    [root@localhost logstash-7.12.1]# ./bin/logstash -f pipeline/logstash.config --config.reload.automatic
    

    瀏覽器訪問 ip:9600 可以打印出以下信息證明啟動(dòng)成功

    {"host":"localhost.localdomain","version":"7.12.1","http_address":"0.0.0.0:9600","id":"15320442-569b-4bfd-a0d6-4c71619bc06d","name":"localhost.localdomain","ephemeral_id":"f6868c4c-fff1-4b6a-89d9-4ca7ea469c6e","status":"green","snapshot":false,"pipeline":{"workers":4,"batch_size":125,"batch_delay":50},"build_date":"2021-04-20T19:51:54Z","build_sha":"a0a95c823ae2da19a75f44a01784665e7ad23d15","build_snapshot":false}
    

總結(jié)

go-mysql-elasticsearch 和 Logstash 工具都可以放到 Supervisor 中來管控,來提高穩(wěn)定性。

這三種方案總的來說各有利弊,至于選擇哪種個(gè)人認(rèn)為:如果項(xiàng)目不是特別大,數(shù)據(jù)量增長速度也不快,對(duì)性能沒太高的要求的話可以考慮第一種,因?yàn)閷?shí)現(xiàn)簡單,利于維護(hù);如果對(duì)雙方同步性能要求比較高,或者數(shù)據(jù)量很大的情況下,就考慮后兩種。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,702評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,143評(píng)論 3 415
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,553評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,620評(píng)論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,416評(píng)論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,940評(píng)論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,024評(píng)論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,170評(píng)論 0 287
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,709評(píng)論 1 333
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,597評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,784評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,291評(píng)論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,029評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,407評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,663評(píng)論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,403評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,746評(píng)論 2 370

推薦閱讀更多精彩內(nèi)容