spring boot 多数据源

连接池用的是Hikari

首先看配置文件(密码用jasypt加密):
spring.datasource.primary.url=jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=UTF-8
spring.datasource.primary.username=root
spring.datasource.primary.password=ENC(hZ1oe0u6d7jAbgH/KEbVH8yQiVs6Iee+)

spring.datasource.secondary.url=jdbc:mysql://127.0.0.1:3306/work?useUnicode=true&characterEncoding=UTF-8
spring.datasource.secondary.username=user_pro
spring.datasource.secondary.password=ENC(hZ1oe0u6d7jAbgH/KEbVH8yQiVs6Iee+)

jasypt.encryptor.password=qwer1234tyu

Hikari配置:

@Configuration
@CommonsLog
public class DataSourcesConfig {

    @Value("${spring.datasource.primary.url}")
    private String primaryUrl;
    @Value("${spring.datasource.primary.username}")
    private String primaryUsername;
    @Value("${spring.datasource.primary.password}")
    private String primaryPassword;
    @Value("${spring.datasource.secondary.url}")
    private String secondaryUrl;
    @Value("${spring.datasource.secondary.username}")
    private String secondaryUsername;
    @Value("${spring.datasource.secondary.password}")
    private String secondaryPassword;

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public HikariDataSource primaryDataSource() {
        log.info("主库链接创建!");
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setJdbcUrl(primaryUrl);
        hikariDataSource.setUsername(primaryUsername);
        hikariDataSource.setPassword(primaryPassword);
        hikariConfig(hikariDataSource);
        return hikariDataSource;
    }

    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.secondary")
    public HikariDataSource secondaryDataSource() {
        log.info("从库(只读)链接创建!");
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setJdbcUrl(secondaryUrl);
        hikariDataSource.setUsername(secondaryUsername);
        hikariDataSource.setPassword(secondaryPassword);
        hikariConfig(hikariDataSource);
        return hikariDataSource;
    }

    private void hikariConfig(HikariDataSource hikariDataSource) {
        hikariDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        hikariDataSource.setAutoCommit(true);
        hikariDataSource.setReadOnly(false);
        hikariDataSource.setMaximumPoolSize(100);
        hikariDataSource.setMaxLifetime(1800000L);
        hikariDataSource.setIdleTimeout(600000L);
        hikariDataSource.setMinimumIdle(5);
        hikariDataSource.setConnectionTimeout(60000L);
        hikariDataSource.setValidationTimeout(60000L);
        hikariDataSource.setConnectionTestQuery("SELECT 1");
    }
}

主库配置:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryPrimary",
        transactionManagerRef="transactionManagerPrimary",
        basePackages= {"com.demo.dao.dao1"})
public class PrimaryConfig {

    @Autowired @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder){
        return builder
                .dataSource(primaryDataSource)
                .properties(getVendorProperties(primaryDataSource))
                //设置实体类所在位置
                .packages("com.demo.model1")
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    @Autowired(required=false)
    private JpaProperties jpaProperties;

    private Map getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }

从库配置:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactorySecondary",
        transactionManagerRef="transactionManagerSecondary",
        basePackages= {"com.demo.dao2"})
public class SecondaryConfig {

    @Autowired(required = false)
    private JpaProperties jpaProperties;

    @Autowired @Qualifier("secondaryDataSource")
    private DataSource secondaryDataSource;

    @Bean(name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySecondary(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(secondaryDataSource)
                .properties(getVendorProperties(secondaryDataSource))
                //设置实体类所在位置
                .packages("com.demo.model2")
                .persistenceUnit("secondaryPersistenceUnit")
                .build();
    }


    private Map getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Bean(name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    }
}

 

这样配置就好了。在写代码的时候,dao用不同的文件夹区分开来,注意:在使用事务的时候,得在调用dao的service方法上声明是主库的事务还是从库的事务。

 

你可能感兴趣的:(spring,boot,事务,Hikari,多数据源)