zhongrj
2024-02-23 1af3f0036d8a0ede8f97ecb2075bee43109a4977
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package org.springblade.binlog.listener;
 
 
import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springblade.binlog.config.DataSourceConfig;
import org.springblade.binlog.constant.BinLogConstants;
import org.springblade.binlog.util.BinLogUtils;
import org.springblade.binlog.vo.BinLogItem;
import org.springblade.binlog.vo.DataProperty;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * 乐游监听器
 * SpringBoot启动成功后的执行业务线程操作
 * CommandLineRunner去实现此操作
 * 在有多个可被执行的业务时,通过使用 @Order 注解,设置各个线程的启动顺序(value值由小到大表示启动顺序)。
 * 多个实现CommandLineRunner接口的类必须要设置启动顺序,不让程序启动会报错!
 *
 * @author zrj
 * @since 2021/7/27
 **/
@Slf4j
@Component
@Order(value = 1)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "binlog.enabled")
public class TourBinLogListener implements CommandLineRunner {
 
    @Resource
    private BinLogConstants binLogConstants;
 
    @Override
    public void run(String... args) throws Exception {
        log.info("初始化配置信息:" + binLogConstants.toString());
 
        // 初始化配置信息
        DataSourceConfig conf = new DataSourceConfig(binLogConstants.getHost(),
            binLogConstants.getPort(),
            binLogConstants.getUsername(),
            binLogConstants.getPassword());
 
        // 初始化监听器
        MysqlBinLogListener mysqlBinLogListener = new MysqlBinLogListener(conf);
 
        // 获取table集合
        List<String> tableList = BinLogUtils.getListByStr(binLogConstants.getTable());
        if (CollectionUtil.isEmpty(tableList)) {
            return;
        }
        // 注册监听
        tableList.forEach(table -> {
            log.info("注册监听信息,注册DB:" + binLogConstants.getDb() + ",注册表:" + table);
            try {
                mysqlBinLogListener.regListener(binLogConstants.getDb(), table, item -> {
                    if (item.getEventType().name().equals("EXT_WRITE_ROWS")){
                        // 新增处理逻辑
                        saveHandle(item);
                    }
                    if (item.getEventType().name().equals("EXT_UPDATE_ROWS")){
                        // 更新处理逻辑
                        updateHandle(item);
                    }
                    if (item.getEventType().name().equals("EXT_DELETE_ROWS")){
                        // 删除处理逻辑
                        deletedHandle(item);
                    }
                });
            } catch (Exception e) {
                log.error("BinLog监听异常:" + e);
            }
        });
        // 多线程消费
        mysqlBinLogListener.parse();
    }
 
    /**
     * 新增处理逻辑
     * @param item
     */
    private void saveHandle(BinLogItem item) {
        String tableName = item.getDbTable().split("-")[1];
        Map<String, Serializable> data = item.getAfter();
        log.info(tableName);
        Map<String, DataProperty> dataProperty = item.getDataProperty();
 
        List<String> keyList = new ArrayList<>();
        List<String> valueList = new ArrayList<>();
        dataProperty.forEach((key,value)->{
            log.info(key);
            log.info(value.dataType);
            if(null!=data.get(key)) {
                log.info(data.get(key).toString());
                valueList.add(data.get(key).toString());
            }else {
                if (value.dataType.equals("int") ||
                    value.dataType.equals("bigint") ||
                    value.dataType.equals("datetime") ||
                    value.dataType.equals("date")){
                    valueList.add(null);
                }else{
                    valueList.add("");
                }
            }
            keyList.add(key);
        });
        // 拼接sql
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("insert into ")
            .append(tableName)
            .append("(")
            .append(String.join(",",keyList))
            .append(") ")
            .append(" values (")
            .append(String.join(",",valueList))
            .append(");");
        log.info("sql: " + sqlBuilder.toString());
    }
 
    /**
     * 修改处理逻辑
     * @param item
     */
    private void updateHandle(BinLogItem item) {
    }
 
    /**
     * 删除处理逻辑
     * @param item 数据
     */
    private void deletedHandle(BinLogItem item) {
    }
}