diff --git a/jetlinks-components/configure-component/pom.xml b/jetlinks-components/configure-component/pom.xml new file mode 100644 index 00000000..d1ce0f6d --- /dev/null +++ b/jetlinks-components/configure-component/pom.xml @@ -0,0 +1,40 @@ + + + + jetlinks-community + org.jetlinks.community + 1.11.0-SNAPSHOT + + 4.0.0 + + configure-component + + + 8 + 8 + + + + + + org.springframework.boot + spring-boot-autoconfigure + + + + org.springframework.data + spring-data-r2dbc + true + + + + io.r2dbc + r2dbc-pool + true + + + + + \ No newline at end of file diff --git a/jetlinks-components/configure-component/src/main/java/org/jetlinks/community/configure/r2dbc/R2dbcPoolConfiguration.java b/jetlinks-components/configure-component/src/main/java/org/jetlinks/community/configure/r2dbc/R2dbcPoolConfiguration.java new file mode 100644 index 00000000..fc2dfd89 --- /dev/null +++ b/jetlinks-components/configure-component/src/main/java/org/jetlinks/community/configure/r2dbc/R2dbcPoolConfiguration.java @@ -0,0 +1,64 @@ +package org.jetlinks.community.configure.r2dbc; + +import io.r2dbc.pool.ConnectionPool; +import io.r2dbc.pool.ConnectionPoolConfiguration; +import io.r2dbc.spi.ConnectionFactory; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.r2dbc.*; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.context.properties.PropertyMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.core.io.ResourceLoader; +import org.springframework.util.StringUtils; + +@Configuration(proxyBeanMethods = false) +@ConditionalOnClass(ConnectionPool.class) +@ConditionalOnMissingBean(ConnectionFactory.class) +@EnableConfigurationProperties({ + R2dbcPoolProperties.class, + R2dbcProperties.class +}) +@AutoConfigureBefore(R2dbcAutoConfiguration.class) +public class R2dbcPoolConfiguration { + + @Bean(destroyMethod = "dispose") + @Primary + ConnectionPool connectionFactory(R2dbcProperties properties, + R2dbcPoolProperties poolProperties, + ResourceLoader resourceLoader, + ObjectProvider customizers) { + ConnectionFactory connectionFactory = ConnectionFactoryBuilder + .of(properties, () -> EmbeddedDatabaseConnection.get(resourceLoader.getClassLoader())) + .configure((options) -> { + for (ConnectionFactoryOptionsBuilderCustomizer optionsCustomizer : customizers) { + optionsCustomizer.customize(options); + } + }) + .build(); + R2dbcProperties.Pool pool = properties.getPool(); + + ConnectionPoolConfiguration.Builder builder = ConnectionPoolConfiguration.builder(connectionFactory); + PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); + map.from(pool.getMaxIdleTime()).to(builder::maxIdleTime); + map.from(poolProperties.getMaxLifeTime()).to(builder::maxLifeTime); + map.from(poolProperties.getMaxAcquireTime()).to(builder::maxAcquireTime); + map.from(poolProperties.getMaxCreateConnectionTime()).to(builder::maxCreateConnectionTime); + map.from(pool.getInitialSize()).to(builder::initialSize); + map.from(pool.getMaxSize()).to(builder::maxSize); + map.from(pool.getValidationQuery()).whenHasText().to(builder::validationQuery); + map.from(poolProperties.getValidationDepth()).to(builder::validationDepth); + map.from(poolProperties.getAcquireRetry()).to(builder::acquireRetry); + + if (StringUtils.hasText(pool.getValidationQuery())) { + builder.validationQuery(pool.getValidationQuery()); + } + + return new ConnectionPool(builder.build()); + } + +} diff --git a/jetlinks-components/configure-component/src/main/java/org/jetlinks/community/configure/r2dbc/R2dbcPoolProperties.java b/jetlinks-components/configure-component/src/main/java/org/jetlinks/community/configure/r2dbc/R2dbcPoolProperties.java new file mode 100644 index 00000000..16284753 --- /dev/null +++ b/jetlinks-components/configure-component/src/main/java/org/jetlinks/community/configure/r2dbc/R2dbcPoolProperties.java @@ -0,0 +1,43 @@ +package org.jetlinks.community.configure.r2dbc; + +import io.r2dbc.spi.ValidationDepth; +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.time.Duration; + +@Getter +@Setter +@ConfigurationProperties(prefix = "spring.r2dbc.pool") +public class R2dbcPoolProperties { + + /** + * Maximum lifetime of a connection in the pool. By default, connections have an + * infinite lifetime. + */ + private Duration maxLifeTime = Duration.ofMinutes(10); + + /** + * Maximum time to acquire a connection from the pool. By default, wait + * indefinitely. + */ + private Duration maxAcquireTime; + + private int acquireRetry = 3; + /** + * Maximum time to wait to create a new connection. By default, wait indefinitely. + */ + private Duration maxCreateConnectionTime; + + /** + * Validation query. + */ + private String validationQuery; + + /** + * Validation depth. + */ + private ValidationDepth validationDepth = ValidationDepth.LOCAL; + +} diff --git a/jetlinks-components/configure-component/src/main/resources/META-INF/spring.factories b/jetlinks-components/configure-component/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..3863734d --- /dev/null +++ b/jetlinks-components/configure-component/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.jetlinks.community.configure.r2dbc.R2dbcPoolConfiguration \ No newline at end of file diff --git a/jetlinks-components/pom.xml b/jetlinks-components/pom.xml index 067d19dd..9682e82b 100644 --- a/jetlinks-components/pom.xml +++ b/jetlinks-components/pom.xml @@ -22,6 +22,7 @@ notify-component logging-component rule-engine-component + configure-component jetlinks-components diff --git a/jetlinks-standalone/pom.xml b/jetlinks-standalone/pom.xml index c36c4081..07b3dfd6 100644 --- a/jetlinks-standalone/pom.xml +++ b/jetlinks-standalone/pom.xml @@ -246,6 +246,11 @@ 2.0.8 + + org.jetlinks.community + configure-component + ${project.version} + diff --git a/jetlinks-standalone/src/main/resources/application.yml b/jetlinks-standalone/src/main/resources/application.yml index be7244be..e25761f2 100644 --- a/jetlinks-standalone/src/main/resources/application.yml +++ b/jetlinks-standalone/src/main/resources/application.yml @@ -31,6 +31,8 @@ spring: pool: max-size: 32 max-idle-time: 2m # 值不能大于mysql server的wait_timeout配置 + max-life-time: 10m + acquire-retry: 3 reactor: debug-agent: enabled: false @@ -38,10 +40,10 @@ spring: elasticsearch: client: reactive: - endpoints: ${elasticsearch.client.host}:${elasticsearch.client.port} + endpoints: localhost:9200 max-in-memory-size: 100MB - socket-timeout: ${elasticsearch.client.socket-timeout} - connection-timeout: ${elasticsearch.client.socket-timeout} + socket-timeout: 5000 + connection-timeout: 8000 easyorm: default-schema: public # 数据库默认的schema dialect: postgres #数据库方言 @@ -51,13 +53,6 @@ elasticsearch: data-path: ./data/elasticsearch port: 9200 host: 0.0.0.0 - client: - host: localhost - port: 9200 - max-conn-total: 128 - connect-timeout: 5000 - socket-timeout: 5000 - connection-request-timeout: 8000 index: default-strategy: time-by-month #默认es的索引按月进行分表, direct则为直接操作索引. settings: