From 9203551eaa44bc5cc102f6cfd8138b6e016d2f68 Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sat, 7 Sep 2024 20:49:08 +0800 Subject: [PATCH 01/13] compatible with file.conf and registry.conf (#6811) --- .../org/apache/seata/common/Constants.java | 4 + .../seata-spring-autoconfigure-core/pom.xml | 5 ++ .../SeataCoreAutoConfiguration.java | 8 ++ .../SeataFileConfigurationProvider.java | 74 +++++++++++++++++++ .../SpringBootConfigurationProvider.java | 4 + .../SeataCoreAutoConfigurationTest.java | 40 ++++++++++ .../resources/application-test.properties | 2 +- .../src/test/resources/file.conf | 67 +++++++++++++++++ .../src/test/resources/registry.conf | 30 ++++++++ 9 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java create mode 100644 seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java create mode 100644 seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/file.conf create mode 100644 seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/registry.conf diff --git a/common/src/main/java/org/apache/seata/common/Constants.java b/common/src/main/java/org/apache/seata/common/Constants.java index 43da1827e05..c157b0c6f07 100644 --- a/common/src/main/java/org/apache/seata/common/Constants.java +++ b/common/src/main/java/org/apache/seata/common/Constants.java @@ -127,6 +127,10 @@ public interface Constants { * The constant BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER */ String BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER = "springApplicationContextProvider"; + /** + * The constant BEAN_NAME_SEATA_FILE_CONFIGURATION_PROVIDER + */ + String BEAN_NAME_SEATA_FILE_CONFIGURATION_PROVIDER = "seataFileConfigurationProvider"; /** * The constant BEAN_NAME_SPRING_FENCE_CONFIG */ diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/pom.xml b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/pom.xml index a3767ecb0ab..1fb5096741a 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/pom.xml +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/pom.xml @@ -38,5 +38,10 @@ ${project.version} test + + org.springframework.boot + spring-boot-starter-test + test + \ No newline at end of file diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfiguration.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfiguration.java index ce8a1e33a35..c5082805f30 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfiguration.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfiguration.java @@ -16,6 +16,7 @@ */ package org.apache.seata.spring.boot.autoconfigure; +import org.apache.seata.spring.boot.autoconfigure.provider.SeataFileConfigurationProvider; import org.apache.seata.spring.boot.autoconfigure.provider.SpringApplicationContextProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -23,6 +24,7 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import static org.apache.seata.common.Constants.BEAN_NAME_SEATA_FILE_CONFIGURATION_PROVIDER; import static org.apache.seata.common.Constants.BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER; import static org.apache.seata.spring.boot.autoconfigure.StarterConstants.SEATA_PREFIX; @@ -37,4 +39,10 @@ public class SeataCoreAutoConfiguration { public SpringApplicationContextProvider springApplicationContextProvider() { return new SpringApplicationContextProvider(); } + + @Bean(BEAN_NAME_SEATA_FILE_CONFIGURATION_PROVIDER) + @ConditionalOnMissingBean(name = {BEAN_NAME_SEATA_FILE_CONFIGURATION_PROVIDER}) + public SeataFileConfigurationProvider seataFileConfigurationProvider() { + return new SeataFileConfigurationProvider(); + } } diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java new file mode 100644 index 00000000000..2e9987e07c6 --- /dev/null +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java @@ -0,0 +1,74 @@ +package org.apache.seata.spring.boot.autoconfigure.provider; + +import org.apache.commons.lang.StringUtils; +import org.apache.seata.common.util.CollectionUtils; +import org.apache.seata.config.FileConfiguration; +import org.apache.seata.config.ConfigurationFactory; +import org.apache.seata.config.file.FileConfig; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.PropertiesPropertySource; + +import java.util.*; + +import static org.apache.seata.common.ConfigurationKeys.FILE_ROOT_PREFIX_CONFIG; +import static org.apache.seata.common.ConfigurationKeys.*; + +public class SeataFileConfigurationProvider implements ApplicationContextAware, ApplicationListener { + + private ApplicationContext applicationContext; + + // Prefix list for filtering configuration keys + List prefixList = Arrays.asList(FILE_ROOT_PREFIX_CONFIG, FILE_ROOT_PREFIX_REGISTRY, SERVER_PREFIX, + STORE_PREFIX, METRICS_PREFIX, TRANSPORT_PREFIX); + + @Override + public void setApplicationContext(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + @Override + public void onApplicationEvent(ContextRefreshedEvent event) { + // Only process if the event is for the current application context + if (event.getApplicationContext().equals(this.applicationContext)) { + ConfigurableEnvironment environment = (ConfigurableEnvironment) applicationContext.getEnvironment(); + loadAndAddConfigurations(environment); + } + } + + /** + * Loads configurations from file.conf and registry.conf and adds them to the Spring environment. + * + * @param environment the Spring environment + */ + private void loadAndAddConfigurations(ConfigurableEnvironment environment) { + // Get configurations from file.conf and registry.conf + FileConfiguration configuration = ConfigurationFactory.getOriginFileInstanceRegistry(); + FileConfig fileConfig = configuration.getFileConfig(); + Map configs = fileConfig.getAllConfig(); + + if (CollectionUtils.isNotEmpty(configs)) { + // Optionally merge other configurations + Optional originFileInstance = ConfigurationFactory.getOriginFileInstance(); + originFileInstance.ifPresent(fileConfiguration -> + configs.putAll(fileConfiguration.getFileConfig().getAllConfig()) + ); + + // Convert and filter configurations based on prefix + Properties properties = new Properties(); + configs.forEach((k, v) -> { + if (v instanceof String && StringUtils.isNotEmpty((String) v)) { + if (prefixList.stream().anyMatch(k::startsWith)) { + properties.put(SEATA_FILE_PREFIX_ROOT_CONFIG + k, v); + } + } + }); + + // Add the properties to the environment with the lowest priority + environment.getPropertySources().addLast(new PropertiesPropertySource("seataFileConfig", properties)); + } + } +} \ No newline at end of file diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SpringBootConfigurationProvider.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SpringBootConfigurationProvider.java index c390de9ebe8..45989b0ae01 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SpringBootConfigurationProvider.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SpringBootConfigurationProvider.java @@ -68,6 +68,10 @@ public Configuration provide(Configuration originalConfiguration) { // 1. Get config value from the system property result = originalConfiguration.getConfigFromSys(rawDataId); + if (result == null) { + result = originalConfiguration.getConfig(rawDataId); + } + if (result == null) { String dataId = convertDataId(rawDataId); diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java new file mode 100644 index 00000000000..61020ad86f3 --- /dev/null +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java @@ -0,0 +1,40 @@ +package org.apache.seata.spring.boot.autoconfigure; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = SeataCoreAutoConfiguration.class) +@TestPropertySource(locations = "classpath:application-test.properties") +public class SeataCoreAutoConfigurationTest { + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + private Environment environment; + + @Test + public void testSeataPropertiesLoaded() { + // default file.conf + String txServiceGroup = environment.getProperty("seata.store.db.url"); + assertEquals("jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true&configType=file", txServiceGroup, "The transaction service group should be correctly loaded from configuration"); + + + // overridden by application-test.properties + String registryType = environment.getProperty("seata.config.type"); + assertEquals("file", registryType, "The config type should be file"); + + // overridden by application-test.properties + String seataNamespaces = environment.getProperty("seata.config.nacos.namespace"); + assertEquals("seata-test-application.yml", seataNamespaces, "The config type should be file"); + } +} \ No newline at end of file diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/application-test.properties b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/application-test.properties index 8467d6cc7b9..dc1f0851cfc 100755 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/application-test.properties +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/application-test.properties @@ -33,7 +33,7 @@ seata.config.apollo.apollo-config-service=fff seata.config.etcd3.server-addr=aaa seata.config.etcd3.key=bbb -seata.config.nacos.namespace=ddd +seata.config.nacos.namespace=seata-test-application.yml seata.config.nacos.server-addr=aaa seata.config.nacos.group=ccc seata.config.nacos.username=eee diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/file.conf b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/file.conf new file mode 100644 index 00000000000..422c6dd5836 --- /dev/null +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/file.conf @@ -0,0 +1,67 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#reduce delay for test +## transaction log store, only used in seata-server +store { + ## store mode: file、db + mode = "file" + + ## file store property + file { + ## store location dir + dir = "sessionStore" + } + + ## database store property + db { + ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc. + datasource = "dbcp" + ## mysql/oracle/h2/oceanbase etc. + dbType = "mysql" + driverClassName = "com.mysql.jdbc.Driver" + ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param + url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true" + user = "mysql" + password = "mysql" + } +} +server { + recovery { + #schedule committing retry period in milliseconds + committingRetryPeriod = 100 + #schedule asyn committing retry period in milliseconds + asynCommittingRetryPeriod = 100 + #schedule rollbacking retry period in milliseconds + rollbackingRetryPeriod = 100 + #schedule timeout retry period in milliseconds + timeoutRetryPeriod = 100 + } + undo { + logSaveDays = 2 + #schedule delete expired undo_log in milliseconds + logDeletePeriod = 86400000 + } +} +## metrics settings +metrics { + enabled = true + registryType = "compact" + # multi exporters use comma divided + exporterList = "prometheus" + exporterPrometheusPort = 9898 +} \ No newline at end of file diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/registry.conf b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/registry.conf new file mode 100644 index 00000000000..ceb312b5e7e --- /dev/null +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/registry.conf @@ -0,0 +1,30 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +config { + type = "file" + + file { + name = "file.conf" + } + nacos { + application = "seata-server" + serverAddr = "127.0.0.1:8848" + namespace = "seata-test" + cluster = "default" + } +} \ No newline at end of file From 57aee15a49b63dfec3299de1f9e6bf12c7cd8268 Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sat, 7 Sep 2024 21:07:22 +0800 Subject: [PATCH 02/13] add change --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 1 + .../boot/autoconfigure/SeataCoreAutoConfigurationTest.java | 5 ----- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 9093aed1db7..61e29097c65 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -84,6 +84,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6794](https://github.com/apache/incubator-seata/pull/6794)] optimize NacosMockTest UT case - [[#6806](https://github.com/apache/incubator-seata/pull/6806)] optimize `tableMeta` cache scheduled refresh issue - [[#6808](https://github.com/apache/incubator-seata/pull/6808)] change version to 2.2.0-SNAPSHOT +- [[#6828](https://github.com/apache/incubator-seata/pull/6828)] spring boot compatible with file.conf and registry.conf ### refactor: diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 850f1d7ea74..b50f817b7c5 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -83,6 +83,7 @@ - [[#6793](https://github.com/apache/incubator-seata/pull/6795)] 独立server的meta信息初始化逻辑 - [[#6806](https://github.com/apache/incubator-seata/pull/6806)] 优化`tableMeta`缓存定时刷新问题 - [[#6808](https://github.com/apache/incubator-seata/pull/6808)] 修改版本号为2.2.0-SNAPSHOT +- [[#6828](https://github.com/apache/incubator-seata/pull/6828)] seata-spring-boot-starter兼容file.conf和registry.conf ### refactor: diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java index 61020ad86f3..13c008e2670 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java @@ -4,7 +4,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -16,9 +15,6 @@ @TestPropertySource(locations = "classpath:application-test.properties") public class SeataCoreAutoConfigurationTest { - @Autowired - private ApplicationContext applicationContext; - @Autowired private Environment environment; @@ -28,7 +24,6 @@ public void testSeataPropertiesLoaded() { String txServiceGroup = environment.getProperty("seata.store.db.url"); assertEquals("jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true&configType=file", txServiceGroup, "The transaction service group should be correctly loaded from configuration"); - // overridden by application-test.properties String registryType = environment.getProperty("seata.config.type"); assertEquals("file", registryType, "The config type should be file"); From da81deaaa832ef4ef8e8ae9068ba7d1029233992 Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sat, 7 Sep 2024 21:11:53 +0800 Subject: [PATCH 03/13] handle references --- .../provider/SeataFileConfigurationProvider.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java index 2e9987e07c6..c2c5754d13a 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java @@ -1,5 +1,11 @@ package org.apache.seata.spring.boot.autoconfigure.provider; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Properties; + import org.apache.commons.lang.StringUtils; import org.apache.seata.common.util.CollectionUtils; import org.apache.seata.config.FileConfiguration; @@ -12,10 +18,13 @@ import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertiesPropertySource; -import java.util.*; - import static org.apache.seata.common.ConfigurationKeys.FILE_ROOT_PREFIX_CONFIG; -import static org.apache.seata.common.ConfigurationKeys.*; +import static org.apache.seata.common.ConfigurationKeys.FILE_ROOT_PREFIX_REGISTRY; +import static org.apache.seata.common.ConfigurationKeys.SERVER_PREFIX; +import static org.apache.seata.common.ConfigurationKeys.STORE_PREFIX; +import static org.apache.seata.common.ConfigurationKeys.METRICS_PREFIX; +import static org.apache.seata.common.ConfigurationKeys.TRANSPORT_PREFIX; +import static org.apache.seata.common.ConfigurationKeys.SEATA_FILE_PREFIX_ROOT_CONFIG; public class SeataFileConfigurationProvider implements ApplicationContextAware, ApplicationListener { From 99e50a2a474936ce079023dbd6f37e5a2d6be284 Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sun, 8 Sep 2024 19:10:40 +0800 Subject: [PATCH 04/13] use SeataPropertiesLoader to solve this problem --- .../org/apache/seata/common/Constants.java | 4 - .../seata-spring-autoconfigure-core/pom.xml | 6 ++ .../SeataCoreAutoConfiguration.java | 8 -- .../loader}/SeataPropertiesLoader.java | 26 +++++- .../SeataFileConfigurationProvider.java | 83 ------------------- .../main/resources/META-INF/spring.factories | 3 + .../SeataCoreAutoConfigurationTest.java | 2 +- .../main/resources/META-INF/spring.factories | 4 +- 8 files changed, 33 insertions(+), 103 deletions(-) rename {server/src/main/java/org/apache/seata/server/spring/listener => seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader}/SeataPropertiesLoader.java (74%) delete mode 100644 seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java diff --git a/common/src/main/java/org/apache/seata/common/Constants.java b/common/src/main/java/org/apache/seata/common/Constants.java index c157b0c6f07..43da1827e05 100644 --- a/common/src/main/java/org/apache/seata/common/Constants.java +++ b/common/src/main/java/org/apache/seata/common/Constants.java @@ -127,10 +127,6 @@ public interface Constants { * The constant BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER */ String BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER = "springApplicationContextProvider"; - /** - * The constant BEAN_NAME_SEATA_FILE_CONFIGURATION_PROVIDER - */ - String BEAN_NAME_SEATA_FILE_CONFIGURATION_PROVIDER = "seataFileConfigurationProvider"; /** * The constant BEAN_NAME_SPRING_FENCE_CONFIG */ diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/pom.xml b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/pom.xml index 1fb5096741a..9c24e75a961 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/pom.xml +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/pom.xml @@ -42,6 +42,12 @@ org.springframework.boot spring-boot-starter-test test + + + ch.qos.logback + logback-classic + + \ No newline at end of file diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfiguration.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfiguration.java index c5082805f30..ce8a1e33a35 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfiguration.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfiguration.java @@ -16,7 +16,6 @@ */ package org.apache.seata.spring.boot.autoconfigure; -import org.apache.seata.spring.boot.autoconfigure.provider.SeataFileConfigurationProvider; import org.apache.seata.spring.boot.autoconfigure.provider.SpringApplicationContextProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -24,7 +23,6 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; -import static org.apache.seata.common.Constants.BEAN_NAME_SEATA_FILE_CONFIGURATION_PROVIDER; import static org.apache.seata.common.Constants.BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER; import static org.apache.seata.spring.boot.autoconfigure.StarterConstants.SEATA_PREFIX; @@ -39,10 +37,4 @@ public class SeataCoreAutoConfiguration { public SpringApplicationContextProvider springApplicationContextProvider() { return new SpringApplicationContextProvider(); } - - @Bean(BEAN_NAME_SEATA_FILE_CONFIGURATION_PROVIDER) - @ConditionalOnMissingBean(name = {BEAN_NAME_SEATA_FILE_CONFIGURATION_PROVIDER}) - public SeataFileConfigurationProvider seataFileConfigurationProvider() { - return new SeataFileConfigurationProvider(); - } } diff --git a/server/src/main/java/org/apache/seata/server/spring/listener/SeataPropertiesLoader.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java similarity index 74% rename from server/src/main/java/org/apache/seata/server/spring/listener/SeataPropertiesLoader.java rename to seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java index 974416b26c5..9b046d6cb90 100644 --- a/server/src/main/java/org/apache/seata/server/spring/listener/SeataPropertiesLoader.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java @@ -14,14 +14,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.seata.server.spring.listener; +package org.apache.seata.spring.boot.autoconfigure.loader; import org.apache.seata.common.util.CollectionUtils; import org.apache.seata.common.util.StringUtils; import org.apache.seata.config.ConfigurationFactory; import org.apache.seata.config.FileConfiguration; import org.apache.seata.config.file.FileConfig; -import org.apache.seata.server.store.StoreConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.Ordered; @@ -29,6 +30,8 @@ import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertiesPropertySource; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -74,8 +77,23 @@ public void initialize(ConfigurableApplicationContext applicationContext) { environment.getPropertySources().addLast(new PropertiesPropertySource("seataOldConfig", properties)); } // Load by priority - System.setProperty("sessionMode", StoreConfig.getSessionMode().getName()); - System.setProperty("lockMode", StoreConfig.getLockMode().getName()); + loadSessionAndLockModes(); } + public void loadSessionAndLockModes() { + try { + Class storeConfigClass = Class.forName("org.apache.seata.server.store.StoreConfig"); + String sessionMode = invokeStaticMethod(storeConfigClass, "getSessionMode").orElse("defaultSessionMode"); + String lockMode = invokeStaticMethod(storeConfigClass, "getLockMode").orElse("defaultLockMode"); + System.setProperty("sessionMode", sessionMode); + System.setProperty("lockMode", lockMode); + } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + // The exception is not printed because it is an expected behavior and does not affect the normal operation of the program. + } + } + + private Optional invokeStaticMethod(Class clazz, String methodName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method method = clazz.getMethod(methodName); + return Optional.ofNullable((String) method.invoke(null)); + } } diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java deleted file mode 100644 index c2c5754d13a..00000000000 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SeataFileConfigurationProvider.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.apache.seata.spring.boot.autoconfigure.provider; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Properties; - -import org.apache.commons.lang.StringUtils; -import org.apache.seata.common.util.CollectionUtils; -import org.apache.seata.config.FileConfiguration; -import org.apache.seata.config.ConfigurationFactory; -import org.apache.seata.config.file.FileConfig; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.context.ApplicationListener; -import org.springframework.context.event.ContextRefreshedEvent; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.PropertiesPropertySource; - -import static org.apache.seata.common.ConfigurationKeys.FILE_ROOT_PREFIX_CONFIG; -import static org.apache.seata.common.ConfigurationKeys.FILE_ROOT_PREFIX_REGISTRY; -import static org.apache.seata.common.ConfigurationKeys.SERVER_PREFIX; -import static org.apache.seata.common.ConfigurationKeys.STORE_PREFIX; -import static org.apache.seata.common.ConfigurationKeys.METRICS_PREFIX; -import static org.apache.seata.common.ConfigurationKeys.TRANSPORT_PREFIX; -import static org.apache.seata.common.ConfigurationKeys.SEATA_FILE_PREFIX_ROOT_CONFIG; - -public class SeataFileConfigurationProvider implements ApplicationContextAware, ApplicationListener { - - private ApplicationContext applicationContext; - - // Prefix list for filtering configuration keys - List prefixList = Arrays.asList(FILE_ROOT_PREFIX_CONFIG, FILE_ROOT_PREFIX_REGISTRY, SERVER_PREFIX, - STORE_PREFIX, METRICS_PREFIX, TRANSPORT_PREFIX); - - @Override - public void setApplicationContext(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } - - @Override - public void onApplicationEvent(ContextRefreshedEvent event) { - // Only process if the event is for the current application context - if (event.getApplicationContext().equals(this.applicationContext)) { - ConfigurableEnvironment environment = (ConfigurableEnvironment) applicationContext.getEnvironment(); - loadAndAddConfigurations(environment); - } - } - - /** - * Loads configurations from file.conf and registry.conf and adds them to the Spring environment. - * - * @param environment the Spring environment - */ - private void loadAndAddConfigurations(ConfigurableEnvironment environment) { - // Get configurations from file.conf and registry.conf - FileConfiguration configuration = ConfigurationFactory.getOriginFileInstanceRegistry(); - FileConfig fileConfig = configuration.getFileConfig(); - Map configs = fileConfig.getAllConfig(); - - if (CollectionUtils.isNotEmpty(configs)) { - // Optionally merge other configurations - Optional originFileInstance = ConfigurationFactory.getOriginFileInstance(); - originFileInstance.ifPresent(fileConfiguration -> - configs.putAll(fileConfiguration.getFileConfig().getAllConfig()) - ); - - // Convert and filter configurations based on prefix - Properties properties = new Properties(); - configs.forEach((k, v) -> { - if (v instanceof String && StringUtils.isNotEmpty((String) v)) { - if (prefixList.stream().anyMatch(k::startsWith)) { - properties.put(SEATA_FILE_PREFIX_ROOT_CONFIG + k, v); - } - } - }); - - // Add the properties to the environment with the lowest priority - environment.getPropertySources().addLast(new PropertiesPropertySource("seataFileConfig", properties)); - } - } -} \ No newline at end of file diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/resources/META-INF/spring.factories b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/resources/META-INF/spring.factories index df445bb6973..62068fab123 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/resources/META-INF/spring.factories +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/resources/META-INF/spring.factories @@ -21,3 +21,6 @@ org.apache.seata.spring.boot.autoconfigure.SeataCoreAutoConfiguration # Environment Post Processors org.springframework.boot.env.EnvironmentPostProcessor=\ org.apache.seata.spring.boot.autoconfigure.SeataCoreEnvironmentPostProcessor + +org.springframework.context.ApplicationContextInitializer=\ +org.apache.seata.spring.boot.autoconfigure.loader.SeataPropertiesLoader \ No newline at end of file diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java index 13c008e2670..e44bb500ab6 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java @@ -22,7 +22,7 @@ public class SeataCoreAutoConfigurationTest { public void testSeataPropertiesLoaded() { // default file.conf String txServiceGroup = environment.getProperty("seata.store.db.url"); - assertEquals("jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true&configType=file", txServiceGroup, "The transaction service group should be correctly loaded from configuration"); + assertEquals("jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true", txServiceGroup, "The transaction service group should be correctly loaded from configuration"); // overridden by application-test.properties String registryType = environment.getProperty("seata.config.type"); diff --git a/server/src/main/resources/META-INF/spring.factories b/server/src/main/resources/META-INF/spring.factories index 4b97deb2101..649fa8a356c 100644 --- a/server/src/main/resources/META-INF/spring.factories +++ b/server/src/main/resources/META-INF/spring.factories @@ -15,6 +15,4 @@ # limitations under the License. # org.springframework.context.ApplicationListener=\ -org.apache.seata.server.spring.listener.ServerApplicationListener -org.springframework.context.ApplicationContextInitializer=\ -org.apache.seata.server.spring.listener.SeataPropertiesLoader \ No newline at end of file +org.apache.seata.server.spring.listener.ServerApplicationListener \ No newline at end of file From 3515ea970440e0f0cc27397980235a4c0af16397 Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sun, 8 Sep 2024 19:17:02 +0800 Subject: [PATCH 05/13] license header --- .../SeataCoreAutoConfigurationTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java index e44bb500ab6..6eafab93231 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.seata.spring.boot.autoconfigure; import org.junit.jupiter.api.Test; From 8837d11c5682e30cc08c138010b2162df41a647e Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sun, 8 Sep 2024 19:34:57 +0800 Subject: [PATCH 06/13] fixed Refactored loadSessionAndLockModes to use ifPresent instead of orElse for setting system properties. --- .../loader/SeataPropertiesLoader.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java index 9b046d6cb90..c9988dbee65 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java @@ -83,17 +83,23 @@ public void initialize(ConfigurableApplicationContext applicationContext) { public void loadSessionAndLockModes() { try { Class storeConfigClass = Class.forName("org.apache.seata.server.store.StoreConfig"); - String sessionMode = invokeStaticMethod(storeConfigClass, "getSessionMode").orElse("defaultSessionMode"); - String lockMode = invokeStaticMethod(storeConfigClass, "getLockMode").orElse("defaultLockMode"); - System.setProperty("sessionMode", sessionMode); - System.setProperty("lockMode", lockMode); + Optional sessionMode = invokeEnumMethod(storeConfigClass, "getSessionMode", "getName"); + Optional lockMode = invokeEnumMethod(storeConfigClass, "getLockMode", "getName"); + sessionMode.ifPresent(value -> System.setProperty("sessionMode", value)); + lockMode.ifPresent(value -> System.setProperty("lockMode", value)); } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { // The exception is not printed because it is an expected behavior and does not affect the normal operation of the program. } } - private Optional invokeStaticMethod(Class clazz, String methodName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Method method = clazz.getMethod(methodName); - return Optional.ofNullable((String) method.invoke(null)); + private Optional invokeEnumMethod(Class clazz, String enumMethodName, String getterMethodName) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method enumMethod = clazz.getMethod(enumMethodName); + Object enumValue = enumMethod.invoke(null); + if (enumValue != null) { + Method getterMethod = enumValue.getClass().getMethod(getterMethodName); + return Optional.ofNullable((String) getterMethod.invoke(enumValue)); + } + return Optional.empty(); } } From e1c6769d51aacc190ea2c2a131364cd8e0e7c7bf Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sun, 8 Sep 2024 19:40:44 +0800 Subject: [PATCH 07/13] improved test method and variable names for clarity in Seata configuration loading test. --- .../autoconfigure/SeataCoreAutoConfigurationTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java index 6eafab93231..4a16dd5cba8 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java @@ -37,15 +37,15 @@ public class SeataCoreAutoConfigurationTest { @Test public void testSeataPropertiesLoaded() { // default file.conf - String txServiceGroup = environment.getProperty("seata.store.db.url"); - assertEquals("jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true", txServiceGroup, "The transaction service group should be correctly loaded from configuration"); + String dbUrl = environment.getProperty("seata.store.db.url"); + assertEquals("jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true", dbUrl, "The DB URL should be correctly loaded from configuration"); // overridden by application-test.properties String registryType = environment.getProperty("seata.config.type"); - assertEquals("file", registryType, "The config type should be file"); + assertEquals("file", registryType, "The config type should be 'file'"); // overridden by application-test.properties String seataNamespaces = environment.getProperty("seata.config.nacos.namespace"); - assertEquals("seata-test-application.yml", seataNamespaces, "The config type should be file"); + assertEquals("seata-test-application.yml", seataNamespaces, "The Nacos namespace should be 'seata-test-application.yml'"); } } \ No newline at end of file From f92343e5b680d287e3f6125cf483d4a313347845 Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sun, 8 Sep 2024 19:56:03 +0800 Subject: [PATCH 08/13] remove unuse import --- .../spring/boot/autoconfigure/loader/SeataPropertiesLoader.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java index c9988dbee65..e0b487aefb1 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java @@ -21,8 +21,6 @@ import org.apache.seata.config.ConfigurationFactory; import org.apache.seata.config.FileConfiguration; import org.apache.seata.config.file.FileConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.Ordered; From 314078447db87e0cd099e09fdccd218a36664623 Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sun, 8 Sep 2024 20:55:24 +0800 Subject: [PATCH 09/13] Add unit tests for ConfigurationFactory to verify configuration loading from file.conf and registry.conf --- .../SeataCoreAutoConfigurationTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java index 4a16dd5cba8..52051848d01 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java @@ -16,6 +16,10 @@ */ package org.apache.seata.spring.boot.autoconfigure; +import org.apache.seata.config.Configuration; +import org.apache.seata.config.ConfigurationFactory; +import org.apache.seata.config.FileConfiguration; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; @@ -24,7 +28,10 @@ import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; +import java.util.Optional; + import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; @ExtendWith(SpringExtension.class) @SpringBootTest(classes = SeataCoreAutoConfiguration.class) @@ -34,6 +41,11 @@ public class SeataCoreAutoConfigurationTest { @Autowired private Environment environment; + @BeforeAll + public static void init() { + ConfigurationFactory.reload(); + } + @Test public void testSeataPropertiesLoaded() { // default file.conf @@ -48,4 +60,33 @@ public void testSeataPropertiesLoaded() { String seataNamespaces = environment.getProperty("seata.config.nacos.namespace"); assertEquals("seata-test-application.yml", seataNamespaces, "The Nacos namespace should be 'seata-test-application.yml'"); } + + @Test + public void testConfigFromFileUsingGetInstance() { + Configuration configFromFile = ConfigurationFactory.getInstance(); + String dbUrlFromFile = configFromFile.getConfig("store.db.url"); + assertEquals("jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true", dbUrlFromFile, "The DB URL should be correctly loaded from file.conf"); + String storeFileDirFromFile = configFromFile.getConfig("store.file.dir"); + assertEquals("sessionStore", storeFileDirFromFile, "The storeFileDir should be 'sessionStore' in file.conf"); + } + + @Test + public void testConfigFromFileUsingGetOriginFileInstance() { + Optional optionalConfigFromFile = ConfigurationFactory.getOriginFileInstance(); + assertTrue(optionalConfigFromFile.isPresent(), "The configuration from file.conf should be present"); + FileConfiguration configFromFile = optionalConfigFromFile.get(); + String dbUrlFromFile = configFromFile.getConfig("store.db.url"); + assertEquals("jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true", dbUrlFromFile, "The DB URL should be correctly loaded from file.conf"); + String storeFileDirFromFile = configFromFile.getConfig("store.file.dir"); + assertEquals("sessionStore", storeFileDirFromFile, "The storeFileDir should be 'sessionStore' in file.conf"); + } + + @Test + public void testConfigFromRegistryUsingGetOriginFileInstanceRegistry() { + Configuration configFromRegistry = ConfigurationFactory.getOriginFileInstanceRegistry(); + String registryTypeFromRegistry = configFromRegistry.getConfig("config.type"); + assertEquals("file", registryTypeFromRegistry, "The config type should be 'file' in registry.conf"); + String seataNamespaceFromRegistry = configFromRegistry.getConfig("config.nacos.namespace"); + assertEquals("seata-test", seataNamespaceFromRegistry, "The Nacos namespace should be 'seata-test' in registry.conf"); + } } \ No newline at end of file From 4e2d72f3ab47d6083dd928caaa827093249c6554 Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sun, 13 Oct 2024 15:59:18 +0800 Subject: [PATCH 10/13] Move SessionMode and LockMode to seata-common and apply StoreMode from seata-common --- .../apache/seata/common/store/LockMode.java | 53 ++++++++ .../seata/common/store/SessionMode.java | 53 ++++++++ .../loader/SeataPropertiesLoader.java | 1 + .../server/cluster/raft/RaftStateMachine.java | 10 +- .../coordinator/DefaultCoordinator.java | 7 +- .../server/lock/LockerManagerFactory.java | 5 +- .../seata/server/session/SessionHolder.java | 34 ++--- .../seata/server/store/StoreConfig.java | 118 +----------------- 8 files changed, 140 insertions(+), 141 deletions(-) create mode 100644 common/src/main/java/org/apache/seata/common/store/LockMode.java create mode 100644 common/src/main/java/org/apache/seata/common/store/SessionMode.java diff --git a/common/src/main/java/org/apache/seata/common/store/LockMode.java b/common/src/main/java/org/apache/seata/common/store/LockMode.java new file mode 100644 index 00000000000..8b60e73d9ef --- /dev/null +++ b/common/src/main/java/org/apache/seata/common/store/LockMode.java @@ -0,0 +1,53 @@ +package org.apache.seata.common.store; + +public enum LockMode { + /** + * The File store mode. + */ + FILE("file"), + /** + * The Db store mode. + */ + DB("db"), + /** + * The Redis store mode. + */ + REDIS("redis"), + /** + * raft store + */ + RAFT("raft"); + + private String name; + + LockMode(String name) { + this.name = name; + } + + public static LockMode get(String name) { + for (LockMode mode : LockMode.values()) { + if (mode.getName().equalsIgnoreCase(name)) { + return mode; + } + } + throw new IllegalArgumentException("unknown lock mode:" + name); + } + + /** + * whether contains value of store mode + * + * @param name the mode name + * @return the boolean + */ + public static boolean contains(String name) { + try { + return get(name) != null ? true : false; + } catch (IllegalArgumentException e) { + return false; + } + } + + public String getName() { + return name; + } +} \ No newline at end of file diff --git a/common/src/main/java/org/apache/seata/common/store/SessionMode.java b/common/src/main/java/org/apache/seata/common/store/SessionMode.java new file mode 100644 index 00000000000..5ec487f2a39 --- /dev/null +++ b/common/src/main/java/org/apache/seata/common/store/SessionMode.java @@ -0,0 +1,53 @@ +package org.apache.seata.common.store; + +public enum SessionMode { + /** + * The File store mode. + */ + FILE("file"), + /** + * The Db store mode. + */ + DB("db"), + /** + * The Redis store mode. + */ + REDIS("redis"), + /** + * raft store + */ + RAFT("raft"); + + private String name; + + SessionMode(String name) { + this.name = name; + } + + public static SessionMode get(String name) { + for (SessionMode mode : SessionMode.values()) { + if (mode.getName().equalsIgnoreCase(name)) { + return mode; + } + } + throw new IllegalArgumentException("unknown session mode:" + name); + } + + /** + * whether contains value of store mode + * + * @param name the mode name + * @return the boolean + */ + public static boolean contains(String name) { + try { + return get(name) != null ? true : false; + } catch (IllegalArgumentException e) { + return false; + } + } + + public String getName() { + return name; + } +} \ No newline at end of file diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java index e0b487aefb1..05931662eee 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/loader/SeataPropertiesLoader.java @@ -87,6 +87,7 @@ public void loadSessionAndLockModes() { lockMode.ifPresent(value -> System.setProperty("lockMode", value)); } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { // The exception is not printed because it is an expected behavior and does not affect the normal operation of the program. + // StoreConfig only exists on the server side } } diff --git a/server/src/main/java/org/apache/seata/server/cluster/raft/RaftStateMachine.java b/server/src/main/java/org/apache/seata/server/cluster/raft/RaftStateMachine.java index 9b49ba011ff..2b14b2a5cb9 100644 --- a/server/src/main/java/org/apache/seata/server/cluster/raft/RaftStateMachine.java +++ b/server/src/main/java/org/apache/seata/server/cluster/raft/RaftStateMachine.java @@ -49,6 +49,8 @@ import org.apache.seata.common.holder.ObjectHolder; import org.apache.seata.common.metadata.ClusterRole; import org.apache.seata.common.metadata.Node; +import org.apache.seata.common.store.SessionMode; +import org.apache.seata.common.store.StoreMode; import org.apache.seata.common.thread.NamedThreadFactory; import org.apache.seata.common.util.CollectionUtils; import org.apache.seata.common.util.StringUtils; @@ -140,7 +142,7 @@ public RaftStateMachine(String group) { return null; }); registryStoreSnapshotFile(new LeaderMetadataSnapshotFile(group)); - if (StoreConfig.StoreMode.RAFT.getName().equalsIgnoreCase(mode)) { + if (StoreMode.RAFT.getName().equalsIgnoreCase(mode)) { registryStoreSnapshotFile(new SessionSnapshotFile(group)); EXECUTES.put(ADD_GLOBAL_SESSION, new AddGlobalSessionExecute()); EXECUTES.put(ADD_BRANCH_SESSION, new AddBranchSessionExecute()); @@ -180,7 +182,7 @@ public void onApply(Iterator iterator) { @Override public void onSnapshotSave(final SnapshotWriter writer, final Closure done) { - if (!StringUtils.equals(StoreConfig.SessionMode.RAFT.getName(), mode)) { + if (!StringUtils.equals(SessionMode.RAFT.getName(), mode)) { done.run(Status.OK()); return; } @@ -198,7 +200,7 @@ public void onSnapshotSave(final SnapshotWriter writer, final Closure done) { @Override public boolean onSnapshotLoad(final SnapshotReader reader) { - if (!StringUtils.equals(StoreConfig.SessionMode.RAFT.getName(), mode)) { + if (!StringUtils.equals(SessionMode.RAFT.getName(), mode)) { return true; } if (isLeader()) { @@ -232,7 +234,7 @@ public void onLeaderStart(final long term) { try { // become the leader again,reloading global session SessionHolder.reload(SessionHolder.getRootSessionManager().allSessions(), - StoreConfig.SessionMode.RAFT, false); + SessionMode.RAFT, false); } finally { SeataClusterContext.unbindGroup(); } diff --git a/server/src/main/java/org/apache/seata/server/coordinator/DefaultCoordinator.java b/server/src/main/java/org/apache/seata/server/coordinator/DefaultCoordinator.java index 9003fe268aa..d1b9e281a7a 100644 --- a/server/src/main/java/org/apache/seata/server/coordinator/DefaultCoordinator.java +++ b/server/src/main/java/org/apache/seata/server/coordinator/DefaultCoordinator.java @@ -29,6 +29,7 @@ import io.netty.channel.Channel; import org.apache.seata.common.DefaultValues; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.common.thread.NamedThreadFactory; import org.apache.seata.common.util.CollectionUtils; import org.apache.seata.config.ConfigurationFactory; @@ -213,7 +214,7 @@ protected DefaultCoordinator(RemotingServer remotingServer) { boolean enableBranchAsyncRemove = CONFIG.getBoolean( ConfigurationKeys.ENABLE_BRANCH_ASYNC_REMOVE, DEFAULT_ENABLE_BRANCH_ASYNC_REMOVE); // create branchRemoveExecutor - if (enableBranchAsyncRemove && StoreConfig.getSessionMode() != StoreConfig.SessionMode.FILE) { + if (enableBranchAsyncRemove && StoreConfig.getSessionMode() != SessionMode.FILE) { branchRemoveExecutor = new ThreadPoolExecutor(BRANCH_ASYNC_POOL_SIZE, BRANCH_ASYNC_POOL_SIZE, Integer.MAX_VALUE, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>( @@ -229,8 +230,8 @@ public static DefaultCoordinator getInstance(RemotingServer remotingServer) { if (null == instance) { synchronized (DefaultCoordinator.class) { if (null == instance) { - StoreConfig.SessionMode storeMode = StoreConfig.getSessionMode(); - instance = Objects.equals(StoreConfig.SessionMode.RAFT, storeMode) + SessionMode storeMode = StoreConfig.getSessionMode(); + instance = Objects.equals(SessionMode.RAFT, storeMode) ? new RaftCoordinator(remotingServer) : new DefaultCoordinator(remotingServer); } } diff --git a/server/src/main/java/org/apache/seata/server/lock/LockerManagerFactory.java b/server/src/main/java/org/apache/seata/server/lock/LockerManagerFactory.java index ae9e2534115..45129d12f93 100644 --- a/server/src/main/java/org/apache/seata/server/lock/LockerManagerFactory.java +++ b/server/src/main/java/org/apache/seata/server/lock/LockerManagerFactory.java @@ -17,10 +17,11 @@ package org.apache.seata.server.lock; import org.apache.seata.common.loader.EnhancedServiceLoader; +import org.apache.seata.common.store.LockMode; +import org.apache.seata.common.store.StoreMode; import org.apache.seata.config.Configuration; import org.apache.seata.config.ConfigurationFactory; import org.apache.seata.server.store.StoreConfig; -import org.apache.seata.server.store.StoreConfig.LockMode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -65,7 +66,7 @@ public static void init(LockMode lockMode) { } LOGGER.info("use lock store mode: {}", lockMode.getName()); //if not exist the lock mode, throw exception - if (null != StoreConfig.StoreMode.get(lockMode.name())) { + if (null != StoreMode.get(lockMode.name())) { LOCK_MANAGER = EnhancedServiceLoader.load(LockManager.class, lockMode.getName()); } } diff --git a/server/src/main/java/org/apache/seata/server/session/SessionHolder.java b/server/src/main/java/org/apache/seata/server/session/SessionHolder.java index aa32ea001e6..afd8c8ad429 100644 --- a/server/src/main/java/org/apache/seata/server/session/SessionHolder.java +++ b/server/src/main/java/org/apache/seata/server/session/SessionHolder.java @@ -29,6 +29,7 @@ import org.apache.seata.common.exception.ShouldNeverHappenException; import org.apache.seata.common.exception.StoreException; import org.apache.seata.common.loader.EnhancedServiceLoader; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.common.util.CollectionUtils; import org.apache.seata.common.util.StringUtils; import org.apache.seata.config.Configuration; @@ -42,7 +43,6 @@ import org.apache.seata.server.cluster.raft.context.SeataClusterContext; import org.apache.seata.server.lock.distributed.DistributedLockerFactory; import org.apache.seata.server.store.StoreConfig; -import org.apache.seata.server.store.StoreConfig.SessionMode; import org.apache.seata.server.store.VGroupMappingStoreManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -116,27 +116,27 @@ public static void init(SessionMode sessionMode) { if (SessionMode.RAFT.equals(sessionMode)) { String group = CONFIG.getConfig(ConfigurationKeys.SERVER_RAFT_GROUP, DEFAULT_SEATA_GROUP); ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, SessionMode.RAFT.getName(), - new Object[]{ROOT_SESSION_MANAGER_NAME}); + new Object[]{ROOT_SESSION_MANAGER_NAME}); SESSION_MANAGER_MAP = new HashMap<>(); SESSION_MANAGER_MAP.put(group, ROOT_SESSION_MANAGER); RaftServerManager.start(); } else { String vGroupMappingStorePath = CONFIG.getConfig(ConfigurationKeys.STORE_FILE_DIR, - DEFAULT_VGROUP_MAPPING_STORE_FILE_DIR) + separator + DEFAULT_VGROUP_MAPPING_STORE_FILE_DIR) + separator + System.getProperty(SERVER_SERVICE_PORT_CAMEL); String sessionStorePath = - CONFIG.getConfig(ConfigurationKeys.STORE_FILE_DIR, DEFAULT_SESSION_STORE_FILE_DIR) + separator - + System.getProperty(SERVER_SERVICE_PORT_CAMEL); + CONFIG.getConfig(ConfigurationKeys.STORE_FILE_DIR, DEFAULT_SESSION_STORE_FILE_DIR) + separator + + System.getProperty(SERVER_SERVICE_PORT_CAMEL); if (StringUtils.isBlank(sessionStorePath) || StringUtils.isBlank(vGroupMappingStorePath)) { throw new StoreException("the {store.file.dir} is empty."); } ROOT_VGROUP_MAPPING_MANAGER = EnhancedServiceLoader.load(VGroupMappingStoreManager.class, SessionMode.FILE.getName(), - new Object[]{vGroupMappingStorePath}); + new Object[]{vGroupMappingStorePath}); ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, SessionMode.FILE.getName(), - new Object[]{ROOT_SESSION_MANAGER_NAME, sessionStorePath}); + new Object[]{ROOT_SESSION_MANAGER_NAME, sessionStorePath}); ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, SessionMode.FILE.getName(), - new Object[]{ROOT_SESSION_MANAGER_NAME, sessionStorePath}); + new Object[]{ROOT_SESSION_MANAGER_NAME, sessionStorePath}); reload(sessionMode); } } else if (SessionMode.REDIS.equals(sessionMode)) { @@ -169,7 +169,7 @@ public static void reload(Collection allSessions, SessionMode sto public static void reload(Collection allSessions, SessionMode storeMode, boolean acquireLock) { if ((SessionMode.FILE == storeMode || SessionMode.RAFT == storeMode) - && CollectionUtils.isNotEmpty(allSessions)) { + && CollectionUtils.isNotEmpty(allSessions)) { for (GlobalSession globalSession : allSessions) { GlobalStatus globalStatus = globalSession.getStatus(); switch (globalStatus) { @@ -179,7 +179,7 @@ public static void reload(Collection allSessions, SessionMode sto SessionHelper.endRollbacked(globalSession, true); } catch (TransactionException e) { LOGGER.error("Could not handle the global session, xid: {},error: {}", - globalSession.getXid(), e.getMessage()); + globalSession.getXid(), e.getMessage()); } break; case Committed: @@ -187,7 +187,7 @@ public static void reload(Collection allSessions, SessionMode sto SessionHelper.endCommitted(globalSession, true); } catch (TransactionException e) { LOGGER.error("Could not handle the global session, xid: {},error: {}", - globalSession.getXid(), e.getMessage()); + globalSession.getXid(), e.getMessage()); } break; case Finished: @@ -214,9 +214,9 @@ public static void reload(Collection allSessions, SessionMode sto if (acquireLock) { lockBranchSessions(globalSession.getSortedBranches()); if (GlobalStatus.Rollbacking.equals(globalSession.getStatus()) - || GlobalStatus.TimeoutRollbacking.equals(globalSession.getStatus())) { + || GlobalStatus.TimeoutRollbacking.equals(globalSession.getStatus())) { globalSession.getBranchSessions().parallelStream() - .forEach(branchSession -> branchSession.setLockStatus(LockStatus.Rollbacking)); + .forEach(branchSession -> branchSession.setLockStatus(LockStatus.Rollbacking)); } } switch (globalStatus) { @@ -230,7 +230,7 @@ public static void reload(Collection allSessions, SessionMode sto try { globalSession.changeGlobalStatus(GlobalStatus.RollbackRetrying); LOGGER.info("change global status: {}, xid: {}", globalSession.getStatus(), - globalSession.getXid()); + globalSession.getXid()); } catch (TransactionException e) { LOGGER.error("change global status fail: {}", e.getMessage(), e); } @@ -250,7 +250,7 @@ public static void reload(Collection allSessions, SessionMode sto // Redis, db and so on CompletableFuture.runAsync(() -> { SessionCondition searchCondition = new SessionCondition(GlobalStatus.UnKnown, GlobalStatus.Committed, - GlobalStatus.Rollbacked, GlobalStatus.TimeoutRollbacked, GlobalStatus.Finished); + GlobalStatus.Rollbacked, GlobalStatus.TimeoutRollbacked, GlobalStatus.Finished); searchCondition.setLazyLoadBranch(true); long now = System.currentTimeMillis(); @@ -320,7 +320,7 @@ public static SessionManager getRootSessionManager() { public static SessionManager getRootSessionManager(String group) { return StringUtils.isNotBlank(group) && SESSION_MANAGER_MAP != null - ? SESSION_MANAGER_MAP.computeIfAbsent(group, k -> ROOT_SESSION_MANAGER) : ROOT_SESSION_MANAGER; + ? SESSION_MANAGER_MAP.computeIfAbsent(group, k -> ROOT_SESSION_MANAGER) : ROOT_SESSION_MANAGER; } //endregion @@ -354,7 +354,7 @@ public static GlobalSession findGlobalSession(String xid, boolean withBranchSess * @return the value */ public static T lockAndExecute(GlobalSession globalSession, GlobalSession.LockCallable lockCallable) - throws TransactionException { + throws TransactionException { return getRootSessionManager().lockAndExecute(globalSession, lockCallable); } diff --git a/server/src/main/java/org/apache/seata/server/store/StoreConfig.java b/server/src/main/java/org/apache/seata/server/store/StoreConfig.java index adf949cf880..057753e21c7 100644 --- a/server/src/main/java/org/apache/seata/server/store/StoreConfig.java +++ b/server/src/main/java/org/apache/seata/server/store/StoreConfig.java @@ -16,6 +16,9 @@ */ package org.apache.seata.server.store; +import org.apache.seata.common.store.LockMode; +import org.apache.seata.common.store.SessionMode; +import org.apache.seata.common.store.StoreMode; import org.apache.seata.common.util.StringUtils; import org.apache.seata.config.Configuration; import org.apache.seata.config.ConfigurationFactory; @@ -142,119 +145,4 @@ public static LockMode getLockMode() { // complication old config return LockMode.get(getStoreMode().name()); } - - public enum StoreMode { - /** - * The File store mode. - */ - FILE("file"), - /** - * The Db store mode. - */ - DB("db"), - /** - * The Redis store mode. - */ - REDIS("redis"), - /** - * The Raft store mode. - */ - RAFT("raft"); - - private String name; - - StoreMode(String name) { - this.name = name; - } - - public static StoreMode get(String name) { - for (StoreMode mode : StoreMode.values()) { - if (mode.getName().equalsIgnoreCase(name)) { - return mode; - } - } - throw new IllegalArgumentException("unknown store mode:" + name); - } - - public String getName() { - return name; - } - } - - public enum SessionMode { - /** - * The File store mode. - */ - FILE("file"), - /** - * The Db store mode. - */ - DB("db"), - /** - * The Redis store mode. - */ - REDIS("redis"), - /** - * raft store - */ - RAFT("raft"); - - private String name; - - SessionMode(String name) { - this.name = name; - } - - public static SessionMode get(String name) { - for (SessionMode mode : SessionMode.values()) { - if (mode.getName().equalsIgnoreCase(name)) { - return mode; - } - } - throw new IllegalArgumentException("unknown session mode:" + name); - } - - public String getName() { - return name; - } - } - - public enum LockMode { - /** - * The File store mode. - */ - FILE("file"), - /** - * The Db store mode. - */ - DB("db"), - /** - * The Redis store mode. - */ - REDIS("redis"), - /** - * raft store - */ - RAFT("raft"); - - private String name; - - LockMode(String name) { - this.name = name; - } - - public static LockMode get(String name) { - for (LockMode mode : LockMode.values()) { - if (mode.getName().equalsIgnoreCase(name)) { - return mode; - } - } - throw new IllegalArgumentException("unknown lock mode:" + name); - } - - public String getName() { - return name; - } - } - } From 473ff27dd35b3d849779543261c83244bbb74a1a Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sun, 13 Oct 2024 16:09:05 +0800 Subject: [PATCH 11/13] Move SessionMode and LockMode to seata-common and apply StoreMode from seata-common --- .../seata/server/cluster/raft/RaftServerManager.java | 3 ++- .../apache/seata/server/coordinator/RaftCoordinator.java | 3 ++- .../org/apache/seata/server/session/SessionHelper.java | 2 +- .../seata/server/coordinator/DefaultCoordinatorTest.java | 2 +- .../apache/seata/server/coordinator/DefaultCoreTest.java | 2 +- .../seata/server/event/DefaultCoreForEventBusTest.java | 2 +- .../java/org/apache/seata/server/lock/LockManagerTest.java | 2 +- .../org/apache/seata/server/raft/RaftSyncMessageTest.java | 4 ++-- .../server/raft/execute/BranchSessionExecuteTest.java | 7 ++++--- .../server/raft/execute/GlobalSessionExecuteTest.java | 7 ++++--- .../apache/seata/server/raft/execute/LockExecuteTest.java | 7 ++++--- .../seata/server/session/FileSessionManagerTest.java | 3 +-- .../org/apache/seata/server/session/GlobalSessionTest.java | 5 ++--- .../org/apache/seata/server/session/SessionHolderTest.java | 3 +-- .../server/session/redis/RedisDistributedLockerTest.java | 4 ++-- .../org/apache/seata/server/store/SessionStoreTest.java | 2 +- .../server/store/file/FileTransactionStoreManagerTest.java | 3 ++- 17 files changed, 32 insertions(+), 29 deletions(-) diff --git a/server/src/main/java/org/apache/seata/server/cluster/raft/RaftServerManager.java b/server/src/main/java/org/apache/seata/server/cluster/raft/RaftServerManager.java index b891bea8484..47e7ee4432b 100644 --- a/server/src/main/java/org/apache/seata/server/cluster/raft/RaftServerManager.java +++ b/server/src/main/java/org/apache/seata/server/cluster/raft/RaftServerManager.java @@ -38,6 +38,7 @@ import com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl; import org.apache.seata.common.ConfigurationKeys; import org.apache.seata.common.XID; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.common.util.StringUtils; import org.apache.seata.config.ConfigurationFactory; import org.apache.seata.core.serializer.SerializerType; @@ -87,7 +88,7 @@ public static CliClientService getCliClientServiceInstance() { public static void init() { if (INIT.compareAndSet(false, true)) { String initConfStr = CONFIG.getConfig(ConfigurationKeys.SERVER_RAFT_SERVER_ADDR); - RAFT_MODE = StoreConfig.getSessionMode().equals(StoreConfig.SessionMode.RAFT); + RAFT_MODE = StoreConfig.getSessionMode().equals(SessionMode.RAFT); if (StringUtils.isBlank(initConfStr)) { if (RAFT_MODE) { throw new IllegalArgumentException( diff --git a/server/src/main/java/org/apache/seata/server/coordinator/RaftCoordinator.java b/server/src/main/java/org/apache/seata/server/coordinator/RaftCoordinator.java index db9af1926bf..8e01601b953 100644 --- a/server/src/main/java/org/apache/seata/server/coordinator/RaftCoordinator.java +++ b/server/src/main/java/org/apache/seata/server/coordinator/RaftCoordinator.java @@ -16,6 +16,7 @@ */ package org.apache.seata.server.coordinator; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.core.exception.TransactionException; import org.apache.seata.core.exception.TransactionExceptionCode; import org.apache.seata.core.protocol.transaction.AbstractTransactionRequest; @@ -64,7 +65,7 @@ private boolean isPass(String group) { } public static void setPrevent(String group, boolean prevent) { - if (StoreConfig.getSessionMode() == StoreConfig.SessionMode.RAFT) { + if (StoreConfig.getSessionMode() == SessionMode.RAFT) { GROUP_PREVENT.put(group, prevent); } } diff --git a/server/src/main/java/org/apache/seata/server/session/SessionHelper.java b/server/src/main/java/org/apache/seata/server/session/SessionHelper.java index 7ffab5f14be..9054c052a99 100644 --- a/server/src/main/java/org/apache/seata/server/session/SessionHelper.java +++ b/server/src/main/java/org/apache/seata/server/session/SessionHelper.java @@ -28,6 +28,7 @@ import java.util.stream.StreamSupport; import org.apache.seata.common.ConfigurationKeys; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.common.util.CollectionUtils; import org.apache.seata.config.Configuration; import org.apache.seata.config.ConfigurationFactory; @@ -42,7 +43,6 @@ import org.apache.seata.server.coordinator.DefaultCoordinator; import org.apache.seata.server.metrics.MetricsPublisher; import org.apache.seata.server.store.StoreConfig; -import org.apache.seata.server.store.StoreConfig.SessionMode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; diff --git a/server/src/test/java/org/apache/seata/server/coordinator/DefaultCoordinatorTest.java b/server/src/test/java/org/apache/seata/server/coordinator/DefaultCoordinatorTest.java index 1de1f04c0ca..02a31496a5f 100644 --- a/server/src/test/java/org/apache/seata/server/coordinator/DefaultCoordinatorTest.java +++ b/server/src/test/java/org/apache/seata/server/coordinator/DefaultCoordinatorTest.java @@ -27,6 +27,7 @@ import org.apache.seata.common.DefaultValues; import org.apache.seata.common.XID; import org.apache.seata.common.loader.EnhancedServiceLoader; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.common.util.NetUtil; import org.apache.seata.common.util.ReflectionUtil; import org.apache.seata.config.Configuration; @@ -45,7 +46,6 @@ import org.apache.seata.server.metrics.MetricsManager; import org.apache.seata.server.session.GlobalSession; import org.apache.seata.server.session.SessionHolder; -import org.apache.seata.server.store.StoreConfig.SessionMode; import org.apache.seata.server.util.StoreUtil; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; diff --git a/server/src/test/java/org/apache/seata/server/coordinator/DefaultCoreTest.java b/server/src/test/java/org/apache/seata/server/coordinator/DefaultCoreTest.java index 6db53df7db9..93a5fbc0542 100644 --- a/server/src/test/java/org/apache/seata/server/coordinator/DefaultCoreTest.java +++ b/server/src/test/java/org/apache/seata/server/coordinator/DefaultCoreTest.java @@ -19,6 +19,7 @@ import java.util.Collection; import java.util.stream.Stream; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.core.exception.TransactionException; import org.apache.seata.core.model.BranchStatus; import org.apache.seata.core.model.BranchType; @@ -28,7 +29,6 @@ import org.apache.seata.server.session.GlobalSession; import org.apache.seata.server.session.SessionHelper; import org.apache.seata.server.session.SessionHolder; -import org.apache.seata.server.store.StoreConfig.SessionMode; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; diff --git a/server/src/test/java/org/apache/seata/server/event/DefaultCoreForEventBusTest.java b/server/src/test/java/org/apache/seata/server/event/DefaultCoreForEventBusTest.java index 8b94efd10a8..0569efe000e 100644 --- a/server/src/test/java/org/apache/seata/server/event/DefaultCoreForEventBusTest.java +++ b/server/src/test/java/org/apache/seata/server/event/DefaultCoreForEventBusTest.java @@ -26,6 +26,7 @@ import com.google.common.eventbus.AllowConcurrentEvents; import com.google.common.eventbus.Subscribe; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.core.event.GlobalTransactionEvent; import org.apache.seata.core.exception.TransactionException; import org.apache.seata.core.model.GlobalStatus; @@ -37,7 +38,6 @@ import org.apache.seata.server.metrics.MetricsManager; import org.apache.seata.server.session.SessionHolder; import org.apache.seata.server.store.StoreConfig; -import org.apache.seata.server.store.StoreConfig.SessionMode; import org.apache.seata.server.util.StoreUtil; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; diff --git a/server/src/test/java/org/apache/seata/server/lock/LockManagerTest.java b/server/src/test/java/org/apache/seata/server/lock/LockManagerTest.java index 6d6889c8045..c96c002ee29 100644 --- a/server/src/test/java/org/apache/seata/server/lock/LockManagerTest.java +++ b/server/src/test/java/org/apache/seata/server/lock/LockManagerTest.java @@ -26,6 +26,7 @@ import javax.annotation.Resource; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.common.util.CollectionUtils; import org.apache.seata.common.result.PageResult; import org.apache.seata.core.exception.TransactionException; @@ -39,7 +40,6 @@ import org.apache.seata.server.session.GlobalSession; import org.apache.seata.server.session.SessionHolder; import org.apache.seata.server.session.SessionManager; -import org.apache.seata.server.store.StoreConfig.SessionMode; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.params.ParameterizedTest; diff --git a/server/src/test/java/org/apache/seata/server/raft/RaftSyncMessageTest.java b/server/src/test/java/org/apache/seata/server/raft/RaftSyncMessageTest.java index e6f9c8f905d..f28bb754420 100644 --- a/server/src/test/java/org/apache/seata/server/raft/RaftSyncMessageTest.java +++ b/server/src/test/java/org/apache/seata/server/raft/RaftSyncMessageTest.java @@ -27,6 +27,7 @@ import org.apache.seata.common.exception.SeataRuntimeException; import org.apache.seata.common.metadata.ClusterRole; import org.apache.seata.common.metadata.Node; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.core.exception.TransactionException; import org.apache.seata.core.model.BranchType; import org.apache.seata.server.cluster.raft.snapshot.RaftSnapshot; @@ -43,7 +44,6 @@ import org.apache.seata.server.session.GlobalSession; import org.apache.seata.server.session.SessionHelper; import org.apache.seata.server.session.SessionHolder; -import org.apache.seata.server.store.StoreConfig; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -58,7 +58,7 @@ public class RaftSyncMessageTest { @BeforeAll public static void setUp(ApplicationContext context){ - SessionHolder.init(StoreConfig.SessionMode.FILE); + SessionHolder.init(SessionMode.FILE); } @AfterAll diff --git a/server/src/test/java/org/apache/seata/server/raft/execute/BranchSessionExecuteTest.java b/server/src/test/java/org/apache/seata/server/raft/execute/BranchSessionExecuteTest.java index ade3a25ef9c..6d8fbf501be 100644 --- a/server/src/test/java/org/apache/seata/server/raft/execute/BranchSessionExecuteTest.java +++ b/server/src/test/java/org/apache/seata/server/raft/execute/BranchSessionExecuteTest.java @@ -16,6 +16,8 @@ */ package org.apache.seata.server.raft.execute; +import org.apache.seata.common.store.LockMode; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.common.util.NetUtil; import org.apache.seata.config.ConfigurationCache; import org.apache.seata.core.exception.TransactionException; @@ -31,7 +33,6 @@ import org.apache.seata.server.session.GlobalSession; import org.apache.seata.server.session.SessionHolder; import org.apache.seata.server.storage.SessionConverter; -import org.apache.seata.server.store.StoreConfig; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; @@ -58,9 +59,9 @@ class BranchSessionExecuteTest { @BeforeAll public static void setUp(ApplicationContext context) throws TransactionException { System.setProperty("server.raft.serverAddr", NetUtil.getLocalIp() + ":9091"); - SessionHolder.init(StoreConfig.SessionMode.RAFT); + SessionHolder.init(SessionMode.RAFT); LockerManagerFactory.destroy(); - LockerManagerFactory.init(StoreConfig.LockMode.RAFT); + LockerManagerFactory.init(LockMode.RAFT); } @AfterAll diff --git a/server/src/test/java/org/apache/seata/server/raft/execute/GlobalSessionExecuteTest.java b/server/src/test/java/org/apache/seata/server/raft/execute/GlobalSessionExecuteTest.java index 9594eb7de56..45d191790ee 100644 --- a/server/src/test/java/org/apache/seata/server/raft/execute/GlobalSessionExecuteTest.java +++ b/server/src/test/java/org/apache/seata/server/raft/execute/GlobalSessionExecuteTest.java @@ -16,6 +16,8 @@ */ package org.apache.seata.server.raft.execute; +import org.apache.seata.common.store.LockMode; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.common.util.NetUtil; import org.apache.seata.config.ConfigurationCache; import org.apache.seata.core.exception.TransactionException; @@ -30,7 +32,6 @@ import org.apache.seata.server.session.SessionHolder; import org.apache.seata.server.session.SessionManager; import org.apache.seata.server.storage.SessionConverter; -import org.apache.seata.server.store.StoreConfig; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; @@ -47,9 +48,9 @@ class GlobalSessionExecuteTest { @BeforeAll public static void setUp(ApplicationContext context){ System.setProperty("server.raft.serverAddr", NetUtil.getLocalIp() + ":9091"); - SessionHolder.init(StoreConfig.SessionMode.RAFT); + SessionHolder.init(SessionMode.RAFT); LockerManagerFactory.destroy(); - LockerManagerFactory.init(StoreConfig.LockMode.RAFT); + LockerManagerFactory.init(LockMode.RAFT); } @AfterAll diff --git a/server/src/test/java/org/apache/seata/server/raft/execute/LockExecuteTest.java b/server/src/test/java/org/apache/seata/server/raft/execute/LockExecuteTest.java index 36cbbaf0d8a..b9f26a9372e 100644 --- a/server/src/test/java/org/apache/seata/server/raft/execute/LockExecuteTest.java +++ b/server/src/test/java/org/apache/seata/server/raft/execute/LockExecuteTest.java @@ -16,6 +16,8 @@ */ package org.apache.seata.server.raft.execute; +import org.apache.seata.common.store.LockMode; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.common.util.NetUtil; import org.apache.seata.config.ConfigurationCache; import org.apache.seata.core.exception.TransactionException; @@ -32,7 +34,6 @@ import org.apache.seata.server.session.GlobalSession; import org.apache.seata.server.session.SessionHolder; import org.apache.seata.server.storage.SessionConverter; -import org.apache.seata.server.store.StoreConfig; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; @@ -59,9 +60,9 @@ class LockExecuteTest { @BeforeAll public static void setUp(ApplicationContext context) throws TransactionException { System.setProperty("server.raft.serverAddr", NetUtil.getLocalIp() + ":9091"); - SessionHolder.init(StoreConfig.SessionMode.RAFT); + SessionHolder.init(SessionMode.RAFT); LockerManagerFactory.destroy(); - LockerManagerFactory.init(StoreConfig.LockMode.RAFT); + LockerManagerFactory.init(LockMode.RAFT); } @AfterAll diff --git a/server/src/test/java/org/apache/seata/server/session/FileSessionManagerTest.java b/server/src/test/java/org/apache/seata/server/session/FileSessionManagerTest.java index 56ce51d343f..478529866af 100644 --- a/server/src/test/java/org/apache/seata/server/session/FileSessionManagerTest.java +++ b/server/src/test/java/org/apache/seata/server/session/FileSessionManagerTest.java @@ -30,6 +30,7 @@ import org.apache.seata.common.XID; import org.apache.seata.common.loader.EnhancedServiceLoader; import org.apache.seata.common.result.PageResult; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.core.model.BranchStatus; import org.apache.seata.core.model.BranchType; import org.apache.seata.core.model.GlobalStatus; @@ -38,10 +39,8 @@ import org.apache.seata.server.console.service.GlobalSessionService; import org.apache.seata.server.console.vo.GlobalSessionVO; import org.apache.seata.server.storage.file.session.FileSessionManager; -import org.apache.seata.server.store.StoreConfig.SessionMode; import org.apache.seata.server.util.StoreUtil; import org.apache.commons.lang.time.DateUtils; -import org.apache.seata.server.session.SessionHolder; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.params.ParameterizedTest; diff --git a/server/src/test/java/org/apache/seata/server/session/GlobalSessionTest.java b/server/src/test/java/org/apache/seata/server/session/GlobalSessionTest.java index b9083874c19..08529720e2f 100644 --- a/server/src/test/java/org/apache/seata/server/session/GlobalSessionTest.java +++ b/server/src/test/java/org/apache/seata/server/session/GlobalSessionTest.java @@ -19,11 +19,10 @@ import java.io.IOException; import java.util.stream.Stream; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.core.model.BranchStatus; import org.apache.seata.core.model.BranchType; import org.apache.seata.core.model.GlobalStatus; -import org.apache.seata.server.store.StoreConfig; -import org.apache.seata.server.session.SessionHolder; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -47,7 +46,7 @@ public class GlobalSessionTest { @BeforeAll public static void init(ApplicationContext context){ - SessionHolder.init(StoreConfig.SessionMode.FILE); + SessionHolder.init(SessionMode.FILE); } @AfterAll public static void destroy(){ diff --git a/server/src/test/java/org/apache/seata/server/session/SessionHolderTest.java b/server/src/test/java/org/apache/seata/server/session/SessionHolderTest.java index 9ac513731c4..6e6fe855c6f 100644 --- a/server/src/test/java/org/apache/seata/server/session/SessionHolderTest.java +++ b/server/src/test/java/org/apache/seata/server/session/SessionHolderTest.java @@ -19,9 +19,8 @@ import java.io.File; import java.io.IOException; import org.apache.seata.common.XID; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.core.constants.ConfigurationKeys; -import org.apache.seata.server.store.StoreConfig.SessionMode; -import org.apache.seata.server.session.SessionHolder; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; diff --git a/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java b/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java index d4f38a2bcc3..215571d3eb9 100644 --- a/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java +++ b/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java @@ -20,6 +20,8 @@ import org.apache.seata.common.XID; import org.apache.seata.common.loader.EnhancedServiceLoader; +import org.apache.seata.common.store.SessionMode; +import org.apache.seata.common.store.StoreMode; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -33,8 +35,6 @@ import org.apache.seata.server.lock.distributed.DistributedLockerFactory; import org.apache.seata.server.session.SessionHolder; import org.apache.seata.server.storage.redis.JedisPooledFactory; -import org.apache.seata.server.store.StoreConfig.SessionMode; -import static org.apache.seata.server.store.StoreConfig.StoreMode; /** * @description redis distributed lock test diff --git a/server/src/test/java/org/apache/seata/server/store/SessionStoreTest.java b/server/src/test/java/org/apache/seata/server/store/SessionStoreTest.java index 50c677df211..23da64b7297 100644 --- a/server/src/test/java/org/apache/seata/server/store/SessionStoreTest.java +++ b/server/src/test/java/org/apache/seata/server/store/SessionStoreTest.java @@ -19,6 +19,7 @@ import java.io.File; import org.apache.seata.common.XID; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.config.Configuration; import org.apache.seata.config.ConfigurationFactory; import org.apache.seata.core.constants.ConfigurationKeys; @@ -31,7 +32,6 @@ import org.apache.seata.server.session.GlobalSession; import org.apache.seata.server.session.SessionHelper; import org.apache.seata.server.session.SessionHolder; -import org.apache.seata.server.store.StoreConfig.SessionMode; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; diff --git a/server/src/test/java/org/apache/seata/server/store/file/FileTransactionStoreManagerTest.java b/server/src/test/java/org/apache/seata/server/store/file/FileTransactionStoreManagerTest.java index d9c8ec08310..dca761e1238 100644 --- a/server/src/test/java/org/apache/seata/server/store/file/FileTransactionStoreManagerTest.java +++ b/server/src/test/java/org/apache/seata/server/store/file/FileTransactionStoreManagerTest.java @@ -23,6 +23,7 @@ import java.util.Collection; import java.util.List; +import org.apache.seata.common.store.SessionMode; import org.apache.seata.server.session.SessionHolder; import org.assertj.core.util.Files; import org.junit.jupiter.api.AfterAll; @@ -51,7 +52,7 @@ public class FileTransactionStoreManagerTest { @BeforeAll public static void init(ApplicationContext context){ - SessionHolder.init(StoreConfig.SessionMode.FILE); + SessionHolder.init(SessionMode.FILE); } @AfterAll public static void destroy(){ From cda9a190af1be0ad49bc372a98a2c32f10f21b80 Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sun, 13 Oct 2024 16:14:20 +0800 Subject: [PATCH 12/13] add head --- .../org/apache/seata/common/store/LockMode.java | 16 ++++++++++++++++ .../apache/seata/common/store/SessionMode.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/common/src/main/java/org/apache/seata/common/store/LockMode.java b/common/src/main/java/org/apache/seata/common/store/LockMode.java index 8b60e73d9ef..e36c6547f86 100644 --- a/common/src/main/java/org/apache/seata/common/store/LockMode.java +++ b/common/src/main/java/org/apache/seata/common/store/LockMode.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.seata.common.store; public enum LockMode { diff --git a/common/src/main/java/org/apache/seata/common/store/SessionMode.java b/common/src/main/java/org/apache/seata/common/store/SessionMode.java index 5ec487f2a39..3db1307d82b 100644 --- a/common/src/main/java/org/apache/seata/common/store/SessionMode.java +++ b/common/src/main/java/org/apache/seata/common/store/SessionMode.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.seata.common.store; public enum SessionMode { From 44152ac4bdb5ab8f1cffebba8b42d7d9768e4028 Mon Sep 17 00:00:00 2001 From: lyl2008dsg Date: Sun, 10 Nov 2024 23:14:03 +0800 Subject: [PATCH 13/13] Ensures springConfigurableEnvironment is available for getConfigFromEnvironment calls, addressing the loading timing issue. --- .../listener/SeataApplicationListener.java | 63 +++++++++++++++++++ .../SpringBootConfigurationProvider.java | 4 -- .../main/resources/META-INF/spring.factories | 5 +- .../SeataCoreAutoConfigurationTest.java | 8 +-- .../resources/application-test.properties | 2 +- 5 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/listener/SeataApplicationListener.java diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/listener/SeataApplicationListener.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/listener/SeataApplicationListener.java new file mode 100644 index 00000000000..09f212fe4f4 --- /dev/null +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/listener/SeataApplicationListener.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.spring.boot.autoconfigure.listener; + +import org.apache.seata.common.holder.ObjectHolder; +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.boot.context.logging.LoggingApplicationListener; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.event.GenericApplicationListener; +import org.springframework.core.ResolvableType; +import org.springframework.core.env.ConfigurableEnvironment; + +import static org.apache.seata.common.Constants.OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT; + + +public class SeataApplicationListener implements GenericApplicationListener { + + @Override + public boolean supportsEventType(ResolvableType eventType) { + return eventType.getRawClass() != null + && (ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType.getRawClass()) || + ApplicationReadyEvent.class.isAssignableFrom(eventType.getRawClass())); + } + + @Override + public void onApplicationEvent(ApplicationEvent event) { + if (event instanceof ApplicationReadyEvent && Boolean.parseBoolean(System.getProperty("production.deploy.output"))) { + System.setProperty("ENV_LOG_SYS_BOOT_COMPLETED", "true"); + return; + } + if (!(event instanceof ApplicationEnvironmentPreparedEvent)) { + return; + } + ApplicationEnvironmentPreparedEvent environmentPreparedEvent = (ApplicationEnvironmentPreparedEvent)event; + ConfigurableEnvironment environment = environmentPreparedEvent.getEnvironment(); + ObjectHolder.INSTANCE.setObject(OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT, environment); + } + + /** + * higher than LoggingApplicationListener + * + * @return the order + */ + @Override + public int getOrder() { + return LoggingApplicationListener.DEFAULT_ORDER - 1; + } +} \ No newline at end of file diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SpringBootConfigurationProvider.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SpringBootConfigurationProvider.java index 45989b0ae01..c390de9ebe8 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SpringBootConfigurationProvider.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/java/org/apache/seata/spring/boot/autoconfigure/provider/SpringBootConfigurationProvider.java @@ -68,10 +68,6 @@ public Configuration provide(Configuration originalConfiguration) { // 1. Get config value from the system property result = originalConfiguration.getConfigFromSys(rawDataId); - if (result == null) { - result = originalConfiguration.getConfig(rawDataId); - } - if (result == null) { String dataId = convertDataId(rawDataId); diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/resources/META-INF/spring.factories b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/resources/META-INF/spring.factories index 62068fab123..27e069aa2ef 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/resources/META-INF/spring.factories +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/main/resources/META-INF/spring.factories @@ -23,4 +23,7 @@ org.springframework.boot.env.EnvironmentPostProcessor=\ org.apache.seata.spring.boot.autoconfigure.SeataCoreEnvironmentPostProcessor org.springframework.context.ApplicationContextInitializer=\ -org.apache.seata.spring.boot.autoconfigure.loader.SeataPropertiesLoader \ No newline at end of file +org.apache.seata.spring.boot.autoconfigure.loader.SeataPropertiesLoader + +org.springframework.context.ApplicationListener=\ +org.apache.seata.spring.boot.autoconfigure.listener.SeataApplicationListener \ No newline at end of file diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java index 52051848d01..8fbf0075e43 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/org/apache/seata/spring/boot/autoconfigure/SeataCoreAutoConfigurationTest.java @@ -19,7 +19,6 @@ import org.apache.seata.config.Configuration; import org.apache.seata.config.ConfigurationFactory; import org.apache.seata.config.FileConfiguration; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; @@ -41,13 +40,10 @@ public class SeataCoreAutoConfigurationTest { @Autowired private Environment environment; - @BeforeAll - public static void init() { - ConfigurationFactory.reload(); - } - @Test public void testSeataPropertiesLoaded() { + ConfigurationFactory.reload(); + // default file.conf String dbUrl = environment.getProperty("seata.store.db.url"); assertEquals("jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true", dbUrl, "The DB URL should be correctly loaded from configuration"); diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/application-test.properties b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/application-test.properties index dc1f0851cfc..c492f36092b 100755 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/application-test.properties +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/resources/application-test.properties @@ -17,7 +17,7 @@ seata.config.type=file seata.config.data-type=bbb -seata.config.file.name=aaa +seata.config.file.name=file.conf seata.config.consul.server-addr=aaa seata.config.consul.acl-token=bbb