Skip to content

Commit

Permalink
Merge branch '2.x' into issue_6957_2.x_1
Browse files Browse the repository at this point in the history
  • Loading branch information
funky-eyes authored Dec 25, 2024
2 parents 4d98d54 + 748f50e commit 82e33b6
Show file tree
Hide file tree
Showing 35 changed files with 482 additions and 167 deletions.
4 changes: 2 additions & 2 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Add changes here for all PR submitted to the 2.x branch.

### optimize:

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

### security:

Expand All @@ -31,6 +31,6 @@ Thanks to these contributors for their code commits. Please report an unintended
<!-- Please make sure your Github ID is in the list below -->

- [slievrly](https://github.com/slievrly)
- [GITHUB_ID](https://github.com/GITHUB_ID)
- [lyl2008dsg](https://github.com/lyl2008dsg)

Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
4 changes: 2 additions & 2 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

### optimize:

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

### security:

Expand All @@ -31,6 +31,6 @@
<!-- 请确保您的 GitHub ID 在以下列表中 -->

- [slievrly](https://github.com/slievrly)
- [GITHUB_ID](https://github.com/GITHUB_ID)
- [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
@@ -0,0 +1,59 @@
/*
* 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 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;
}
}
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() {
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 @@ -21,3 +21,9 @@ 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

org.springframework.context.ApplicationListener=\
org.apache.seata.spring.boot.autoconfigure.listener.SeataApplicationListener
Loading

0 comments on commit 82e33b6

Please sign in to comment.