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 all 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
3 changes: 2 additions & 1 deletion changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6905](https://github.com/apache/incubator-seata/pull/6905)] remove incompatible licenses at build time
- [[#6906](https://github.com/apache/incubator-seata/pull/6906)] h2 dependency adds test scope
- [[#6911](https://github.com/apache/incubator-seata/pull/6911)] fix some typos in project

- [[#6828](https://github.com/apache/incubator-seata/pull/6828)] spring boot compatible with file.conf and registry.conf

### refactor:

Expand All @@ -49,6 +49,7 @@ Thanks to these contributors for their code commits. Please report an unintended
- [PleaseGiveMeTheCoke](https://github.com/PleaseGiveMeTheCoke)
- [dsomehan](https://github.com/dsomehan)
- [psxjoy](https://github.com/psxjoy)
- [lyl2008dsg](https://github.com/lyl2008dsg)



Expand Down
3 changes: 2 additions & 1 deletion changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
- [[#6905](https://github.com/apache/incubator-seata/pull/6905)] 移除构建期不兼容的 license
- [[#6906](https://github.com/apache/incubator-seata/pull/6906)] h2依赖添加test scope
- [[#6911](https://github.com/apache/incubator-seata/pull/6911)] 修正项目中的部分拼写错误

- [[#6828](https://github.com/apache/incubator-seata/pull/6828)] seata-spring-boot-starter兼容file.conf和registry.conf

### refactor:

Expand All @@ -52,6 +52,7 @@
- [PleaseGiveMeTheCoke](https://github.com/PleaseGiveMeTheCoke)
- [dsomehan](https://github.com/dsomehan)
- [psxjoy](https://github.com/psxjoy)
- [lyl2008dsg](https://github.com/lyl2008dsg)

同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。

69 changes: 69 additions & 0 deletions common/src/main/java/org/apache/seata/common/store/LockMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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 {
/**
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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 {
/**
* 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;
}
}
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,22 @@
* 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.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 +75,30 @@ 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");
Optional<String> sessionMode = invokeEnumMethod(storeConfigClass, "getSessionMode", "getName");
Optional<String> 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.
// StoreConfig only exists on the server side
}
}

private Optional<String> 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();
}
}
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,92 @@
/*
* 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.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;
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 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)
@TestPropertySource(locations = "classpath:application-test.properties")
public class SeataCoreAutoConfigurationTest {

@Autowired
private Environment environment;

@BeforeAll
public static void init() {
ConfigurationFactory.reload();
}

@Test
public void testSeataPropertiesLoaded() {
// 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");

// 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 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<FileConfiguration> 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");
}
}
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
Loading
Loading