Refactor CDC Debezium Source
- Add AVRO format support. Defaults to JSON - Remove the Metadata repo configuration. - Remove the Spring property sort-cuts. Only the native Debezium properties are used. - change the native properties prefix from `cdc.config to cdc.debezium`. - Removes the need to fork and modify the Debezium engine code. - Add binding name strategy. Remove old CDC projects and replace with the new implementation Remove duplicate pom properties remove non existing pom module Rename `cdc-debezium-supplier/source` to `debezium-supplier/source` streamline naming and tests streamline supplier tests test streamline. Github workflow Fix Debezium engine generics mess Break `DebeziumConfiguration` into engine auto-config and consumer configuration add initial reactive consumer impl. Splits Debezium into a pure supplier and a streaming source - Remove any SCS related dependencies from the supplier. - Supplier uses Sinks.Many. complete the supplier vs source separation Improve Debezium Egine auto-conf - Provide configuration hooks for OffsetCommitPolicy, ConnectorCallback, CompletionCallback and Clock with default implementations. - Remove obsolete code. minor improvements Rename cdc property prefix to `debezium` and `cdc.debezium` to `debezium.inner` remove obsolete `StreamBridge` tests address reviews. improve testing Supplier tests. Rename `inner` to `properties`. Streamline the README. Minor improvements streamline supplier tests final fixes
This commit is contained in:
@@ -1,124 +0,0 @@
|
||||
= Debezium Spring Boot Starter
|
||||
|
||||
Spring Boot Starter for easy integration of https://debezium.io[Debezium] in Spring Boot applications.
|
||||
|
||||
To use it you just add the `cdc-debezium-boot-starter` dependency to your application POM and implement your own `Consumer<SourceRecord>` handler to process the incoming database change events. Follow the link below for further instructions.
|
||||
|
||||
https://en.wikipedia.org/wiki/Change_data_capture[Change Data Capture] (CDC) Spring Boot Starter that allows capturing change events from various databases including `MySQL`, `PostgreSQL`, `MongoDB`, `Oracle` and `SQL Server`.
|
||||
|
||||
== Quick Start
|
||||
|
||||
https://start.spring.io/[Start new Spring Boot project] and add the following starter dependency to activate the CDC:
|
||||
|
||||
[source, xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud.fn</groupId>
|
||||
<artifactId>cdc-debezium-boot-starter</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
NOTE: for MySQL you need to adjust the mysql client version to `mysql:mysql-connector-java:8.0.13`.
|
||||
|
||||
The CDC starter setups an embedded https://debezium.io/documentation/reference/0.10/connectors/index.html[debezium connector] to capture the database changes.
|
||||
|
||||
Then implement a `Consumer<SourceRecord>` bean handler to receive and process the CDC notifications:
|
||||
|
||||
[source, java]
|
||||
----
|
||||
@Bean
|
||||
public Consumer<SourceRecord> mySourceRecordConsumer( # <1>
|
||||
Function<SourceRecord, byte[]> keySerializer, # <2>
|
||||
Function<SourceRecord, byte[]> valueSerializer) { # <3>
|
||||
|
||||
return sourceRecord -> {
|
||||
System.out.println(sourceRecord.topic() # <4>
|
||||
+ " : " + new String(keySerializer.apply(sourceRecord)) # <5>
|
||||
+ " : " + new String(valueSerializer.apply(sourceRecord))); # <6>
|
||||
};
|
||||
}
|
||||
----
|
||||
|
||||
<1> CDC event listener that is called on every data change.
|
||||
<2> Optional keySerializer converter that allows to serialize the keys of the SourceRecord into a compact binary format.
|
||||
<3> Optional valueSerializer converter (by default JsonConverter) that allows to serialize the SourceRecord into a compact binary format.
|
||||
<4> The `topic()` provides a logical identifier of the changed data (e.g. cdc-name.DB-name.table-name)
|
||||
<5> Convert kay to text message using the keySerializer
|
||||
<6> Convert payload to JSON message using the valueSerializer.
|
||||
|
||||
NOTE: you may have to add `@AutoConfigureBefore(CdcAutoConfiguration.class)` to your Boot class to ensure that your record consumer overrides the default handlers.
|
||||
|
||||
Above handler will produce output messages like this:
|
||||
[source, bash]
|
||||
----
|
||||
my-app-connector.inventory.addresses : {"id":10,"customer_id":1001,"street":"3183 Moore Avenue","city":"Euless","state":"Texas","zip":"76036","type":"SHIPPING"}
|
||||
my-app-connector.inventory.customers : {"id":1004,"first_name":"Anne","last_name":"Kretchmar","email":"annek@noanswer.org"}
|
||||
my-app-connector.inventory.orders : {"order_number":10001,"order_date":16816,"purchaser":1001,"quantity":1,"product_id":102}
|
||||
my-app-connector.inventory.products : {"id":109,"name":"spare tire","description":"24 inch spare tire","weight":22.200000762939453}
|
||||
my-app-connector.inventory.products_on_hand : {"product_id":101,"quantity":3}
|
||||
----
|
||||
|
||||
All Debezium connector properties are supported. Just prefix those properties with `cdc.config` prefix.
|
||||
|
||||
Following snipped shows how to configure the https://debezium.io/docs/connectors/mysql/[MySqlConnector] to connect to a local MySQL database run via the `debezium/example-mysql` docker image:
|
||||
[source, bash]
|
||||
----
|
||||
docker run -it --rm --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=debezium -e MYSQL_USER=mysqluser -e MYSQL_PASSWORD=mysqlpw debezium/example-mysql:1.0
|
||||
----
|
||||
|
||||
[TIP]
|
||||
====
|
||||
(optional) Use `mysql` client to connected to the database and to create a `debezium` user with required credentials:
|
||||
[source, bash]
|
||||
----
|
||||
docker run -it --rm --name mysqlterm --link mysql --rm mysql:5.7 sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
|
||||
mysql> GRANT SELECT, RELOAD, SHOW DATABASES, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'debezium' IDENTIFIED BY 'dbz';
|
||||
----
|
||||
====
|
||||
|
||||
Configure `cdc-debezium-boot-starter` for consuming cdc events from the MySQL:
|
||||
|
||||
[source,properties]
|
||||
----
|
||||
cdc.name=my-sql-connector # <1>
|
||||
|
||||
cdc.connector=mysql # <2>
|
||||
|
||||
cdc.config.database.server.id=85744 # <3>
|
||||
cdc.config.database.server.name=my-app-connector # <3>
|
||||
cdc.config.database.user=debezium # <3>
|
||||
cdc.config.database.password=dbz # <3>
|
||||
cdc.config.database.hostname=localhost # <3>
|
||||
cdc.config.database.port=3306 # <3>
|
||||
|
||||
cdc.schema=false # <4>
|
||||
|
||||
cdc.flattening.enabled=true # <5>
|
||||
----
|
||||
|
||||
<1> Metadata used to identify and dispatch the events received by this cdc consumer instance.
|
||||
<2> Configures the CDC Source to use https://debezium.io/docs/connectors/mysql/[MySqlConnector]. (equivalent to setting `cdc.config.connector.class=io.debezium.connector.mysql.MySqlConnector`).
|
||||
<3> Connector specific configurations. MySQL server logical name, connect location and access credentials.
|
||||
<4> Do not serialize the record's schema in the output messages.
|
||||
<5> Enables the https://debezium.io/docs/configuration/event-flattening/[CDC Event Flattening] feature.
|
||||
|
||||
The full list of properties:
|
||||
|
||||
==== Options
|
||||
|
||||
//tag::configuration-properties[]
|
||||
$$cdc.config$$:: $$Spring pass-trough wrapper for debezium configuration properties. All properties with a 'cdc.config.' prefix are native Debezium properties. The prefix is removed, converting them into Debezium io.debezium.config.Configuration.$$ *($$Map<String, String>$$, default: `$$<none>$$`)*
|
||||
$$cdc.connector$$:: $$Shortcut for the cdc.config.connector.class property. Either of those can be used as long as they do not contradict with each other.$$ *($$ConnectorType$$, default: `$$<none>$$`, possible values: `mysql`,`postgres`,`mongodb`,`oracle`,`sqlserver`)*
|
||||
$$cdc.flattening.add-fields$$:: $$Comma separated list of metadata fields to add to the flattened message. The fields will be prefixed with "__" or "__[<]struct]__", depending on the specification of the struct.$$ *($$String$$, default: `$$<none>$$`)*
|
||||
$$cdc.flattening.add-headers$$:: $$Comma separated list specify a list of metadata fields to add to the header of the flattened message. The fields will be prefixed with "__" or "__[struct]__".$$ *($$String$$, default: `$$<none>$$`)*
|
||||
$$cdc.flattening.delete-handling-mode$$:: $$Options for handling deleted records: (1) none - pass the records through, (2) drop - remove the records and (3) rewrite - add a '__deleted' field to the records.$$ *($$DeleteHandlingMode$$, default: `$$<none>$$`, possible values: `drop`,`rewrite`,`none`)*
|
||||
$$cdc.flattening.drop-tombstones$$:: $$By default Debezium generates tombstone records to enable Kafka compaction on deleted records. The dropTombstones can suppress the tombstone records.$$ *($$Boolean$$, default: `$$true$$`)*
|
||||
$$cdc.flattening.enabled$$:: $$Enable flattening the source record events (https://debezium.io/docs/configuration/event-flattening).$$ *($$Boolean$$, default: `$$true$$`)*
|
||||
$$cdc.name$$:: $$Unique name for this sourceConnector instance.$$ *($$String$$, default: `$$<none>$$`)*
|
||||
$$cdc.offset.commit-timeout$$:: $$Maximum number of milliseconds to wait for records to flush and partition offset data to be committed to offset storage before cancelling the process and restoring the offset data to be committed in a future attempt.$$ *($$Duration$$, default: `$$5000ms$$`)*
|
||||
$$cdc.offset.flush-interval$$:: $$Interval at which to try committing offsets. The default is 1 minute.$$ *($$Duration$$, default: `$$60000ms$$`)*
|
||||
$$cdc.offset.policy$$:: $$Offset storage commit policy.$$ *($$OffsetPolicy$$, default: `$$<none>$$`)*
|
||||
$$cdc.offset.storage$$:: $$Kafka connector tracks the number processed records and regularly stores the count (as "offsets") in a preconfigured metadata storage. On restart the connector resumes the reading from the last recorded source offset.$$ *($$OffsetStorageType$$, default: `$$<none>$$`, possible values: `memory`,`file`,`kafka`,`metadata`)*
|
||||
$$cdc.schema$$:: $$Include the schema's as part of the outbound message.$$ *($$Boolean$$, default: `$$false$$`)*
|
||||
//end::configuration-properties[]
|
||||
@@ -1,104 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud.fn</groupId>
|
||||
<artifactId>spring-functions-parent</artifactId>
|
||||
<version>4.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../spring-functions-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>cdc-debezium-boot-starter</artifactId>
|
||||
<name>cdc-debezium-boot-starter</name>
|
||||
<description>Change Data Capture (CDC) Debezium Boot Starter</description>
|
||||
|
||||
<properties>
|
||||
<version.debezium>1.9.6.Final</version.debezium>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud.fn</groupId>
|
||||
<artifactId>cdc-debezium-common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-mysql</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-mongodb</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-postgres</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-oracle</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-sqlserver</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.common.cdc;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.kafka.connect.source.SourceRecord;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@Import(CdcCommonConfiguration.class)
|
||||
public class CdcAutoConfiguration {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(CdcAutoConfiguration.class);
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public Consumer<SourceRecord> defaultSourceRecordConsumer() {
|
||||
return sourceRecord -> logger.info("[CDC Event]: " + ((sourceRecord == null) ? "null" : sourceRecord.toString()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EmbeddedEngineExecutorService embeddedEngine(EmbeddedEngine.Builder embeddedEngineBuilder,
|
||||
Consumer<SourceRecord> sourceRecordConsumer, Function<SourceRecord, SourceRecord> recordFlattening) {
|
||||
|
||||
EmbeddedEngine embeddedEngine = embeddedEngineBuilder
|
||||
.notifying(sourceRecord -> sourceRecordConsumer.accept(recordFlattening.apply(sourceRecord)))
|
||||
.build();
|
||||
|
||||
return new EmbeddedEngineExecutorService(embeddedEngine) {
|
||||
@PostConstruct
|
||||
@Override
|
||||
public void start() {
|
||||
super.start();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
org.springframework.cloud.fn.common.cdc.CdcAutoConfiguration
|
||||
@@ -1,119 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.common.cdc;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.jdbc.JdbcTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
|
||||
/**
|
||||
* @author Christian Tzolov
|
||||
* @author David Turanski
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
|
||||
@Tag("integration")
|
||||
@Testcontainers
|
||||
public class CdcBootStarterIntegrationTest {
|
||||
|
||||
private static final String DATABASE_NAME = "inventory";
|
||||
|
||||
private static String MAPPED_PORT;
|
||||
|
||||
@Container
|
||||
static GenericContainer debeziumMySQL =
|
||||
new GenericContainer<>(DockerImageName.parse("debezium/example-mysql:1.9.6.Final"))
|
||||
.withEnv("MYSQL_ROOT_PASSWORD", "debezium")
|
||||
.withEnv("MYSQL_USER", "mysqluser")
|
||||
.withEnv("MYSQL_PASSWORD", "mysqlpw")
|
||||
// .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("mysql")))
|
||||
.withExposedPorts(3306)
|
||||
.withStartupTimeout(Duration.ofSeconds(120))
|
||||
.withStartupAttempts(3);
|
||||
|
||||
private static JdbcTemplate jdbcTemplate;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
MAPPED_PORT = String.valueOf(debeziumMySQL.getMappedPort(3306));
|
||||
jdbcTemplate = jdbcTemplate(
|
||||
"com.mysql.cj.jdbc.Driver",
|
||||
"jdbc:mysql://localhost:" + MAPPED_PORT + "/" + DATABASE_NAME + "?enabledTLSProtocols=TLSv1.2",
|
||||
"root",
|
||||
"debezium");
|
||||
}
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(TestCdcApplication.class)
|
||||
.withPropertyValues(
|
||||
"spring.datasource.type=com.zaxxer.hikari.HikariDataSource",
|
||||
"cdc.name=my-sql-connector",
|
||||
"cdc.schema=false",
|
||||
"cdc.flattening.enabled=true",
|
||||
"cdc.stream.header.offset=true",
|
||||
"cdc.connector=mysql",
|
||||
"cdc.config.database.user=debezium",
|
||||
"cdc.config.database.password=dbz",
|
||||
"cdc.config.database.hostname=localhost",
|
||||
"cdc.config.database.port=" + MAPPED_PORT,
|
||||
"cdc.config.database.server.id=85744",
|
||||
"cdc.config.database.server.name=my-app-connector",
|
||||
"cdc.config.database.history=io.debezium.relational.history.MemoryDatabaseHistory");
|
||||
|
||||
@Test
|
||||
public void consumerTest() {
|
||||
contextRunner
|
||||
.withPropertyValues(
|
||||
"cdc.flattening.deleteHandlingMode=drop",
|
||||
"cdc.flattening.dropTombstones=true")
|
||||
.run(context -> {
|
||||
TestCdcApplication.TestSourceRecordConsumer testConsumer = context
|
||||
.getBean(TestCdcApplication.TestSourceRecordConsumer.class);
|
||||
jdbcTemplate.update(
|
||||
"insert into `customers`(`first_name`,`last_name`,`email`) " +
|
||||
"VALUES('Test666', 'Test666', 'Test666@spring.org')");
|
||||
JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "customers", "first_name = ?", "Test666");
|
||||
|
||||
await().atMost(Duration.ofSeconds(30))
|
||||
.untilAsserted(() -> assertThat(testConsumer.recordList).hasSizeGreaterThanOrEqualTo(52));
|
||||
});
|
||||
}
|
||||
|
||||
public static JdbcTemplate jdbcTemplate(String jdbcDriver, String jdbcUrl, String user, String password) {
|
||||
HikariDataSource dataSource = new HikariDataSource();
|
||||
dataSource.setDriverClassName(jdbcDriver);
|
||||
dataSource.setJdbcUrl(jdbcUrl);
|
||||
dataSource.setUsername(user);
|
||||
dataSource.setPassword(password);
|
||||
return new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.common.cdc;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.apache.kafka.connect.source.SourceRecord;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.integration.metadata.SimpleMetadataStore;
|
||||
|
||||
/**
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
|
||||
public class TestCdcApplication {
|
||||
|
||||
@Bean
|
||||
public Consumer<SourceRecord> mySourceRecordConsumer(Function<SourceRecord, byte[]> valueSerializer,
|
||||
Function<SourceRecord, byte[]> keySerializer) {
|
||||
return new TestSourceRecordConsumer(valueSerializer, keySerializer);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleMetadataStore simpleMetadataStore() {
|
||||
return new SimpleMetadataStore();
|
||||
}
|
||||
|
||||
public static class TestSourceRecordConsumer implements Consumer<SourceRecord> {
|
||||
|
||||
private final Function<SourceRecord, byte[]> valueSerializer;
|
||||
|
||||
private final Function<SourceRecord, byte[]> keySerializer;
|
||||
|
||||
public Map<Object, Object> keyValue = new HashMap<>();
|
||||
|
||||
public List<SourceRecord> recordList = new CopyOnWriteArrayList<>();
|
||||
|
||||
public TestSourceRecordConsumer(Function<SourceRecord, byte[]> valueSerializer,
|
||||
Function<SourceRecord, byte[]> keySerializer) {
|
||||
this.valueSerializer = valueSerializer;
|
||||
this.keySerializer = keySerializer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(SourceRecord sourceRecord) {
|
||||
if (sourceRecord != null) { // ignore null records
|
||||
recordList.add(sourceRecord);
|
||||
Object payload = valueSerializer.apply(sourceRecord);
|
||||
Object key = keySerializer.apply(sourceRecord);
|
||||
keyValue.put(key, payload);
|
||||
|
||||
System.out.println("[CDC Event]: " + sourceRecord.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud.fn</groupId>
|
||||
<artifactId>spring-functions-parent</artifactId>
|
||||
<version>4.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../spring-functions-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>cdc-debezium-common</artifactId>
|
||||
<name>cdc-debezium-common</name>
|
||||
<description>Change Data Capture (CDC) Debezium Common</description>
|
||||
|
||||
<properties>
|
||||
<version.debezium>1.9.6.Final</version.debezium>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-api</artifactId>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-embedded</artifactId>
|
||||
<version>${version.debezium}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-mysql</artifactId>
|
||||
<optional>true</optional>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-postgres</artifactId>
|
||||
<optional>true</optional>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-mongodb</artifactId>
|
||||
<optional>true</optional>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-oracle</artifactId>
|
||||
<optional>true</optional>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-sqlserver</artifactId>
|
||||
<optional>true</optional>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud.fn</groupId>
|
||||
<artifactId>metadata-store-common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-ip</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.common.cdc;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import io.debezium.transforms.ExtractNewRecordState;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.kafka.connect.json.JsonConverter;
|
||||
import org.apache.kafka.connect.source.SourceRecord;
|
||||
import org.apache.kafka.connect.storage.Converter;
|
||||
import org.apache.kafka.connect.storage.OffsetBackingStore;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(CdcCommonProperties.class)
|
||||
@Import(CdcOffsetBackingStoreConfiguration.class)
|
||||
public class CdcCommonConfiguration {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(CdcCommonConfiguration.class);
|
||||
|
||||
@Bean
|
||||
public io.debezium.config.Configuration configuration(CdcCommonProperties properties) {
|
||||
Map<String, String> configMap = properties.getConfig();
|
||||
return io.debezium.config.Configuration.from(configMap);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<SourceRecord, SourceRecord> recordFlattening(CdcCommonProperties properties,
|
||||
ExtractNewRecordState extractNewRecordState) {
|
||||
return sourceRecord -> properties.getFlattening().isEnabled() ?
|
||||
(SourceRecord) extractNewRecordState.apply(sourceRecord) : sourceRecord;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ExtractNewRecordState extractNewRecordState(CdcCommonProperties properties) {
|
||||
ExtractNewRecordState extractNewRecordState = new ExtractNewRecordState();
|
||||
Map<String, Object> config = extractNewRecordState.config().defaultValues();
|
||||
config.put("drop.tombstones", properties.getFlattening().isDropTombstones());
|
||||
config.put("delete.handling.mode", properties.getFlattening().getDeleteHandlingMode().name());
|
||||
if (!StringUtils.isEmpty(properties.getFlattening().getAddHeaders())) {
|
||||
config.put("add.headers", properties.getFlattening().getAddHeaders());
|
||||
}
|
||||
if (!StringUtils.isEmpty(properties.getFlattening().getAddFields())) {
|
||||
config.put("add.fields", properties.getFlattening().getAddFields());
|
||||
}
|
||||
|
||||
extractNewRecordState.configure(config);
|
||||
return extractNewRecordState;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public JsonConverter jsonConverter(CdcCommonProperties properties) {
|
||||
JsonConverter jsonConverter = new JsonConverter();
|
||||
jsonConverter.configure(Collections.singletonMap("schemas.enable", properties.isSchema()), false);
|
||||
return jsonConverter;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<SourceRecord, byte[]> valueSerializer(Converter valueConverter) {
|
||||
return sourceRecord -> valueConverter.fromConnectData(
|
||||
sourceRecord.topic(), sourceRecord.valueSchema(), sourceRecord.value());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<SourceRecord, byte[]> keySerializer(Converter valueConverter) {
|
||||
return sourceRecord -> valueConverter.fromConnectData(
|
||||
sourceRecord.topic(), sourceRecord.keySchema(), sourceRecord.key());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EmbeddedEngine.Builder embeddedEngineBuilder(CdcCommonProperties properties,
|
||||
OffsetBackingStore offsetBackingStore) {
|
||||
|
||||
if (!properties.getConfig().containsKey("connector.class")) {
|
||||
properties.getConfig().put("connector.class", properties.getConnector().connectorClass);
|
||||
}
|
||||
|
||||
if (!properties.getConfig().containsKey("name")) {
|
||||
properties.getConfig().put("name", properties.getName());
|
||||
}
|
||||
|
||||
if (!properties.getConfig().containsKey("offset.flush.interval.ms")) {
|
||||
properties.getConfig().put("offset.flush.interval.ms", properties.getOffset().getFlushInterval().toMillis() + "");
|
||||
}
|
||||
|
||||
if (!properties.getConfig().containsKey("offset.flush.timeout.ms")) {
|
||||
properties.getConfig().put("offset.flush.timeout.ms", properties.getOffset().getCommitTimeout().toMillis() + "");
|
||||
}
|
||||
|
||||
if (!properties.getConfig().containsKey("offset.commit.policy")) {
|
||||
properties.getConfig().put("offset.commit.policy", properties.getOffset().getPolicy().policyClass);
|
||||
}
|
||||
|
||||
if (!properties.getConfig().containsKey("offset.storage")) {
|
||||
properties.getConfig().put("offset.storage", properties.getOffset().getStorage().offsetStorageClass);
|
||||
}
|
||||
|
||||
return EmbeddedEngine.create()
|
||||
.using(io.debezium.config.Configuration.from(properties.getConfig()))
|
||||
.offsetBackingStore(offsetBackingStore);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,312 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.common.cdc;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import jakarta.validation.constraints.AssertTrue;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
@ConfigurationProperties("cdc")
|
||||
@Validated
|
||||
public class CdcCommonProperties {
|
||||
|
||||
/**
|
||||
* Unique name for this sourceConnector instance.
|
||||
*/
|
||||
@NotEmpty
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Shortcut for the cdc.config.connector.class property. Either of those can be used as long as they do not
|
||||
* contradict with each other.
|
||||
*/
|
||||
@NotNull
|
||||
private ConnectorType connector = null;
|
||||
|
||||
private final Offset offset = new Offset();
|
||||
|
||||
/**
|
||||
* Include the schema's as part of the outbound message.
|
||||
*/
|
||||
private boolean schema = false;
|
||||
|
||||
/**
|
||||
* Event Flattening (https://debezium.io/docs/configuration/event-flattening).
|
||||
*/
|
||||
private final Flattening flattening = new Flattening();
|
||||
|
||||
/**
|
||||
* Spring pass-trough wrapper for debezium configuration properties.
|
||||
* All properties with a 'cdc.config.' prefix are native Debezium properties.
|
||||
* The prefix is removed, converting them into Debezium io.debezium.config.Configuration.
|
||||
*/
|
||||
private Map<String, String> config = defaultConfig();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Offset getOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
public Flattening getFlattening() {
|
||||
return flattening;
|
||||
}
|
||||
|
||||
public Map<String, String> getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public boolean isSchema() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
public void setSchema(boolean schema) {
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
private Map<String, String> defaultConfig() {
|
||||
Map<String, String> defaultConfig = new HashMap<>();
|
||||
defaultConfig.put("database.history", "io.debezium.relational.history.MemoryDatabaseHistory");
|
||||
//defaultConfig.put("offset.flush.interval.ms", "60000");
|
||||
return defaultConfig;
|
||||
}
|
||||
|
||||
public ConnectorType getConnector() {
|
||||
return connector;
|
||||
}
|
||||
|
||||
public void setConnector(ConnectorType connector) {
|
||||
this.connector = connector;
|
||||
}
|
||||
|
||||
@AssertTrue
|
||||
public boolean connectorIsSet() {
|
||||
return this.getConnector() != null || this.getConfig().containsKey("connector.class");
|
||||
}
|
||||
|
||||
public static class Offset {
|
||||
|
||||
/**
|
||||
* Interval at which to try committing offsets. The default is 1 minute.
|
||||
*/
|
||||
private Duration flushInterval = Duration.ofMillis(60000);
|
||||
|
||||
/**
|
||||
* Maximum number of milliseconds to wait for records to flush and partition offset data to be committed to
|
||||
* offset storage before cancelling the process and restoring the offset data to be committed in a future attempt.
|
||||
*/
|
||||
private Duration commitTimeout = Duration.ofMillis(5000);
|
||||
|
||||
/**
|
||||
* Offset storage commit policy.
|
||||
*/
|
||||
private OffsetPolicy policy = OffsetPolicy.periodic;
|
||||
|
||||
/**
|
||||
* Kafka connector tracks the number processed records and regularly stores the count (as "offsets") in a
|
||||
* preconfigured metadata storage. On restart the connector resumes the reading from the last recorded source offset.
|
||||
*/
|
||||
private OffsetStorageType storage = OffsetStorageType.metadata;
|
||||
|
||||
public enum OffsetPolicy {
|
||||
/** periodic. */
|
||||
periodic("io.debezium.embedded.spi.OffsetCommitPolicy$PeriodicCommitOffsetPolicy"),
|
||||
/** always. */
|
||||
always("OffsetCommitPolicy.AlwaysCommitOffsetPolicy.class.getName()");
|
||||
|
||||
/** policy. */
|
||||
public final String policyClass;
|
||||
|
||||
OffsetPolicy(String policyClassName) {
|
||||
this.policyClass = policyClassName;
|
||||
}
|
||||
}
|
||||
|
||||
public Duration getFlushInterval() {
|
||||
return flushInterval;
|
||||
}
|
||||
|
||||
public void setFlushInterval(Duration flushInterval) {
|
||||
this.flushInterval = flushInterval;
|
||||
}
|
||||
|
||||
public Duration getCommitTimeout() {
|
||||
return commitTimeout;
|
||||
}
|
||||
|
||||
public void setCommitTimeout(Duration commitTimeout) {
|
||||
this.commitTimeout = commitTimeout;
|
||||
}
|
||||
|
||||
public OffsetPolicy getPolicy() {
|
||||
return policy;
|
||||
}
|
||||
|
||||
public void setPolicy(OffsetPolicy policy) {
|
||||
this.policy = policy;
|
||||
}
|
||||
|
||||
public OffsetStorageType getStorage() {
|
||||
return storage;
|
||||
}
|
||||
|
||||
public void setStorage(OffsetStorageType storage) {
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum OffsetStorageType {
|
||||
/** memory offset storage type. */
|
||||
memory("org.apache.kafka.connect.storage.MemoryOffsetBackingStore"),
|
||||
/** File offset storage type. */
|
||||
file("org.apache.kafka.connect.storage.FileOffsetBackingStore"),
|
||||
/** Kafka offset storage type. */
|
||||
kafka("org.apache.kafka.connect.storage.KafkaOffsetBackingStore"),
|
||||
/** Metadata offset storage type. */
|
||||
metadata("org.springframework.cloud.stream.app.cdc.common.core.store.MetadataStoreOffsetBackingStore");
|
||||
|
||||
/** Class name of the Offset Storage. */
|
||||
public final String offsetStorageClass;
|
||||
|
||||
OffsetStorageType(String type) {
|
||||
this.offsetStorageClass = type;
|
||||
}
|
||||
}
|
||||
|
||||
public enum ConnectorType {
|
||||
/** MySql connector type. */
|
||||
mysql("io.debezium.connector.mysql.MySqlConnector"),
|
||||
/** Postgres connector type. */
|
||||
postgres("io.debezium.connector.postgresql.PostgresConnector"),
|
||||
/** Mongodb connector type. */
|
||||
mongodb("io.debezium.connector.mongodb.MongoDbConnector"),
|
||||
/** Oracle connector type. */
|
||||
oracle("io.debezium.connector.oracle.OracleConnector"),
|
||||
/** SqlServer connector type. */
|
||||
sqlserver("io.debezium.connector.sqlserver.SqlServerConnector");
|
||||
|
||||
/** Connector class name. */
|
||||
public final String connectorClass;
|
||||
|
||||
ConnectorType(String type) {
|
||||
this.connectorClass = type;
|
||||
}
|
||||
}
|
||||
|
||||
public enum DeleteHandlingMode {
|
||||
/** records are removed. */
|
||||
drop,
|
||||
/** add a __deleted column with true/false values based on record operation. */
|
||||
rewrite,
|
||||
/** pass delete events. */
|
||||
none
|
||||
}
|
||||
|
||||
/**
|
||||
* https://debezium.io/documentation/reference/0.10/configuration/event-flattening.html .
|
||||
* https://debezium.io/documentation/reference/0.10/configuration/event-flattening.html#configuration_options
|
||||
*/
|
||||
public static class Flattening {
|
||||
|
||||
/**
|
||||
* Enable flattening the source record events (https://debezium.io/docs/configuration/event-flattening).
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* By default Debezium generates tombstone records to enable Kafka compaction on deleted records.
|
||||
* The dropTombstones can suppress the tombstone records.
|
||||
*/
|
||||
private boolean dropTombstones = true;
|
||||
|
||||
/**
|
||||
* Options for handling deleted records: (1) none - pass the records through, (2) drop - remove the records and
|
||||
* (3) rewrite - add a '__deleted' field to the records.
|
||||
*/
|
||||
private DeleteHandlingMode deleteHandlingMode = DeleteHandlingMode.drop;
|
||||
|
||||
/**
|
||||
* Comma separated list of metadata fields to add to the flattened message.
|
||||
* The fields will be prefixed with "__" or "__[<]struct]__", depending on the specification of the struct.
|
||||
*/
|
||||
private String addFields = null;
|
||||
|
||||
/**
|
||||
* Comma separated list specify a list of metadata fields to add to the header of the flattened message.
|
||||
* The fields will be prefixed with "__" or "__[struct]__".
|
||||
*/
|
||||
private String addHeaders = null;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public boolean isDropTombstones() {
|
||||
return dropTombstones;
|
||||
}
|
||||
|
||||
public void setDropTombstones(boolean dropTombstones) {
|
||||
this.dropTombstones = dropTombstones;
|
||||
}
|
||||
|
||||
public DeleteHandlingMode getDeleteHandlingMode() {
|
||||
return deleteHandlingMode;
|
||||
}
|
||||
|
||||
public void setDeleteHandlingMode(DeleteHandlingMode deleteHandlingMode) {
|
||||
this.deleteHandlingMode = deleteHandlingMode;
|
||||
}
|
||||
|
||||
public String getAddFields() {
|
||||
return addFields;
|
||||
}
|
||||
|
||||
public void setAddFields(String addFields) {
|
||||
this.addFields = addFields;
|
||||
}
|
||||
|
||||
public String getAddHeaders() {
|
||||
return addHeaders;
|
||||
}
|
||||
|
||||
public void setAddHeaders(String addHeaders) {
|
||||
this.addHeaders = addHeaders;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.common.cdc;
|
||||
|
||||
import org.apache.kafka.connect.storage.FileOffsetBackingStore;
|
||||
import org.apache.kafka.connect.storage.KafkaOffsetBackingStore;
|
||||
import org.apache.kafka.connect.storage.MemoryOffsetBackingStore;
|
||||
import org.apache.kafka.connect.storage.OffsetBackingStore;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.cloud.fn.common.cdc.store.MetadataStoreOffsetBackingStore;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.integration.metadata.MetadataStore;
|
||||
|
||||
/**
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
@Configuration
|
||||
public class CdcOffsetBackingStoreConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnExpression("'${cdc.config.offset.storage}'.equalsIgnoreCase('org.springframework.cloud.stream.app.cdc.common.core.store.MetadataStoreOffsetBackingStore') " +
|
||||
"or '${cdc.offset.storage:metadata}'.equals('metadata')")
|
||||
public OffsetBackingStore metadataStoreOffsetBackingStore(MetadataStore metadataStore) {
|
||||
return new MetadataStoreOffsetBackingStore(metadataStore);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnExpression("'${cdc.config.offset.storage}'.equalsIgnoreCase('org.apache.kafka.connect.storage.FileOffsetBackingStore') " +
|
||||
"or '${cdc.offset.storage:metadata}'.equalsIgnoreCase('file')")
|
||||
public OffsetBackingStore fileOffsetBackingStore() {
|
||||
return new FileOffsetBackingStore();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnExpression("'${cdc.config.offset.storage}'.equalsIgnoreCase('org.apache.kafka.connect.storage.KafkaOffsetBackingStore') " +
|
||||
"or '${cdc.offset.storage:metadata}'.equalsIgnoreCase('kafka')")
|
||||
public OffsetBackingStore kafkaOffsetBackingStore() {
|
||||
return new KafkaOffsetBackingStore();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnExpression("'${cdc.config.offset.storage}'.equalsIgnoreCase('org.apache.kafka.connect.storage.MemoryOffsetBackingStore') " +
|
||||
"or '${cdc.offset.storage:metadata}'.equalsIgnoreCase('memory')")
|
||||
public OffsetBackingStore memoryOffsetBackingStore() {
|
||||
return new MemoryOffsetBackingStore();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.common.cdc;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
public class EmbeddedEngineExecutorService implements Closeable {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(EmbeddedEngineExecutorService.class);
|
||||
|
||||
private final EmbeddedEngine engine;
|
||||
private final ExecutorService executor;
|
||||
|
||||
public EmbeddedEngineExecutorService(EmbeddedEngine engine) {
|
||||
this.engine = engine;
|
||||
this.executor = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
logger.info("Start Embedded Engine");
|
||||
this.executor.execute(this.engine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
logger.info("Stop Embedded Engine");
|
||||
this.engine.stop();
|
||||
this.executor.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.common.cdc.store;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.kafka.connect.runtime.WorkerConfig;
|
||||
import org.apache.kafka.connect.runtime.standalone.StandaloneConfig;
|
||||
import org.apache.kafka.connect.storage.MemoryOffsetBackingStore;
|
||||
|
||||
import org.springframework.integration.metadata.MetadataStore;
|
||||
|
||||
/**
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
public class MetadataStoreOffsetBackingStore extends MemoryOffsetBackingStore {
|
||||
|
||||
private MetadataStore metadataStore;
|
||||
private String offsetStoreName;
|
||||
|
||||
public MetadataStoreOffsetBackingStore(MetadataStore metadataStore) {
|
||||
this.metadataStore = metadataStore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(WorkerConfig config) {
|
||||
super.configure(config);
|
||||
this.offsetStoreName = config.getString(StandaloneConfig.OFFSET_STORAGE_FILE_FILENAME_CONFIG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void start() {
|
||||
super.start();
|
||||
//log.info("Starting FileOffsetBackingStore with file {}", file);
|
||||
load();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void stop() {
|
||||
super.stop();
|
||||
// Nothing to do since this doesn't maintain any outstanding connections/data
|
||||
// log.info("Stopped FileOffsetBackingStore");
|
||||
}
|
||||
|
||||
private void load() {
|
||||
if (this.metadataStore.get("keys") != null) {
|
||||
String[] keys = this.metadataStore.get("keys").split(",");
|
||||
for (String keySting : keys) {
|
||||
String valueString = this.metadataStore.get(keySting);
|
||||
ByteBuffer key = ByteBuffer.wrap(keySting.getBytes());
|
||||
ByteBuffer value = ByteBuffer.wrap(valueString.getBytes());
|
||||
this.data.put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void save() {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (Map.Entry<ByteBuffer, ByteBuffer> mapEntry : this.data.entrySet()) {
|
||||
byte[] key = (mapEntry.getKey() != null) ? mapEntry.getKey().array() : null;
|
||||
byte[] value = (mapEntry.getValue() != null) ? mapEntry.getValue().array() : null;
|
||||
if (key != null && value != null) {
|
||||
keys.add(new String(key, StandardCharsets.UTF_8));
|
||||
this.metadataStore.put(
|
||||
new String(key, StandardCharsets.UTF_8),
|
||||
new String(value, StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
this.metadataStore.put("keys", String.join(",", keys));
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
|
||||
spring.cloud.stream.kafka.default.producer.messageKeyExpression=headers['cdc_key'].bytes
|
||||
@@ -23,8 +23,6 @@
|
||||
<module>tcp-common</module>
|
||||
<module>twitter-common</module>
|
||||
<module>tensorflow-common</module>
|
||||
<module>cdc-debezium-common</module>
|
||||
<module>cdc-debezium-boot-starter</module>
|
||||
<module>xmpp-common</module>
|
||||
</modules>
|
||||
<build>
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud.fn</groupId>
|
||||
<artifactId>cdc-debezium-supplier</artifactId>
|
||||
<artifactId>debezium-supplier</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
# CDC Debezium Supplier
|
||||
|
||||
This module provides a CDC Debezium supplier that can be reused and composed in other applications.
|
||||
The `Supplier` uses the `Debezium` library.
|
||||
`cdcSupplier` is implemented as a `java.util.function.Supplier`.
|
||||
This supplier gives you a reactive stream from CDC sources. The supplier has a signature of `Supplier<Flux<Message<?>>>`.
|
||||
Users have to subscribe to this `Flux` and then receive the data.
|
||||
|
||||
https://en.wikipedia.org/wiki/Change_data_capture[Change Data Capture] (CDC) `source` that captures and streams change events from various databases.
|
||||
Currently, it supports `MySQL`, `PostgreSQL`, `MongoDB`, `Oracle` and `SQL Server` databases.
|
||||
|
||||
Build upon https://debezium.io/docs/embedded/[Debezium Embedded Connector], the `CDC Source` allows capturing and streaming database changes over different message binders such Apache Kafka, RabbitMQ and all Spring Cloud Stream supporter brokers.
|
||||
|
||||
## Beans for injection
|
||||
|
||||
You can import the `CdcSupplierConfiguration` in the application and then inject the following bean.
|
||||
|
||||
`cdcSupplier`
|
||||
|
||||
You need to inject this as `Supplier<Flux<Message<?>>>`.
|
||||
|
||||
You can use `cdcSupplier` as a qualifier when injecting.
|
||||
|
||||
Once injected, you can use the `get` method of the `Supplier` to invoke it and then subscribe to the returned `Flux`.
|
||||
|
||||
## Configuration Options
|
||||
|
||||
All configuration properties are prefixed with `cdc`.
|
||||
Use the `cdc.config.` prefix to inject native, Debezium properties. Convenient shortcuts for the most frequently used
|
||||
Debezium properties are available.
|
||||
Uses `BackingOffsetStore` configuration, based on the https://github.com/spring-cloud/stream-applications/tree/master/functions/common/metadata-store-common[MetadataStore] service.
|
||||
Later provides various microservices friendly ways for storing the offset metadata.
|
||||
|
||||
For more information on the various options available, please see:
|
||||
|
||||
* link:../../common/cdc-debezium-common/src/main/java/org/springframework/cloud/fn/common/cdc/CdcCommonProperties.java[CdcCommonProperties].
|
||||
* link:src/main/java/org/springframework/cloud/fn/supplier/cdc/CdcSupplierProperties.java[CdcSupplierProperties].
|
||||
|
||||
## Examples
|
||||
See this link:../../../applications/source/cdc-debezium-source/src/test/java/org/springframework/cloud/stream/app/source/cdc[test suite] for the various ways, this supplier is used.
|
||||
|
||||
## Other usage
|
||||
|
||||
See this link:../../../applications/source/cdc-debezium-source/README.adoc[README] where this supplier is used to create a Spring Cloud Stream CDC Source.
|
||||
|
||||
Also check link:../../common/cdc-debezium-boot-starter/README.adoc[README] to see how the core CDC can be used a Spring Boot Starter.
|
||||
@@ -1,82 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud.fn</groupId>
|
||||
<artifactId>spring-functions-parent</artifactId>
|
||||
<version>4.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../spring-functions-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>cdc-debezium-supplier</artifactId>
|
||||
<name>cdc-debezium-supplier</name>
|
||||
<description>CDC Debezium Suppliers</description>
|
||||
|
||||
<properties>
|
||||
<version.debezium>1.9.6.Final</version.debezium>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud.fn</groupId>
|
||||
<artifactId>cdc-debezium-common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-mysql</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-mongodb</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-postgres</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-oracle</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-sqlserver</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${version.debezium}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,173 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.supplier.cdc;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Iterator;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.kafka.connect.header.Header;
|
||||
import org.apache.kafka.connect.source.SourceRecord;
|
||||
import reactor.core.publisher.EmitterProcessor;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.FluxSink;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.fn.common.cdc.CdcCommonConfiguration;
|
||||
import org.springframework.cloud.fn.common.cdc.EmbeddedEngine;
|
||||
import org.springframework.cloud.fn.common.cdc.EmbeddedEngineExecutorService;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
/**
|
||||
* CDC source that uses the Debezium Connectors to monitor and record all of the row-level
|
||||
* changes in the databases. https://debezium.io/docs/connectors
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
@EnableConfigurationProperties(CdcSupplierProperties.class)
|
||||
@Import(CdcCommonConfiguration.class)
|
||||
public class CdcSupplierConfiguration implements BeanClassLoaderAware {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(CdcSupplierConfiguration.class);
|
||||
|
||||
/**
|
||||
* ORG_SPRINGFRAMEWORK_KAFKA_SUPPORT_KAFKA_NULL.
|
||||
*/
|
||||
public static final String ORG_SPRINGFRAMEWORK_KAFKA_SUPPORT_KAFKA_NULL = "org.springframework.kafka.support.KafkaNull";
|
||||
|
||||
private Object kafkaNull = null;
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
try {
|
||||
Class<?> clazz = ClassUtils.forName(ORG_SPRINGFRAMEWORK_KAFKA_SUPPORT_KAFKA_NULL, classLoader);
|
||||
Field field = clazz.getDeclaredField("INSTANCE");
|
||||
this.kafkaNull = field.get(null);
|
||||
}
|
||||
catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Supplier<Flux<Message<?>>> cdcSupplier(EmbeddedEngineExecutorService embeddedEngineExecutorService) {
|
||||
|
||||
return () -> emitterProcessor
|
||||
.doOnSubscribe(subscription -> {
|
||||
embeddedEngineExecutorService.start();
|
||||
})
|
||||
.doAfterTerminate(() -> {
|
||||
logger.info("Proactive shutdown");
|
||||
embeddedEngineExecutorService.close();
|
||||
})
|
||||
.doOnError(throwable -> logger.error(throwable.getMessage(), throwable));
|
||||
}
|
||||
|
||||
private EmitterProcessor<Message<?>> emitterProcessor = EmitterProcessor.create(256, false);
|
||||
|
||||
@Bean
|
||||
public EmbeddedEngineExecutorService embeddedEngineExecutorService(
|
||||
EmbeddedEngine.Builder embeddedEngineBuilder,
|
||||
Function<SourceRecord, byte[]> valueSerializer, Function<SourceRecord, byte[]> keySerializer,
|
||||
Function<SourceRecord, SourceRecord> recordFlattening,
|
||||
ObjectMapper mapper, CdcSupplierProperties cdcStreamingEngineProperties) {
|
||||
|
||||
FluxSink<Message<?>> sink = emitterProcessor.sink(FluxSink.OverflowStrategy.BUFFER);
|
||||
Consumer<SourceRecord> messageConsumer = sourceRecord -> {
|
||||
|
||||
// When cdc.flattening.deleteHandlingMode=none and cdc.flattening.dropTombstones=false
|
||||
// then on deletion event an additional sourceRecord is sent with value Null.
|
||||
// Here we filter out such condition.
|
||||
if (sourceRecord == null) {
|
||||
logger.debug("Ignore disabled tombstone events");
|
||||
return;
|
||||
}
|
||||
|
||||
Object cdcJsonPayload = valueSerializer.apply(sourceRecord);
|
||||
|
||||
// When the tombstone event is enabled, Debezium serializes the payload to null (e.g. empty payload)
|
||||
// while the metadata information is carried through the headers (cdc_key).
|
||||
// Note: Event for none flattened responses, when the cdc.config.tombstones.on.delete=true
|
||||
// (default), tombstones are generate by Debezium and handled by the code below.
|
||||
if (cdcJsonPayload == null) {
|
||||
cdcJsonPayload = this.kafkaNull;
|
||||
}
|
||||
|
||||
// If payload is still null ignore the message.
|
||||
if (cdcJsonPayload == null) {
|
||||
logger.info("dropped null payload message");
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] key = keySerializer.apply(sourceRecord);
|
||||
if (key == null) {
|
||||
logger.warn("Null serialised key for sourceRecord: " + sourceRecord);
|
||||
key = new byte[0];
|
||||
}
|
||||
|
||||
MessageBuilder<?> messageBuilder = MessageBuilder
|
||||
.withPayload(cdcJsonPayload)
|
||||
// .setHeader("cdc_key", new String(key))
|
||||
.setHeader("cdc_key", key)
|
||||
.setHeader("cdc_topic", sourceRecord.topic())
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
(cdcJsonPayload.equals(this.kafkaNull)) ? MimeTypeUtils.TEXT_PLAIN_VALUE
|
||||
: MimeTypeUtils.APPLICATION_JSON_VALUE);
|
||||
|
||||
if (cdcStreamingEngineProperties.getHeader().isConvertConnectHeaders()) {
|
||||
// Convert the Connect Headers into Message Headers.
|
||||
if (sourceRecord.headers() != null && !sourceRecord.headers().isEmpty()) {
|
||||
Iterator<Header> itr = sourceRecord.headers().iterator();
|
||||
while (itr.hasNext()) {
|
||||
Header header = itr.next();
|
||||
messageBuilder.setHeader(header.key(), header.value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cdcStreamingEngineProperties.getHeader().isOffset()) {
|
||||
try {
|
||||
messageBuilder.setHeader("cdc_offset",
|
||||
mapper.writeValueAsString(sourceRecord.sourceOffset()));
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
logger.warn("Failed to record cdc_offset header", e);
|
||||
}
|
||||
}
|
||||
|
||||
sink.next(messageBuilder.build());
|
||||
};
|
||||
|
||||
EmbeddedEngine engine = embeddedEngineBuilder
|
||||
.notifying(record -> messageConsumer.accept(recordFlattening.apply(record)))
|
||||
.build();
|
||||
|
||||
return new EmbeddedEngineExecutorService(engine);
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.supplier.cdc;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
@ConfigurationProperties("cdc.stream")
|
||||
@Validated
|
||||
public class CdcSupplierProperties {
|
||||
|
||||
/**
|
||||
* Control what information to be serialized in the outbound message headers.
|
||||
*/
|
||||
private Header header = new Header();
|
||||
|
||||
public Header getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public static class Header {
|
||||
|
||||
/**
|
||||
* Serializes the source record's offset metadata into the outbound message header under cdc.offset.
|
||||
*/
|
||||
private boolean offset = false;
|
||||
|
||||
/**
|
||||
* When true the {@link org.apache.kafka.connect.header.Header} are converted into message headers with the
|
||||
* {@link org.apache.kafka.connect.header.Header#key()} as name and {@link org.apache.kafka.connect.header.Header#value()}.
|
||||
*/
|
||||
private boolean convertConnectHeaders = true;
|
||||
|
||||
public boolean isOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
public void setOffset(boolean offset) {
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
public boolean isConvertConnectHeaders() {
|
||||
return convertConnectHeaders;
|
||||
}
|
||||
|
||||
public void setConvertConnectHeaders(boolean convertConnectHeaders) {
|
||||
this.convertConnectHeaders = convertConnectHeaders;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
|
||||
spring.cloud.stream.kafka.default.producer.messageKeyExpression=headers['cdc_key'].bytes
|
||||
181
supplier/debezium-supplier/pom.xml
Normal file
181
supplier/debezium-supplier/pom.xml
Normal file
@@ -0,0 +1,181 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud.fn</groupId>
|
||||
<artifactId>spring-functions-parent</artifactId>
|
||||
<version>4.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../spring-functions-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>debezium-supplier</artifactId>
|
||||
<name>debezium-supplier</name>
|
||||
<description>Debezium Supplier</description>
|
||||
|
||||
<properties>
|
||||
<version.debezium>2.2.0.Final</version.debezium>
|
||||
<apicurio.version>2.4.2.Final</apicurio.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-embedded</artifactId>
|
||||
<version>${version.debezium}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-reload4j</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- Debezium Connectors -->
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-mysql</artifactId>
|
||||
<version>${version.debezium}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-mongodb</artifactId>
|
||||
<version>${version.debezium}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-postgres</artifactId>
|
||||
<version>${version.debezium}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-oracle</artifactId>
|
||||
<version>${version.debezium}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-sqlserver</artifactId>
|
||||
<version>${version.debezium}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-db2</artifactId>
|
||||
<version>${version.debezium}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-vitess</artifactId>
|
||||
<version>${version.debezium}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!-- <dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-cassandra</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<groupId>org.hibernate</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<optional>true</optional>
|
||||
</dependency> -->
|
||||
<dependency>
|
||||
<groupId>io.debezium</groupId>
|
||||
<artifactId>debezium-connector-spanner</artifactId>
|
||||
<version>${version.debezium}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- APICURIO Schema Registry (for AVRO format)-->
|
||||
<dependency>
|
||||
<groupId>io.apicurio</groupId>
|
||||
<artifactId>apicurio-registry-utils-converter</artifactId>
|
||||
<version>${apicurio.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.apicurio</groupId>
|
||||
<artifactId>apicurio-registry-client</artifactId>
|
||||
<version>${apicurio.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- TEST -->
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<version>4.0.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<configuration>
|
||||
<groups>integration</groups>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.supplier.debezium;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import io.debezium.engine.ChangeEvent;
|
||||
import io.debezium.engine.DebeziumEngine;
|
||||
import io.debezium.engine.DebeziumEngine.CompletionCallback;
|
||||
import io.debezium.engine.DebeziumEngine.ConnectorCallback;
|
||||
import io.debezium.engine.spi.OffsetCommitPolicy;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* DebeziumEngine auto-configuration.
|
||||
*
|
||||
* The engine configuration is entirely standalone and only talks with the source system; Applications using the engine
|
||||
* auto-configuration simply provides a {@link Consumer consumer function} implementation to which the engine will pass
|
||||
* all records containing database change events.
|
||||
* <p>
|
||||
* With the engine, the application that runs the connector assumes all responsibility for fault tolerance, scalability,
|
||||
* and durability. Additionally, applications must specify how the engine can store its relational database schema
|
||||
* history and offsets. By default, this information will be stored in memory and will thus be lost upon application
|
||||
* restart.
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(DebeziumProperties.class)
|
||||
public class DebeziumEngineAutoConfiguration {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DebeziumEngineAutoConfiguration.class);
|
||||
|
||||
/**
|
||||
* The fully-qualified class name of the commit policy type. The default is a periodic commit policy based upon time
|
||||
* intervals.
|
||||
* @param properties The 'debezium.properties.offset.flush.interval.ms' configuration is compulsory for the Periodic
|
||||
* policy type. The ALWAYS and DEFAULT doesn't require properties.
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public OffsetCommitPolicy offsetCommitPolicy(DebeziumProperties properties) {
|
||||
|
||||
switch (properties.getOffsetCommitPolicy()) {
|
||||
case PERIODIC:
|
||||
return OffsetCommitPolicy.periodic(properties.getDebeziumNativeConfiguration());
|
||||
case ALWAYS:
|
||||
return OffsetCommitPolicy.always();
|
||||
case DEFAULT:
|
||||
default:
|
||||
return NULL_OFFSET_COMMIT_POLICY;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the specified clock when needing to determine the current time. Defaults to {@link Clock#systemDefaultZone()
|
||||
* system clock}, but you can override the Bean in your configuration with you {@link Clock implementation}. Returns
|
||||
* @return Clock for the system default zone.
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public Clock debeziumClock() {
|
||||
return Clock.systemDefaultZone();
|
||||
}
|
||||
|
||||
/**
|
||||
* When the engine's {@link DebeziumEngine#run()} method completes, call the supplied function with the results.
|
||||
* @return Default completion callback that logs the completion status. The bean can be overridden in custom
|
||||
* implementation.
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CompletionCallback completionCallback() {
|
||||
return DEFAULT_COMPLETION_CALLBACK;
|
||||
}
|
||||
|
||||
/**
|
||||
* During the engine run, provides feedback about the different stages according to the completion state of each
|
||||
* component running within the engine (connectors, tasks etc). The bean can be overridden in custom implementation.
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ConnectorCallback connectorCallback() {
|
||||
return DEFAULT_CONNECTOR_CALLBACK;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DebeziumEngine<?> debeziumEngine(Consumer<ChangeEvent<byte[], byte[]>> changeEventConsumer,
|
||||
OffsetCommitPolicy offsetCommitPolicy, CompletionCallback completionCallback,
|
||||
ConnectorCallback connectorCallback, DebeziumProperties properties, Clock debeziumClock) {
|
||||
|
||||
DebeziumEngine<ChangeEvent<byte[], byte[]>> debeziumEngine = DebeziumEngine
|
||||
.create(properties.getFormat().serializationFormat())
|
||||
.using(properties.getDebeziumNativeConfiguration())
|
||||
.using(debeziumClock)
|
||||
.using(completionCallback)
|
||||
.using(connectorCallback)
|
||||
.using((offsetCommitPolicy != NULL_OFFSET_COMMIT_POLICY) ? offsetCommitPolicy : null)
|
||||
.notifying(changeEventConsumer)
|
||||
.build();
|
||||
|
||||
|
||||
return debeziumEngine;
|
||||
}
|
||||
|
||||
/**
|
||||
* A callback function to be notified when the connector completes.
|
||||
*/
|
||||
private static final CompletionCallback DEFAULT_COMPLETION_CALLBACK = new CompletionCallback() {
|
||||
@Override
|
||||
public void handle(boolean success, String message, Throwable error) {
|
||||
logger.info(String.format("Debezium Engine completed with success:%s, message:%s ", success, message),
|
||||
error);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Callback function which informs users about the various stages a connector goes through during startup.
|
||||
*/
|
||||
private static final ConnectorCallback DEFAULT_CONNECTOR_CALLBACK = new ConnectorCallback() {
|
||||
|
||||
/**
|
||||
* Called after a connector has been successfully started by the engine.
|
||||
*/
|
||||
public void connectorStarted() {
|
||||
logger.info("Connector Started!");
|
||||
};
|
||||
|
||||
/**
|
||||
* Called after a connector has been successfully stopped by the engine.
|
||||
*/
|
||||
public void connectorStopped() {
|
||||
logger.info("Connector Stopped!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after a connector task has been successfully started by the engine.
|
||||
*/
|
||||
public void taskStarted() {
|
||||
logger.info("Connector Task Started!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after a connector task has been successfully stopped by the engine.
|
||||
*/
|
||||
public void taskStopped() {
|
||||
logger.info("Connector Task Stopped!");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* The policy that defines when the offsets should be committed to offset storage.
|
||||
*/
|
||||
private static final OffsetCommitPolicy NULL_OFFSET_COMMIT_POLICY = new OffsetCommitPolicy() {
|
||||
@Override
|
||||
public boolean performCommit(long numberOfMessagesSinceLastCommit, Duration timeSinceLastCommit) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'performCommit'");
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.supplier.debezium;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import io.debezium.engine.format.SerializationFormat;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
@ConfigurationProperties("debezium")
|
||||
public class DebeziumProperties {
|
||||
|
||||
public enum DebeziumFormat {
|
||||
/**
|
||||
* JSON change event format.
|
||||
*/
|
||||
JSON(io.debezium.engine.format.JsonByteArray.class, "application/json"),
|
||||
/**
|
||||
* AVRO change event format.
|
||||
*/
|
||||
AVRO(io.debezium.engine.format.Avro.class, "application/avro"),
|
||||
/**
|
||||
* ProtoBuf change event format.
|
||||
*/
|
||||
PROTOBUF(io.debezium.engine.format.Protobuf.class, "application/x-protobuf"),;
|
||||
|
||||
private final Class<? extends SerializationFormat<byte[]>> serializationFormat;
|
||||
private final String contentType;
|
||||
|
||||
DebeziumFormat(Class<? extends SerializationFormat<byte[]>> serializationFormat, String contentType) {
|
||||
this.serializationFormat = serializationFormat;
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
public Class<? extends SerializationFormat<byte[]>> serializationFormat() {
|
||||
return serializationFormat;
|
||||
}
|
||||
|
||||
public final String contentType() {
|
||||
return contentType;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Spring pass-trough wrapper for debezium configuration properties. All properties with a 'debezium.properties.*'
|
||||
* prefix are native Debezium properties.
|
||||
*/
|
||||
private Map<String, String> properties = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Change Event message content format. Defaults to 'JSON'.
|
||||
*/
|
||||
private DebeziumFormat format = DebeziumFormat.JSON;
|
||||
|
||||
/**
|
||||
* Copy Change Event headers into Message headers.
|
||||
*/
|
||||
private boolean copyHeaders = true;
|
||||
|
||||
/**
|
||||
* The policy that defines when the offsets should be committed to offset storage.
|
||||
*/
|
||||
private DebeziumOffsetCommitPolicy offsetCommitPolicy = DebeziumOffsetCommitPolicy.DEFAULT;
|
||||
|
||||
public Map<String, String> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public DebeziumFormat getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(DebeziumFormat format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public boolean isCopyHeaders() {
|
||||
return copyHeaders;
|
||||
}
|
||||
|
||||
public void setCopyHeaders(boolean copyHeaders) {
|
||||
this.copyHeaders = copyHeaders;
|
||||
}
|
||||
|
||||
public enum DebeziumOffsetCommitPolicy {
|
||||
/**
|
||||
* Commits offsets as frequently as possible. This may result in reduced performance, but it has the least
|
||||
* potential for seeing source records more than once upon restart.
|
||||
*/
|
||||
ALWAYS,
|
||||
/**
|
||||
* Commits offsets no more than the specified time period. If the specified time is less than {@code 0} then the
|
||||
* policy will behave as ALWAYS policy. Requires the 'debezium.properties.offset.flush.interval.ms' native property
|
||||
* to be set.
|
||||
*/
|
||||
PERIODIC,
|
||||
/**
|
||||
* Uses the default Debezium engine policy (PERIODIC).
|
||||
*/
|
||||
DEFAULT;
|
||||
}
|
||||
|
||||
public DebeziumOffsetCommitPolicy getOffsetCommitPolicy() {
|
||||
return offsetCommitPolicy;
|
||||
}
|
||||
|
||||
public void setOffsetCommitPolicy(DebeziumOffsetCommitPolicy offsetCommitPolicy) {
|
||||
this.offsetCommitPolicy = offsetCommitPolicy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the Spring Framework "debezium.properties.*" properties into native Debezium configuration.
|
||||
*/
|
||||
public Properties getDebeziumNativeConfiguration() {
|
||||
Properties outProps = new java.util.Properties();
|
||||
outProps.putAll(this.getProperties());
|
||||
return outProps;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.supplier.debezium;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import io.debezium.engine.ChangeEvent;
|
||||
import io.debezium.engine.DebeziumEngine;
|
||||
import io.debezium.engine.Header;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(DebeziumProperties.class)
|
||||
public class DebeziumReactiveConsumerConfiguration implements BeanClassLoaderAware {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DebeziumReactiveConsumerConfiguration.class);
|
||||
/**
|
||||
* ORG_SPRINGFRAMEWORK_KAFKA_SUPPORT_KAFKA_NULL.
|
||||
*/
|
||||
public static final String ORG_SPRINGFRAMEWORK_KAFKA_SUPPORT_KAFKA_NULL = "org.springframework.kafka.support.KafkaNull";
|
||||
|
||||
private Object kafkaNull = null;
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
try {
|
||||
Class<?> clazz = ClassUtils.forName(ORG_SPRINGFRAMEWORK_KAFKA_SUPPORT_KAFKA_NULL, classLoader);
|
||||
Field field = clazz.getDeclaredField("INSTANCE");
|
||||
this.kafkaNull = field.get(null);
|
||||
}
|
||||
catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reactive Streams, single subscriber, sink used to push down the change event signals received from the Debezium
|
||||
* Engine.
|
||||
*/
|
||||
private final Sinks.Many<Message<?>> eventSink = Sinks.many().unicast().onBackpressureError();
|
||||
|
||||
/**
|
||||
* Debezium Engine is designed to be submitted to an {@link ExecutorService} for execution by a single thread, and a
|
||||
* running connector can be stopped either by calling {@link #stop()} from another thread or by interrupting the
|
||||
* running thread (e.g., as is the case with {@link ExecutorService#shutdownNow()}).
|
||||
*/
|
||||
private final ExecutorService debeziumExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
@Bean
|
||||
public Supplier<Flux<Message<?>>> debeziumSupplier(DebeziumEngine<?> debeziumEngine) {
|
||||
return () -> this.eventSink.asFlux()
|
||||
.doOnRequest(r -> debeziumExecutor.execute(debeziumEngine))
|
||||
.doOnTerminate(debeziumExecutor::shutdownNow);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public Consumer<ChangeEvent<byte[], byte[]>> changeEventConsumer(DebeziumProperties properties) {
|
||||
|
||||
return new ChangeEventConsumer<byte[]>(properties.getFormat().contentType(), properties.isCopyHeaders(),
|
||||
this.eventSink);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format-agnostic change event consumer.
|
||||
*/
|
||||
private final class ChangeEventConsumer<T> implements Consumer<ChangeEvent<T, T>> {
|
||||
|
||||
private final String contentType;
|
||||
private final boolean copyHeaders;
|
||||
private final Sinks.Many<Message<?>> eventSink;
|
||||
|
||||
private ChangeEventConsumer(String contentType, boolean copyHeaders, Sinks.Many<Message<?>> eventSink) {
|
||||
this.contentType = contentType;
|
||||
this.copyHeaders = copyHeaders;
|
||||
this.eventSink = eventSink;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(ChangeEvent<T, T> changeEvent) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("[Debezium Event]: " + changeEvent.key());
|
||||
}
|
||||
|
||||
Object key = changeEvent.key();
|
||||
Object payload = changeEvent.value();
|
||||
String destination = changeEvent.destination();
|
||||
|
||||
// When the tombstone event is enabled, Debezium serializes the payload to null (e.g. empty payload)
|
||||
// while the metadata information is carried through the headers (debezium_key).
|
||||
// Note: Event for none flattened responses, when the debezium.properties.tombstones.on.delete=true
|
||||
// (default), tombstones are generate by Debezium and handled by the code below.
|
||||
if (payload == null) {
|
||||
payload = DebeziumReactiveConsumerConfiguration.this.kafkaNull;
|
||||
}
|
||||
|
||||
// If payload is still null ignore the message.
|
||||
if (payload == null) {
|
||||
logger.info("Dropped null payload message");
|
||||
return;
|
||||
}
|
||||
|
||||
MessageBuilder<?> messageBuilder = MessageBuilder
|
||||
.withPayload(payload)
|
||||
.setHeader("debezium_key", key)
|
||||
.setHeader("debezium_destination", destination)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE,
|
||||
(payload.equals(DebeziumReactiveConsumerConfiguration.this.kafkaNull))
|
||||
? MimeTypeUtils.TEXT_PLAIN_VALUE
|
||||
: this.contentType);
|
||||
|
||||
if (this.copyHeaders) {
|
||||
List<Header<T>> headers = changeEvent.headers();
|
||||
if (headers != null && !headers.isEmpty()) {
|
||||
Iterator<Header<T>> itr = headers.iterator();
|
||||
while (itr.hasNext()) {
|
||||
Header<T> header = itr.next();
|
||||
messageBuilder.setHeader(header.getKey(), header.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.eventSink.tryEmitNext(messageBuilder.build());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.springframework.cloud.fn.supplier.debezium.DebeziumEngineAutoConfiguration
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.supplier.debezium;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
@Configuration
|
||||
public class TestJdbcTemplateConfiguration {
|
||||
@Bean
|
||||
public JdbcTemplate myJdbcTemplate(DataSource dataSource) {
|
||||
return new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConfigurationProperties("app.datasource")
|
||||
public DataSourceProperties dataSourceProperties() {
|
||||
return new DataSourceProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HikariDataSource dataSource(DataSourceProperties dataSourceProperties) {
|
||||
return dataSourceProperties.initializeDataSourceBuilder()
|
||||
.type(HikariDataSource.class)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.supplier.debezium.custom;
|
||||
|
||||
import java.io.File;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import io.debezium.engine.ChangeEvent;
|
||||
import io.debezium.engine.DebeziumEngine;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.cloud.fn.supplier.debezium.TestJdbcTemplateConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.jdbc.JdbcTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
|
||||
/**
|
||||
* This test illustrate how to leverage the DebeziumEngineAutoConfiguration to build a supplier function with a custom
|
||||
* change event Consumer.
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
@Tag("integration")
|
||||
@Testcontainers
|
||||
public class DebeziumEngineAutoConfigurationTest {
|
||||
private static final Log logger = LogFactory.getLog(DebeziumEngineAutoConfigurationTest.class);
|
||||
|
||||
private static final String DATABASE_NAME = "inventory";
|
||||
public static final String IMAGE_TAG = "2.2.0.Final";
|
||||
public static final String DEBEZIUM_EXAMPLE_MYSQL_IMAGE = "debezium/example-mysql:" + IMAGE_TAG;
|
||||
|
||||
@TempDir
|
||||
static File anotherTempDir;
|
||||
|
||||
@Container
|
||||
static GenericContainer<?> debeziumMySQL = new GenericContainer<>(DEBEZIUM_EXAMPLE_MYSQL_IMAGE)
|
||||
.withEnv("MYSQL_ROOT_PASSWORD", "debezium")
|
||||
.withEnv("MYSQL_USER", "mysqluser")
|
||||
.withEnv("MYSQL_PASSWORD", "mysqlpw")
|
||||
.withExposedPorts(3306);
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(DebeziumCustomConsumerApplication.class)
|
||||
.withPropertyValues(
|
||||
"spring.datasource.type=com.zaxxer.hikari.HikariDataSource",
|
||||
|
||||
"debezium.properties.offset.storage=org.apache.kafka.connect.storage.FileOffsetBackingStore",
|
||||
"debezium.properties.offset.storage.file.filename=" + anotherTempDir.getAbsolutePath()
|
||||
+ "offsets.dat",
|
||||
"debezium.properties.offset.flush.interval.ms=60000",
|
||||
|
||||
"debezium.properties.schema.history.internal=io.debezium.storage.file.history.FileSchemaHistory", // new
|
||||
"debezium.properties.schema.history.internal.file.filename=" + anotherTempDir.getAbsolutePath()
|
||||
+ "schemahistory.dat", // new
|
||||
|
||||
"debezium.properties.topic.prefix=my-topic", // new
|
||||
|
||||
"debezium.properties.name=my-sql-connector",
|
||||
"debezium.properties.connector.class=io.debezium.connector.mysql.MySqlConnector",
|
||||
|
||||
"debezium.properties.database.user=debezium",
|
||||
"debezium.properties.database.password=dbz",
|
||||
"debezium.properties.database.hostname=localhost",
|
||||
"debezium.properties.database.port=" + debeziumMySQL.getMappedPort(3306),
|
||||
"debezium.properties.database.server.id=85744",
|
||||
"debezium.properties.database.server.name=my-app-connector",
|
||||
"debezium.properties.database.history=io.debezium.relational.history.MemoryDatabaseHistory",
|
||||
|
||||
// JdbcTemplate configuration
|
||||
String.format("app.datasource.url=jdbc:mysql://localhost:%d/%s?enabledTLSProtocols=TLSv1.2",
|
||||
debeziumMySQL.getMappedPort(3306), DATABASE_NAME),
|
||||
"app.datasource.username=root",
|
||||
"app.datasource.password=debezium",
|
||||
"app.datasource.driver-class-name=com.mysql.cj.jdbc.Driver",
|
||||
"app.datasource.type=com.zaxxer.hikari.HikariDataSource");
|
||||
|
||||
@Test
|
||||
public void consumerTest() {
|
||||
|
||||
logger.info("Temp dir: " + anotherTempDir.getAbsolutePath());
|
||||
|
||||
contextRunner
|
||||
.withPropertyValues(
|
||||
// Flattering:
|
||||
// https://debezium.io/documentation/reference/stable/transformations/event-flattening.html
|
||||
"debezium.properties.transforms=unwrap",
|
||||
"debezium.properties.transforms.unwrap.type=io.debezium.transforms.ExtractNewRecordState",
|
||||
"debezium.properties.transforms.unwrap.drop.tombstones=false",
|
||||
"debezium.properties.transforms.unwrap.delete.handling.mode=rewrite",
|
||||
"debezium.properties.transforms.unwrap.add.fields=name,db")
|
||||
.run(context -> {
|
||||
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
|
||||
|
||||
DebeziumCustomConsumerApplication.TestDebeziumConsumer testConsumer = context
|
||||
.getBean(DebeziumCustomConsumerApplication.TestDebeziumConsumer.class);
|
||||
jdbcTemplate.update(
|
||||
"insert into `customers`(`first_name`,`last_name`,`email`) " +
|
||||
"VALUES('Test666', 'Test666', 'Test666@spring.org')");
|
||||
JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "customers", "first_name = ?", "Test666");
|
||||
|
||||
await().atMost(Duration.ofSeconds(30))
|
||||
.untilAsserted(() -> assertThat(testConsumer.recordList).hasSizeGreaterThanOrEqualTo(52));
|
||||
});
|
||||
}
|
||||
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, MongoAutoConfiguration.class })
|
||||
@Import(TestJdbcTemplateConfiguration.class)
|
||||
public static class DebeziumCustomConsumerApplication {
|
||||
|
||||
@Bean
|
||||
public EmbeddedEngineExecutorService embeddedEngine(DebeziumEngine<?> debeziumEngine) {
|
||||
return new EmbeddedEngineExecutorService(debeziumEngine);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Consumer<ChangeEvent<byte[], byte[]>> customConsumer() {
|
||||
return new TestDebeziumConsumer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom, test, change event Consumer.
|
||||
*/
|
||||
public static class TestDebeziumConsumer implements Consumer<ChangeEvent<byte[], byte[]>> {
|
||||
|
||||
public Map<Object, Object> keyValue = new HashMap<>();
|
||||
|
||||
public List<ChangeEvent<byte[], byte[]>> recordList = new CopyOnWriteArrayList<>();
|
||||
|
||||
public TestDebeziumConsumer() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(ChangeEvent<byte[], byte[]> changeEvent) {
|
||||
if (changeEvent != null) { // ignore null records
|
||||
recordList.add(changeEvent);
|
||||
keyValue.put(changeEvent.key(), changeEvent.value());
|
||||
System.out.println("SIZE=" + recordList.size());
|
||||
System.out.println("[Debezium Event]: " + changeEvent.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.supplier.debezium.custom;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import io.debezium.engine.DebeziumEngine;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
|
||||
/**
|
||||
* The Debezium Engine is designed to be submitted to an {@link Executor} or {@link ExecutorService} for execution by a
|
||||
* single thread, and a running connector can be stopped either by calling {@link #stop()} from another thread or by
|
||||
* interrupting the running thread (e.g., as is the case with {@link ExecutorService#shutdownNow()}).
|
||||
*
|
||||
* The EmbeddedEngineExecutorService provides a sample ExecutorService implementation aligned with the Spring lifecycle.
|
||||
*
|
||||
* Note that the DebeziumReactiveConsumerConfiguration embeds an ExecutorService as part of the
|
||||
* Supplier<Flux<Message<?>>> configuration.
|
||||
*
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
public class EmbeddedEngineExecutorService implements SmartLifecycle, AutoCloseable {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(EmbeddedEngineExecutorService.class);
|
||||
|
||||
private final DebeziumEngine<?> engine;
|
||||
private final ExecutorService executor;
|
||||
private final AtomicBoolean running = new AtomicBoolean(false);
|
||||
|
||||
public EmbeddedEngineExecutorService(DebeziumEngine<?> engine) {
|
||||
this.engine = engine;
|
||||
this.executor = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
logger.info("Start Embedded Engine");
|
||||
this.executor.execute(this.engine);
|
||||
this.running.set(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
this.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
logger.info("Stop Embedded Engine");
|
||||
try {
|
||||
this.engine.close();
|
||||
this.running.set(false);
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.warn("Failed to close the Debezium Engine:", e);
|
||||
}
|
||||
this.executor.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return this.running.get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* Copyright 2023-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.fn.supplier.debezium.supplier;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.fn.supplier.debezium.DebeziumReactiveConsumerConfiguration;
|
||||
import org.springframework.cloud.fn.supplier.debezium.TestJdbcTemplateConfiguration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = {
|
||||
"spring.cloud.function.definition=debeziumSupplier",
|
||||
|
||||
// https://debezium.io/documentation/reference/2.2/transformations/event-flattening.html
|
||||
"debezium.properties.transforms=unwrap",
|
||||
"debezium.properties.transforms.unwrap.type=io.debezium.transforms.ExtractNewRecordState",
|
||||
"debezium.properties.transforms.unwrap.drop.tombstones=true",
|
||||
"debezium.properties.transforms.unwrap.delete.handling.mode=rewrite",
|
||||
"debezium.properties.transforms.unwrap.add.fields=name,db,op,table",
|
||||
|
||||
"debezium.properties.schema.history.internal=io.debezium.relational.history.MemorySchemaHistory",
|
||||
"debezium.properties.offset.storage=org.apache.kafka.connect.storage.MemoryOffsetBackingStore",
|
||||
|
||||
// Drop schema from the message payload.
|
||||
"debezium.properties.key.converter.schemas.enable=false",
|
||||
"debezium.properties.value.converter.schemas.enable=false",
|
||||
|
||||
"debezium.properties.topic.prefix=my-topic",
|
||||
"debezium.properties.name=my-connector",
|
||||
"debezium.properties.database.server.id=85744",
|
||||
"debezium.properties.database.server.name=my-app-connector",
|
||||
"debezium.properties.connector.class=io.debezium.connector.mysql.MySqlConnector",
|
||||
"debezium.properties.database.user=debezium",
|
||||
"debezium.properties.database.password=dbz",
|
||||
"debezium.properties.database.hostname=localhost",
|
||||
|
||||
"debezium.properties.table.include.list=inventory.customers, inventory.addresses",
|
||||
|
||||
// JdbcTemplate config.
|
||||
"app.datasource.username=root",
|
||||
"app.datasource.password=debezium",
|
||||
"app.datasource.driver-class-name=com.mysql.cj.jdbc.Driver",
|
||||
"app.datasource.type=com.zaxxer.hikari.HikariDataSource"
|
||||
})
|
||||
@Testcontainers
|
||||
public class DebeziumSupplierTest {
|
||||
|
||||
public static final String IMAGE_TAG = "2.2.0.Final";
|
||||
public static final String DEBEZIUM_EXAMPLE_MYSQL_IMAGE = "debezium/example-mysql:" + IMAGE_TAG;
|
||||
|
||||
@Container
|
||||
static GenericContainer<?> debeziumMySQL = new GenericContainer<>(DEBEZIUM_EXAMPLE_MYSQL_IMAGE)
|
||||
.withEnv("MYSQL_ROOT_PASSWORD", "debezium")
|
||||
.withEnv("MYSQL_USER", "mysqluser")
|
||||
.withEnv("MYSQL_PASSWORD", "mysqlpw")
|
||||
.withExposedPorts(3306);
|
||||
|
||||
@DynamicPropertySource
|
||||
static void mysqlDbProperties(DynamicPropertyRegistry registry) {
|
||||
registry.add("debezium.properties.database.port", () -> debeziumMySQL.getMappedPort(3306));
|
||||
registry.add("app.datasource.url",
|
||||
() -> String.format("jdbc:mysql://localhost:%d/%s?enabledTLSProtocols=TLSv1.2",
|
||||
debeziumMySQL.getMappedPort(3306), "inventory")); // JdbcTemplate config.
|
||||
}
|
||||
|
||||
private ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Autowired
|
||||
private Supplier<Flux<Message<?>>> debeziumSupplier;
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Test
|
||||
void testDebeziumSupplier() {
|
||||
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO `customers`(`first_name`,`last_name`,`email`) VALUES('Test666', 'Test666', 'Test666@spring.org')");
|
||||
// jdbcTemplate.update("DELETE FROM `customers` WHERE `first_name` = ?", "Test666");
|
||||
|
||||
Flux<Message<?>> messageFlux = this.debeziumSupplier.get();
|
||||
|
||||
// Message size should correspond to the number of insert statements in:
|
||||
// https://github.com/debezium/container-images/blob/main/examples/mysql/2.2/inventory.sql
|
||||
// filtered by Customers and Addresses table.
|
||||
StepVerifier.create(messageFlux)
|
||||
.expectNextCount(16) // Skip the DDL transaction logs.
|
||||
|
||||
// Customers table
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":1001,\"first_name\":\"Sally\",\"last_name\":\"Thomas\",\"email\":\"sally.thomas@acme.com\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"customers\",\"__deleted\":\"false\"}"))
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":1002,\"first_name\":\"George\",\"last_name\":\"Bailey\",\"email\":\"gbailey@foobar.com\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"customers\",\"__deleted\":\"false\"}"))
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":1003,\"first_name\":\"Edward\",\"last_name\":\"Walker\",\"email\":\"ed@walker.com\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"customers\",\"__deleted\":\"false\"}"))
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":1004,\"first_name\":\"Anne\",\"last_name\":\"Kretchmar\",\"email\":\"annek@noanswer.org\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"customers\",\"__deleted\":\"false\"}"))
|
||||
|
||||
// NEW Customer Insert
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":1005,\"first_name\":\"Test666\",\"last_name\":\"Test666\",\"email\":\"Test666@spring.org\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"customers\",\"__deleted\":\"false\"}"))
|
||||
|
||||
// Addresses table
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":10,\"customer_id\":1001,\"street\":\"3183 Moore Avenue\",\"city\":\"Euless\",\"state\":\"Texas\",\"zip\":\"76036\",\"type\":\"SHIPPING\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"addresses\",\"__deleted\":\"false\"}"))
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":11,\"customer_id\":1001,\"street\":\"2389 Hidden Valley Road\",\"city\":\"Harrisburg\",\"state\":\"Pennsylvania\",\"zip\":\"17116\",\"type\":\"BILLING\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"addresses\",\"__deleted\":\"false\"}"))
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":12,\"customer_id\":1002,\"street\":\"281 Riverside Drive\",\"city\":\"Augusta\",\"state\":\"Georgia\",\"zip\":\"30901\",\"type\":\"BILLING\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"addresses\",\"__deleted\":\"false\"}"))
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":13,\"customer_id\":1003,\"street\":\"3787 Brownton Road\",\"city\":\"Columbus\",\"state\":\"Mississippi\",\"zip\":\"39701\",\"type\":\"SHIPPING\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"addresses\",\"__deleted\":\"false\"}"))
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":14,\"customer_id\":1003,\"street\":\"2458 Lost Creek Road\",\"city\":\"Bethlehem\",\"state\":\"Pennsylvania\",\"zip\":\"18018\",\"type\":\"SHIPPING\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"addresses\",\"__deleted\":\"false\"}"))
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":15,\"customer_id\":1003,\"street\":\"4800 Simpson Square\",\"city\":\"Hillsdale\",\"state\":\"Oklahoma\",\"zip\":\"73743\",\"type\":\"BILLING\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"addresses\",\"__deleted\":\"false\"}"))
|
||||
.assertNext((message) -> assertThat(payloadString(message))
|
||||
.isEqualTo(
|
||||
"{\"id\":16,\"customer_id\":1004,\"street\":\"1289 University Hill Road\",\"city\":\"Canehill\",\"state\":\"Arkansas\",\"zip\":\"72717\",\"type\":\"LIVING\",\"__name\":\"my-topic\",\"__db\":\"inventory\",\"__op\":\"r\",\"__table\":\"addresses\",\"__deleted\":\"false\"}"))
|
||||
.thenCancel()
|
||||
.verify();
|
||||
|
||||
}
|
||||
|
||||
private String payloadString(Message<?> message) {
|
||||
return new String((byte[]) message.getPayload());
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAutoConfiguration(exclude = { MongoAutoConfiguration.class })
|
||||
@Import({DebeziumReactiveConsumerConfiguration.class, TestJdbcTemplateConfiguration.class})
|
||||
static class DebeziumSupplierTestApplication {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,4 +27,4 @@
|
||||
|
||||
<logger name="org.testcontainers" level="INFO"/>
|
||||
<logger name="com.github.dockerjava" level="WARN"/>
|
||||
</configuration>
|
||||
</configuration>
|
||||
@@ -26,7 +26,7 @@
|
||||
<module>websocket-supplier</module>
|
||||
<module>s3-supplier</module>
|
||||
<module>twitter-supplier</module>
|
||||
<module>cdc-debezium-supplier</module>
|
||||
<module>debezium-supplier</module>
|
||||
<module>syslog-supplier</module>
|
||||
<module>xmpp-supplier</module>
|
||||
<module>zeromq-supplier</module>
|
||||
|
||||
Reference in New Issue
Block a user