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 16 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 @@ -43,7 +43,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6950](https://github.com/apache/incubator-seata/pull/6950)] Remove JVM parameter app.id
- [[#6959](https://github.com/apache/incubator-seata/pull/6959)] update the naming and description for the `seata-http-jakarta` module
- [[#6991](https://github.com/apache/incubator-seata/pull/6991)] gRPC serialization default to Protobuf

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

### refactor:

Expand All @@ -69,6 +69,7 @@ Thanks to these contributors for their code commits. Please report an unintended
- [xingfudeshi](https://github.com/xingfudeshi)
- [o-jimin](https://github.com/o-jimin)
- [lixingjia77](https://github.com/lixingjia77)
- [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 @@ -45,6 +45,7 @@
- [[#6950](https://github.com/apache/incubator-seata/pull/6950)] 移除JVM参数app.id
- [[#6959](https://github.com/apache/incubator-seata/pull/6959)] 修正 `seata-http-jakarta`的模块命名和描述
- [[#6991](https://github.com/apache/incubator-seata/pull/6991)] gRPC协议序列化默认值为protobuf
- [[#6828](https://github.com/apache/incubator-seata/pull/6828)] seata-spring-boot-starter兼容file.conf和registry.conf

### refactor:

Expand All @@ -71,7 +72,7 @@
- [xingfudeshi](https://github.com/xingfudeshi)
- [o-jimin](https://github.com/o-jimin)
- [lixingjia77](https://github.com/lixingjia77)

- [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,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;
}
}
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 @@ -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
Loading