Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: spring boot compatible with file.conf and registry.conf (#6811) #6828

Open
wants to merge 13 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
11 changes: 11 additions & 0 deletions seata-spring-autoconfigure/seata-spring-autoconfigure-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,16 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@
* 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;
import org.springframework.core.annotation.Order;
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;
Expand Down Expand Up @@ -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() {
lyl2008dsg marked this conversation as resolved.
Show resolved Hide resolved
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");
lyl2008dsg marked this conversation as resolved.
Show resolved Hide resolved
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<String> invokeStaticMethod(Class<?> clazz, String methodName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method = clazz.getMethod(methodName);
return Optional.ofNullable((String) method.invoke(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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.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 Environment environment;

@Test
public void testSeataPropertiesLoaded() {
// default file.conf
String txServiceGroup = environment.getProperty("seata.store.db.url");
funky-eyes marked this conversation as resolved.
Show resolved Hide resolved
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");
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
4 changes: 1 addition & 3 deletions server/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
Expand Up @@ -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
org.apache.seata.server.spring.listener.ServerApplicationListener
Loading