mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
refactor: optimize code for pull request 249,fix issue #153
This commit is contained in:
parent
5f0865129e
commit
a0f76f7ba9
@ -9,15 +9,13 @@ import java.util.*;
|
|||||||
/**
|
/**
|
||||||
* 多重数据源配置
|
* 多重数据源配置
|
||||||
*
|
*
|
||||||
* @author tjq
|
* @author Kung Yao
|
||||||
* @since 2020/4/27
|
* @since 2020/4/27
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@ConfigurationProperties("spring.datasource")
|
@ConfigurationProperties("spring.datasource")
|
||||||
public class MultiDatasourceProperties {
|
public class MultiDatasourceProperties {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private DataSourceProperties remote = new DataSourceProperties();
|
private DataSourceProperties remote = new DataSourceProperties();
|
||||||
|
|
||||||
private DataSourceProperties local = new DataSourceProperties();
|
private DataSourceProperties local = new DataSourceProperties();
|
||||||
@ -27,7 +25,7 @@ public class MultiDatasourceProperties {
|
|||||||
|
|
||||||
private HibernateProperties hibernate = new HibernateProperties();
|
private HibernateProperties hibernate = new HibernateProperties();
|
||||||
|
|
||||||
public void setHibernate( HibernateProperties hibernate ) {
|
public void setHibernate(HibernateProperties hibernate) {
|
||||||
this.hibernate = hibernate;
|
this.hibernate = hibernate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,9 +37,9 @@ public class MultiDatasourceProperties {
|
|||||||
|
|
||||||
public static class HibernateProperties {
|
public static class HibernateProperties {
|
||||||
|
|
||||||
private Map<String,String> properties = Maps.newHashMap();
|
private Map<String, String> properties = Maps.newHashMap();
|
||||||
|
|
||||||
public void setProperties( Map<String, String> properties ) {
|
public void setProperties(Map<String, String> properties) {
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,11 +48,11 @@ public class MultiDatasourceProperties {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLocal( DataSourceProperties local ) {
|
public void setLocal(DataSourceProperties local) {
|
||||||
this.local = local;
|
this.local = local;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRemote( DataSourceProperties remote ) {
|
public void setRemote(DataSourceProperties remote) {
|
||||||
this.remote = remote;
|
this.remote = remote;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
package tech.powerjob.server.persistence.config;
|
|
||||||
|
|
||||||
import org.hibernate.dialect.PostgreSQL10Dialect;
|
|
||||||
import org.hibernate.type.descriptor.sql.LongVarcharTypeDescriptor;
|
|
||||||
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
|
|
||||||
|
|
||||||
import java.sql.Types;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Kung Yao
|
|
||||||
* 2021/3/24 下午 04:23
|
|
||||||
* 1074_King
|
|
||||||
*/
|
|
||||||
public class PowerJobPGDialect extends PostgreSQL10Dialect {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SqlTypeDescriptor remapSqlTypeDescriptor( SqlTypeDescriptor sqlTypeDescriptor ) {
|
|
||||||
if ( Types.CLOB == sqlTypeDescriptor.getSqlType() ) {
|
|
||||||
return LongVarcharTypeDescriptor.INSTANCE;
|
|
||||||
}
|
|
||||||
return super.remapSqlTypeDescriptor( sqlTypeDescriptor );
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,5 @@
|
|||||||
package tech.powerjob.server.persistence.config;
|
package tech.powerjob.server.persistence.config;
|
||||||
|
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
|
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
|
||||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
|
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
|
||||||
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
|
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
|
||||||
@ -10,7 +7,6 @@ import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Primary;
|
import org.springframework.context.annotation.Primary;
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
@ -46,12 +42,10 @@ public class RemoteJpaConfig {
|
|||||||
@Resource(name = "multiDatasourceProperties")
|
@Resource(name = "multiDatasourceProperties")
|
||||||
private MultiDatasourceProperties properties;
|
private MultiDatasourceProperties properties;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static final String CORE_PACKAGES = "tech.powerjob.server.persistence.remote";
|
public static final String CORE_PACKAGES = "tech.powerjob.server.persistence.remote";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成配置文件,包括 JPA配置文件和Hibernate配置文件,相当于一下三个配置
|
* 生成配置文件,包括 JPA配置文件和Hibernate配置文件,相当于以下三个配置
|
||||||
* spring.jpa.show-sql=false
|
* spring.jpa.show-sql=false
|
||||||
* spring.jpa.open-in-view=false
|
* spring.jpa.open-in-view=false
|
||||||
* spring.jpa.hibernate.ddl-auto=update
|
* spring.jpa.hibernate.ddl-auto=update
|
||||||
@ -64,10 +58,6 @@ public class RemoteJpaConfig {
|
|||||||
jpaProperties.setOpenInView(false);
|
jpaProperties.setOpenInView(false);
|
||||||
jpaProperties.setShowSql(false);
|
jpaProperties.setShowSql(false);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
HibernateProperties hibernateProperties = new HibernateProperties();
|
HibernateProperties hibernateProperties = new HibernateProperties();
|
||||||
hibernateProperties.setDdlAuto("update");
|
hibernateProperties.setDdlAuto("update");
|
||||||
|
|
||||||
@ -80,11 +70,11 @@ public class RemoteJpaConfig {
|
|||||||
@Primary
|
@Primary
|
||||||
@Bean(name = "remoteEntityManagerFactory")
|
@Bean(name = "remoteEntityManagerFactory")
|
||||||
public LocalContainerEntityManagerFactoryBean initRemoteEntityManagerFactory(EntityManagerFactoryBuilder builder) {
|
public LocalContainerEntityManagerFactoryBean initRemoteEntityManagerFactory(EntityManagerFactoryBuilder builder) {
|
||||||
Map< String,Object > datasourceProperties = genDatasourceProperties();
|
Map<String, Object> datasourceProperties = genDatasourceProperties();
|
||||||
datasourceProperties.putAll( properties.getRemote().getHibernate().getProperties() );
|
datasourceProperties.putAll(properties.getRemote().getHibernate().getProperties());
|
||||||
return builder
|
return builder
|
||||||
.dataSource(omsRemoteDatasource)
|
.dataSource(omsRemoteDatasource)
|
||||||
.properties( datasourceProperties )
|
.properties(datasourceProperties)
|
||||||
.packages(CORE_PACKAGES)
|
.packages(CORE_PACKAGES)
|
||||||
.persistenceUnit("remotePersistenceUnit")
|
.persistenceUnit("remotePersistenceUnit")
|
||||||
.build();
|
.build();
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package tech.powerjob.server.persistence.config.dialect;
|
||||||
|
|
||||||
|
import org.hibernate.dialect.PostgreSQL10Dialect;
|
||||||
|
import org.hibernate.type.descriptor.sql.LongVarcharTypeDescriptor;
|
||||||
|
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
|
||||||
|
|
||||||
|
import java.sql.Types;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kung Yao
|
||||||
|
* @author Echo009
|
||||||
|
* 2021/3/24 下午 04:23
|
||||||
|
* 1074_King
|
||||||
|
*/
|
||||||
|
public class PowerJobPGDialect extends PostgreSQL10Dialect {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用 {@link Types#LONGVARCHAR} 覆盖 {@link Types#CLOB} 类型
|
||||||
|
*
|
||||||
|
* 注意,如果在 PG 库创建表时使用的列类型为 oid ,那么这样会导致没法正确读取数据
|
||||||
|
* 在 PowerJob 中能这样用是因为 PowerJob 的所有实体类中被 @Lob 注解标记的列对应数据库中的字段类型都是 text
|
||||||
|
* 另外还需要注意数据库版本,如果是 10.x 以前的,需自行提供一个合适的 Dialect 类(选择合适的版本继承)
|
||||||
|
*
|
||||||
|
* 更多内容请关注该 issues:https://github.com/PowerJob/PowerJob/issues/153
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) {
|
||||||
|
return Types.CLOB == sqlCode ? LongVarcharTypeDescriptor.INSTANCE : null;
|
||||||
|
}
|
||||||
|
}
|
@ -8,8 +8,10 @@ spring.datasource.core.username=root
|
|||||||
spring.datasource.core.password=No1Bug2Please3!
|
spring.datasource.core.password=No1Bug2Please3!
|
||||||
spring.datasource.core.hikari.maximum-pool-size=20
|
spring.datasource.core.hikari.maximum-pool-size=20
|
||||||
spring.datasource.core.hikari.minimum-idle=5
|
spring.datasource.core.hikari.minimum-idle=5
|
||||||
## The SQL dialect makes Hibernate generate better SQL for the chosen database
|
|
||||||
spring.datasource.remote.hibernate.properties.hibernate.dialect=tech.powerjob.server.persistence.config.PowerJobPGDialect
|
####### Fix issue 153 for PostgreSQL users, see https://github.com/PowerJob/PowerJob/issues/153#issuecomment-812771783 for more information #######
|
||||||
|
####### Notice, the configuration below only supports PostgreSQL 10.x and later #######
|
||||||
|
# spring.datasource.remote.hibernate.properties.hibernate.dialect=tech.powerjob.server.persistence.config.dialect.PowerJobPGDialect
|
||||||
|
|
||||||
####### MongoDB properties(Non-core configuration properties) #######
|
####### MongoDB properties(Non-core configuration properties) #######
|
||||||
####### configure oms.mongodb.enable=false to disable mongodb #######
|
####### configure oms.mongodb.enable=false to disable mongodb #######
|
||||||
|
Loading…
x
Reference in New Issue
Block a user