BAEL-4467: Introduction to Debezium
This commit is contained in:
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.libraries.debezium;
|
||||
|
||||
import com.baeldung.libraries.debezium.repository.CustomerRepository;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = DebeziumCDCApplication.class)
|
||||
@ActiveProfiles("test")
|
||||
public class DebeziumCDCLiveTest {
|
||||
|
||||
@Autowired
|
||||
private CustomerRepository customerRepository;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("sourceJdbcTemplate")
|
||||
private NamedParameterJdbcTemplate jdbcTemplate;
|
||||
|
||||
@Before
|
||||
public void clearData() {
|
||||
jdbcTemplate.update("delete from customer where id = :id", Collections.singletonMap("id", 1));
|
||||
}
|
||||
|
||||
@DynamicPropertySource
|
||||
static void registerProperties(DynamicPropertyRegistry registry) {
|
||||
registry.add("customer.datasource.port", MySQLTestContainerConfiguration::getPort);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsertDataToSourceDatabase_thenCdcOk() throws InterruptedException {
|
||||
assertThat(customerRepository.findAll().size()).isZero();
|
||||
|
||||
// insert data to source DB
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", 1);
|
||||
map.put("fullname", "John Doe");
|
||||
map.put("email", "test@test.com");
|
||||
|
||||
jdbcTemplate.update("INSERT INTO customer(id, fullname, email) VALUES (:id, :fullname, :email)", map);
|
||||
|
||||
// verify target DB
|
||||
Thread.sleep(10000);
|
||||
assertThat(customerRepository.findAll().size()).isNotZero();
|
||||
}
|
||||
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.libraries.debezium;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
import org.testcontainers.containers.MySQLContainer;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class MySQLTestContainerConfiguration {
|
||||
|
||||
public static final DockerImageName MYSQL_IMAGE = DockerImageName.parse("mysql:8.0");
|
||||
|
||||
private static final MySQLContainer<?> mysqlContainer = new MySQLContainer<>(MYSQL_IMAGE)
|
||||
.withCommand("--default-authentication-plugin=mysql_native_password")
|
||||
.withInitScript("debezium/customer.sql")
|
||||
.withDatabaseName("SOURCE_DB")
|
||||
.withUsername("user")
|
||||
.withPassword("user")
|
||||
.withEnv("MYSQL_ROOT_PASSWORD", "user");
|
||||
|
||||
MySQLTestContainerConfiguration() {
|
||||
mysqlContainer.start();
|
||||
}
|
||||
|
||||
public static int getPort() {
|
||||
return mysqlContainer.getFirstMappedPort();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public EmbeddedDatabase targetDatasource() {
|
||||
return new EmbeddedDatabaseBuilder()
|
||||
.setType(EmbeddedDatabaseType.H2)
|
||||
.setName("TAGRET_DB")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean(name = "SOURCE_DS")
|
||||
public DataSource sourceDataSource() {
|
||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
||||
dataSource.setUrl(mysqlContainer.getJdbcUrl());
|
||||
dataSource.setUsername(mysqlContainer.getUsername());
|
||||
dataSource.setPassword(mysqlContainer.getPassword());
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean(name = "sourceJdbcTemplate")
|
||||
public NamedParameterJdbcTemplate getJdbcTemplate(@Qualifier("SOURCE_DS") DataSource sourceDataSource) {
|
||||
return new NamedParameterJdbcTemplate(sourceDataSource);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
## Source Database Properties
|
||||
customer:
|
||||
datasource:
|
||||
host: localhost
|
||||
database: SOURCE_DB
|
||||
username: root
|
||||
password: user
|
||||
@@ -0,0 +1,9 @@
|
||||
drop table if exists customer;
|
||||
|
||||
CREATE TABLE customer
|
||||
(
|
||||
id integer NOT NULL,
|
||||
fullname character varying(255),
|
||||
email character varying(255),
|
||||
CONSTRAINT customer_pkey PRIMARY KEY (id)
|
||||
);
|
||||
Reference in New Issue
Block a user