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

时间:2018-04-22 17:59:42

标签: java spring hibernate spring-boot

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

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

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

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

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

首先我创建了一个 CustomerClient

@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()));
        }
    }
}

然后我创建了 ProductClient

@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()));
        }
    }
}

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

CustomerStatsCollectorService

@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);
    }
}

ProductStatsCollectorService

@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
                + "}";
    }
}

最后是我的两个存储库:

CustomerRepository

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

ProductRepository

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

主要应用程序如下:

@SpringBootApplication
@EnableScheduling
public class StatsCollectorApplication {

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

我的 application.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中的字段customerRepository需要一个名为&#39; entityManagerFactory&#39;的bean。无法找到。   行动:   考虑定义一个名为&#39; entityManagerFactory&#39;在你的   组态。   处理以退出代码1完成

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

My application structure

最后,我的 pom.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>

1 个答案:

答案 0 :(得分:0)

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