Springboot 应用程序从多个数据源读取到新数据库中



我正在创建一个新的 Springboot 应用程序,我想实现以下目标。

我需要有多个数据源(不同的只读数据库)从中读取信息。 收集的信息我需要将其存储在一个新的不同数据库中。

由于我从中获取数据的数据库是只读的,因此我不打算使用 Jpa 或 Hibernate。

但是为了存储从获得的数据中实际新生成的对象,我想使用 Hibernate 来让我更容易。

通过执行以下操作,我能够从多个数据源读取数据。

首先,我创建了一个客户客户端

@Configuration
public class CustomerClient {
@Bean(name = "customerDataSource")
@ConfigurationProperties("stats-collector.clients.customers")
public DataSource getDataSource() {
return DataSourceBuilder.create().build();
}
public List<Customer> getCustomers() {
String sql = "SELECT * FROM customer";
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
return jdbcTemplate.query(sql, new CustomerRowMapper());
}
public static class CustomerRowMapper implements RowMapper<Customer> {
@Override
public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Customer(rs.getLong("id"), rs.getString("name"), new Timestamp(System.currentTimeMillis()));
}
}
}

然后我创建了一个产品客户端

@Configuration
public class ProductClient {
@Bean(name = "productDataSource")
@ConfigurationProperties("stats-collector.clients.products")
public DataSource getDataSource() {
return DataSourceBuilder.create().build();
}
public List<Product> getProducts() {
String sql = "SELECT * FROM product";
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
return jdbcTemplate.query(sql, new ProductRowMapper());
}
public static class ProductRowMapper implements RowMapper<Product> {
@Override
public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Product(rs.getLong("id"), rs.getString("name"), rs.getDouble("price"), new Timestamp(System.currentTimeMillis()));
}
}
}

有了这个,我能够从两个不同的数据库中检索信息。 为了尝试这个,我创建了两个服务:

客户统计收集器服务

@Service
public class CustomerStatsCollectorService {
private static final Logger logger = LoggerFactory.getLogger(CustomerStatsCollectorService.class);
@Autowired
private CustomerClient customerClient;
@Autowired
private CustomerRepository customerRepository;
@Scheduled(cron = "0 * * * * ?")
public void collectInformation() {
List<Customer> customers = customerClient.getCustomers();
logger.info("Collected information from customers database:: Customers fetched - {}", customers.size());
customerRepository.saveAll(customers);
}
}

产品统计收集器服务

@Service
public class ProductStatsCollectorService {
private static final Logger logger = LoggerFactory.getLogger(ProductStatsCollectorService.class);
@Autowired
private ProductClient productClient;
@Autowired
private ProductRepository productRepository;
@Scheduled(cron = "0 * * * * ?")
public void collectInformation() {
List<Product> products = productClient.getProducts();
logger.info("Collected information from products database :: Products fetched - {}", products.size());
productRepository.saveAll(products);
}
}

我的模型是用以下内容完成的:

客户

@Entity
public class Customer implements Serializable {
@Id
private Long id;
private String name;
private Timestamp timeCreated;
public Customer() {
}
public Customer(Long id, String name, Timestamp timeCreated) {
this.id = id;
this.name = name;
this.timeCreated = timeCreated;
}
public Long getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Timestamp getTimeCreated() {
return timeCreated;
}
@Override
public String toString() {
return "Customer {"
+ "id: " + id
+ ", name: " + name
+ "}";
}
}

产品

@Entity
public class Product implements Serializable {
@Id
private Long id;
private String name;
private Double price;
private Timestamp timeCreated;
public Product() {
}
public Product(Long id, String name, Double price, Timestamp timeCreated) {
this.id = id;
this.name = name;
this.price = price;
this.timeCreated = timeCreated;
}
public Long getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Double getPrice() {
return this.price;
}
public Timestamp getTimeCreated() {
return timeCreated;
}
@Override
public String toString() {
return "Target {"
+ "id: " + id
+ ", name: " + name
+ ", price: " + price
+ "}";
}
}

最后是我的两个存储库:

客户存储库

public interface CustomerRepository extends CrudRepository<Customer, Long> {
}

产品存储库

public interface ProductRepository extends CrudRepository<Product, Long> {
}

主要应用程序具有以下内容:

@SpringBootApplication
@EnableScheduling
public class StatsCollectorApplication {
public static void main(String[] args) {
SpringApplication.run(StatsCollectorApplication.class, args);
}
}

我的应用程序.yml配置文件:

stats-collector:
clients:
customers:
jdbc-url: jdbc:postgresql://localhost:5432/customersDatabase
username: postgres
password:
products:
jdbc-url: jdbc:postgresql://localhost:5432/productsDatabase
username: postgres
password:
spring:
datasource:
jdbc-url: jdbc:postgresql://localhost:5432/testdb
username: postgres
password:
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
id:
new_generator_mappings: false
hibernate:
ddl-auto: create

启动应用程序后,出现以下错误:

com.test.stats.collector.service.CustomerStatsCollectorService 中的 Field customerRepository 需要一个名为"entityManagerFactory"的 bean,但找不到。 行动: 考虑在 配置。 进程已完成,退出代码为 1

应用程序从不同的数据源收集信息,直到我添加 jpa 和存储库来存储该数据。我不明白如何解决这个问题,因为我只想从产品数据库和客户数据库中读取对象,并且仅使用 Hibernate 将数据存储到新数据库中。

我的应用程序结构

最后,我的绒球.xml

<?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>
<artifactId>amc-stats-collector-service</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AMC Stats Collector</name>
<description>AMC Stats Collector</description>
<parent>
<groupId>com.mulesoft.amc</groupId>
<artifactId>amc-stats-collector</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

问题是你正在使用CrudRepository,这需要符合JPA的框架。您可能对Spring Data JDBC感兴趣。

最新更新