package com.yaotiao.mall.search.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@PropertySource( "classpath:application-${spring.profiles.active}.yml")
@EnableJpaRepositories(
basePackages = {"com.yaotiao.mall.search.dao.source"},
entityManagerFactoryRef = "sourceEntityManagerFactory",
transactionManagerRef = "sourceTransactionManager"
)
public class SourceConfig {
@Autowired
JpaProperties jpaProperties;
@Autowired
@Qualifier("druidSource")
private DataSource druidDataSource;
@Autowired
private Environment env;
@Bean(name = "sourceEntityManager")
@Primary
public EntityManager sourceEntityManager(EntityManagerFactoryBuilder builder) {
return sourceEntityManagerFactory(builder).getObject().createEntityManager();
}
@Bean(name = "sourceEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean sourceEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(druidDataSource)
.properties(getVendorProperties())
.packages("com.yaotiao.mall.search.entity.source")
.persistenceUnit("persistenceUnitSpring")
.build();
}
private Map<String, Object> getVendorProperties() {
HibernateProperties hibernateProperties = new HibernateProperties();
Map<String, Object> map = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(),new HibernateSettings());
map.put("hibernate.dialect", env.getProperty("spring.jpa.database-platform"));
map.put("hibernate.show_sql", env.getProperty("spring.jpa.show-sql"));
return map;
}
@Bean(name = "sourceTransactionManager")
@Primary
PlatformTransactionManager sourceTransactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(sourceEntityManagerFactory(builder).getObject());
}
}
package com.yaotiao.mall.search.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@PropertySource( "classpath:application-${spring.profiles.active}.yml")
@EnableJpaRepositories(
basePackages = {"com.yaotiao.mall.search.dao.target"},
entityManagerFactoryRef = "targetEntityManagerFactory",
transactionManagerRef = "targetTransactionManager"
)
public class TargetConfig {
@Autowired
JpaProperties jpaProperties;
@Autowired
@Qualifier("druidTarget")
private DataSource druidDataSource;
@Autowired
private Environment env;
@Bean(name = "targetEntityManager")
public EntityManager targetEntityManager(EntityManagerFactoryBuilder builder) {
return targetEntityManagerFactory(builder).getObject().createEntityManager();
}
@Bean(name = "targetEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean targetEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(druidDataSource)
.properties(getVendorProperties())
.packages("com.yaotiao.mall.search.entity.target")
.persistenceUnit("persistenceUnitSpring")
.build();
}
private Map<String, Object> getVendorProperties() {
HibernateProperties hibernateProperties = new HibernateProperties();
Map<String, Object> map = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(),new HibernateSettings());
map.put("hibernate.dialect", env.getProperty("spring.jpa.database-platform"));
map.put("hibernate.show_sql", env.getProperty("spring.jpa.show-sql"));
map.put("hibernate.hbm2ddl.auto",env.getProperty("spring.jpa.hibernate.ddl-auto"));
return map;
}
@Bean(name = "targetTransactionManager")
PlatformTransactionManager targetTransactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(targetEntityManagerFactory(builder).getObject());
}
}
package com.yaotiao.mall.search.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@PropertySource( "classpath:application-${spring.profiles.active}.yml")
public class DruidSourceConfig {
@Value("${spring.datasource.driver-class-name}")
String driverClassName;
@Value("${spring.datasource.source.url}")
String url;
@Value("${spring.datasource.source.username}")
String username;
@Value("${spring.datasource.source.password}")
String password;
@Value("${spring.datasource.initialSize}")
int initialSize;
@Value("${spring.datasource.minIdle}")
int minIdle;
@Value("${spring.datasource.maxActive}")
int maxActive;
@Value("${spring.datasource.maxWait}")
int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.validationQuery}")
String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.filters}")
String filters;
@Value("${spring.datasource.connectionProperties}")
String connectionProperties;
@Value("${spring.datasource.useGlobalDataSourceStat}")
boolean useGlobalDataSourceStat;
@Bean(name = "druidSource", initMethod = "init", destroyMethod = "close")
@Primary
@Qualifier("druidDataSource")
public DataSource dataSource() throws Exception {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
druidDataSource.setDriverClassName(driverClassName);
druidDataSource.setInitialSize(initialSize);
druidDataSource.setMaxActive(maxActive);
druidDataSource.setMinIdle(minIdle);
druidDataSource.setMaxWait(maxWait);
druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
druidDataSource.setValidationQuery(validationQuery);
druidDataSource.setTestWhileIdle(testWhileIdle);
druidDataSource.setTestOnBorrow(testOnBorrow);
druidDataSource.setTestOnReturn(testOnReturn);
druidDataSource.setPoolPreparedStatements(poolPreparedStatements);
druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
druidDataSource.setFilters(filters);
try {
if (null != druidDataSource) {
druidDataSource.setUseGlobalDataSourceStat(true);
Properties properties = new Properties();
properties.setProperty("decrypt", "true");
druidDataSource.setConnectProperties(properties);
druidDataSource.init();
}
} catch (Exception e) {
throw new RuntimeException(
"load datasource error, dbProperties is :", e);
}
return druidDataSource;
}
}
package com.yaotiao.mall.search.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@PropertySource( "classpath:application-${spring.profiles.active}.yml")
public class DruidTargetConfig {
@Value("${spring.datasource.driver-class-name}")
String driverClassName;
@Value("${spring.datasource.target.url}")
String url;
@Value("${spring.datasource.target.username}")
String username;
@Value("${spring.datasource.target.password}")
String password;
@Value("${spring.datasource.initialSize}")
int initialSize;
@Value("${spring.datasource.minIdle}")
int minIdle;
@Value("${spring.datasource.maxActive}")
int maxActive;
@Value("${spring.datasource.maxWait}")
int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.validationQuery}")
String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.filters}")
String filters;
@Value("${spring.datasource.connectionProperties}")
String connectionProperties;
@Value("${spring.datasource.useGlobalDataSourceStat}")
boolean useGlobalDataSourceStat;
@Bean(name = "druidTarget", initMethod = "init", destroyMethod = "close")
@Qualifier("druidDataSource")
public DataSource dataSource() throws Exception {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
druidDataSource.setDriverClassName(driverClassName);
druidDataSource.setInitialSize(initialSize);
druidDataSource.setMaxActive(maxActive);
druidDataSource.setMinIdle(minIdle);
druidDataSource.setMaxWait(maxWait);
druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
druidDataSource.setValidationQuery(validationQuery);
druidDataSource.setTestWhileIdle(testWhileIdle);
druidDataSource.setTestOnBorrow(testOnBorrow);
druidDataSource.setTestOnReturn(testOnReturn);
druidDataSource.setPoolPreparedStatements(poolPreparedStatements);
druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
druidDataSource.setFilters(filters);
try {
if (null != druidDataSource) {
druidDataSource.setUseGlobalDataSourceStat(true);
Properties properties = new Properties();
properties.setProperty("decrypt", "true");
druidDataSource.setConnectProperties(properties);
druidDataSource.init();
}
} catch (Exception e) {
throw new RuntimeException(
"load datasource error, dbProperties is :", e);
}
return druidDataSource;
}
}
package com.yaotiao.mall.search.config;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
public class ThreadConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(100);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(99999);
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}