spring 如何创建Hazelcast数据源

qnakjoqk  于 2023-04-19  发布在  Spring
关注(0)|答案(1)|浏览(143)

大家好,我目前正在尝试使用Hazelcast作为Spring Batch的持久层,为此我需要创建一个具有Hazelcast支持的DataSource。这样,Spring Batch和Spring Data等项目可以使用Hazelcast + Spring Integration提供的Repository和TransactionManager。
有人知道如何创建指向Hazelcast的DataSource吗?
我试过这样定义一个bean,但没有用,它不起作用:

@Configuration
public class DataSourceConfig {

//    @Value("${spring.datasource.url}")
//    String url;
//    @Value("${spring.datasource.username}")
//    String userName;
//    @Value("${spring.datasource.password}")
//    String pwd;

    @SneakyThrows
    @Bean
    public JobRepository hazelCastJobRepo(DataSource dataSource, HazelcastTransactionManager hazelcastTransactionManager){
        JobRepositoryFactoryBean jobRepositoryFactoryBean = new JobRepositoryFactoryBean();
        jobRepositoryFactoryBean.setDataSource(dataSource);
        jobRepositoryFactoryBean.setDatabaseType("POSTGRES");
        jobRepositoryFactoryBean.setTransactionManager(hazelcastTransactionManager);
        return jobRepositoryFactoryBean.getObject();
    }

    @Bean
    public HazelcastInstance hazelcastInstance() {
        ClientConfig config = new ClientConfig();
        config.setClusterName("dev");
        return HazelcastClient.newHazelcastClient(config);
    }

    @Bean
    public HazelcastTransactionManager hazelcastTransactionManager(HazelcastInstance hazelcastInstance) {
        return new HazelcastTransactionManager(hazelcastInstance);
    }

    @Bean
    public JobBuilderFactory jobBuilderFactory(JobRepository hazelCastJobRepo){
        JobBuilderFactory jobBuilderFactory = new JobBuilderFactory(hazelCastJobRepo);
        return jobBuilderFactory;
    }

    @Bean
    public StepBuilderFactory stepBuilderFactory(JobRepository hazelCastJobRepo, HazelcastTransactionManager hazelcastTransactionManager){
        StepBuilderFactory stepBuilderFactory = new StepBuilderFactory(hazelCastJobRepo, hazelcastTransactionManager);
        return stepBuilderFactory;
    }

    @Bean
    public JobExplorer jobExplorer(){
        JobInstanceDao jdbcJobInstanceDao = new JdbcJobInstanceDao();
        JobExecutionDao jobExecutionDao = new JdbcJobExecutionDao();
        StepExecutionDao stepExecutionDao = new JdbcStepExecutionDao();
        ExecutionContextDao executionContextDao = new JdbcExecutionContextDao();
        return new SimpleJobExplorer(jdbcJobInstanceDao, jobExecutionDao, stepExecutionDao, executionContextDao);
    }

    @Bean
    public ListableJobLocator listableJobLocator(){
        return new MapJobRegistry();
    }

    @Bean
    public DataSource dataSource() {
        return new DriverDataSource("jdbc:mysql://127.0.0.1:5701", "com.hazelcast.datastore.JdbcDataStoreFactory", new Properties(), "", "");
    }

}
x4shl7ld

x4shl7ld1#

参见com.hazelcast.config.Config

/**
 * Adds an external data store configuration.
 * <p>
 * <p>
 * Example:
 * <pre>{@code
 *      Config config = new Config();
 *      Properties properties = new Properties();
 *      properties.put("jdbcUrl", jdbcUrl);
 *      properties.put("username", username);
 *      properties.put("password", password);
 *      ExternalDataStoreConfig externalDataStoreConfig = new ExternalDataStoreConfig()
 *              .setName("my-jdbc-data-store")
 *              .setClassName(JdbcDataStoreFactory.class.getName())
 *              .setProperties(properties);
 *      config.addExternalDataStoreConfig(externalDataStoreConfig);
 * }</pre>
 *
 * @since 5.2
 */
@Beta
public Config addExternalDataStoreConfig(ExternalDataStoreConfig externalDataStoreConfig) {

注册外部数据存储。
看起来你需要这样做:

NodeEngineImpl nodeEngine = Util.getNodeEngine(hazelcastInstance);
ExternalDataStoreFactory<?> dataStoreFactory = nodeEngine.getExternalDataStoreService().getExternalDataStoreFactory(name);

并将其命名为getDataStore()
这是一个新的东西,因为Hazelcast 5.2,看起来我们真的需要它的喷气流处理器。不知道为什么会处理Hazelcast门面,如果它仍然是一个外部数据库,所以你可以简单地配置常规DataSourcejdbc:mysql://127.0.0.1:5701

相关问题