一、环境准备
1、环境信息
192.168.184.192
2、关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
二、基础环境安装
1、安装postgresql数据库
我这里安装的是postgresql14,其他版本也可以,安装可参考:
CentOS7.5【脚本编译】安装PostgreSQL14.8
(1)修改配置文件
postgresql.conf调整wal_level属性为logical,该属性需要重启才能生效
cd /data/pg14/db
vim postgresql.conf
修改为如下内容
wal_level = logical
shared_preload_libraries = 'wal2json.so'
(2)重启数据库
这里要等wal2json安装好再进行重启,不然会报错
su - mydba
pg_ctl restart
pg_ctl status
(3)查看参数
psql -d postgres
show wal_level;
2、安装jdk
3、安装zookeeper
4、安装kafka
(1)修改配置文件
cd /data/soft/kafka_2.12-2.4.1/config/
vi server.properties
修改如下内容:
listeners=PLAINTEXT://:9092
advertised.listeners=PLAINTEXT://192.168.184.192:9092
(2)重启kafka
jps -m
kill -9 进程id
cd /data/soft/kafka_2.12-2.4.1/
bin/kafka-server-start.sh -daemon config/server.properties
(3)测试kafka
cd /data/soft/kafka_2.12-2.4.1/
新增topic
bin/kafka-topics.sh --create --zookeeper 192.168.184.192:2181 --partitions 1 --replication-factor 1 --topic hello
查询topic
bin/kafka-topics.sh --list --zookeeper 192.168.184.192:2181
删除topic
bin/kafka-topics.sh --delete --zookeeper 192.168.184.192:2181 --topic hello
三、安装kafkacat
1、安装依赖
yum install -y librdkafka-devel
2、下载kafkacat
(1)git下载
cd /root/
yum -y install git
git clone https://github.com/edenhill/kafkacat
(2)官网下载
https://github.com/edenhill/kcat
3、编译安装
cd /root/kafkacat
./configure
make -j 24
make install -j 24
kcat --help
四、安装wal2json插件
1、下载地址
(1)官网地址
https://github.com/eulerto/wal2json/releases/tag/wal2json_2_3
(2)网盘地址
链接: https://pan.baidu.com/s/1hApchE38ZmYpYJAGohl1Jw?pwd=da4b
提取码: da4b
2、上传安装包
cd /root/
ll
tar -xf wal2json-wal2json_2_3.tar.gz
3、编译安装
cd /root/wal2json-wal2json_2_3
make -j 24
make install -j 24
4、重启postgresql数据库
之前重启会失败的,现在重启
su - mydba
pg_ctl restart
pg_ctl status
五、进行测试
1、创建复制槽
(1)创建复制槽
su - mydba
pg_recvlogical -d postgres --slot slot1 --create-slot -P wal2json
(2)查看复制槽
su - mydba
psql -d postgres -c "select * from pg_replication_slots;"
2、创建测试表
psql -d postgres
CREATE TABLE test_table (
id char(10) NOT NULL,
code char(10),
PRIMARY KEY (id)
);
\d
3、pg_recvlogical+kafkacat发送数据
pg_recvlogical读取增量数据,并通过kafkacat发送数据到指定的kafka的topic
pg_recvlogical -h 192.168.184.192 -p 5432 -U mydba -d postgres -S slot1 --start -f - | /root/kafkacat/kcat -b 192.168.184.192:9092 -t testdb_topic
参数解释:
4、制造数据
INSERT INTO test_table (id, code)
VALUES
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5'),
('6', '6'),
('7', '7'),
('8', '8'),
('9', '9'),
('10', '10');
5、kafka进行消费数据
cd /data/soft/kafka_2.12-2.4.1
bin/kafka-console-consumer.sh --topic testdb_topic --bootstrap-server 192.168.184.192:9092 --from-beginning