Skip to content

Commit

Permalink
bugfix: fixed the inability to get some remote configurations (apache…
Browse files Browse the repository at this point in the history
  • Loading branch information
funky-eyes authored Mar 8, 2022
1 parent 3d7a8b6 commit 1ddde40
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 81 deletions.
1 change: 1 addition & 0 deletions changes/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [[#4408](https://github.com/seata/seata/pull/4408)] 修复容器环境中设置环境变量无效的问题
- [[#4441](https://github.com/seata/seata/pull/4441)] 修复redis模式下查询时未关闭Pipeline和分支注册后添加分支session时branchSessions为null的问题
- [[#4438](https://github.com/seata/seata/pull/4438)] 修复develop版本file模式下GlobalSession在延迟删除的情况下无法被正常删除的问题
- [[#4432](https://github.com/seata/seata/pull/4432)] 修复develop版本下ServerApplicationListener无法读取配置中心配置的问题


### optimize:
Expand Down
1 change: 1 addition & 0 deletions changes/en-us/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
- [[#4408](https://github.com/seata/seata/pull/4408)] fix the invalid environment variable in container env
- [[#4441](https://github.com/seata/seata/pull/4441)] fix the problem that pipelined resources are not closed in redis mode and add branchSession judge branchSessions is not null
- [[#4438](https://github.com/seata/seata/pull/4438)] fix the problem that GlobalSession could not be deleted normally in the case of delayed deletion in the file mode of the develop branch
- [[#4432](https://github.com/seata/seata/pull/4432)] fix the inability to get some remote configurations



Expand Down
4 changes: 4 additions & 0 deletions common/src/main/java/io/seata/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public interface Constants {
* The constant OBJECT_KEY_SPRING_APPLICATION_CONTEXT
*/
String OBJECT_KEY_SPRING_APPLICATION_CONTEXT = "springApplicationContext";
/**
* The constant OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT
*/
String OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT = "springConfigurableEnvironment";
/**
* The constant BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER
*/
Expand Down
30 changes: 30 additions & 0 deletions common/src/main/java/io/seata/common/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.seata.common.Constants;
import io.seata.common.exception.ShouldNeverHappenException;
Expand All @@ -36,6 +38,9 @@
*/
public class StringUtils {

private static final Pattern CAMEL_PATTERN = Pattern.compile("[A-Z]");
private static final Pattern LINE_PATTERN = Pattern.compile("-(\\w)");

private StringUtils() {
}

Expand Down Expand Up @@ -319,4 +324,29 @@ public static boolean isEmpty(final CharSequence cs) {
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}

/**
* hump to Line or line to hump, only spring environment use
*
* @param str str
* @return string string
*/
public static String hump2Line(String str) {
Matcher matcher = CAMEL_PATTERN.matcher(str);
StringBuffer sb = new StringBuffer();
if (matcher.find()) {
matcher.appendReplacement(sb, "-" + matcher.group(0).toLowerCase());
while (matcher.find()) {
matcher.appendReplacement(sb, "-" + matcher.group(0).toLowerCase());
}
} else {
matcher = LINE_PATTERN.matcher(str);
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
}
matcher.appendTail(sb);
return sb.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public void testIsNullOrEmpty() {
assertThat(StringUtils.isNullOrEmpty(" ")).isFalse();
}

@Test
public void testHump2Line(){
assertThat(StringUtils.hump2Line("abc-d").equals("abcD")).isTrue();
assertThat(StringUtils.hump2Line("aBc").equals("a-bc")).isTrue();
assertThat(StringUtils.hump2Line("abc").equals("abc")).isTrue();
}

@Test
public void testInputStream2String() throws IOException {
assertNull(StringUtils.inputStream2String(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@
import org.springframework.context.ApplicationContextAware;

import static io.seata.common.Constants.OBJECT_KEY_SPRING_APPLICATION_CONTEXT;
import static io.seata.common.Constants.OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT;

/**
* @author [email protected]
* @author funkye
* The type spring application context provider
*/
public class SpringApplicationContextProvider implements ApplicationContextAware, BeanFactoryPostProcessor {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ObjectHolder.INSTANCE.setObject(OBJECT_KEY_SPRING_APPLICATION_CONTEXT, applicationContext);
if (ObjectHolder.INSTANCE.getObject(OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT) == null) {
ObjectHolder.INSTANCE.setObject(OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT,
applicationContext.getEnvironment());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.seata.spring.boot.autoconfigure;

import io.seata.common.holder.ObjectHolder;
import io.seata.rm.tcc.config.TCCFenceConfig;
import io.seata.saga.engine.StateMachineConfig;
import io.seata.spring.boot.autoconfigure.properties.SagaAsyncThreadPoolProperties;
Expand All @@ -31,6 +32,7 @@
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

import static io.seata.common.Constants.OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CLIENT_RM_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CLIENT_TM_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.COMPRESS_PREFIX;
Expand All @@ -52,6 +54,7 @@ public class SeataClientEnvironmentPostProcessor implements EnvironmentPostProce

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
ObjectHolder.INSTANCE.setObject(OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT, environment);
PROPERTY_BEAN_MAP.put(SEATA_PREFIX, SeataProperties.class);

PROPERTY_BEAN_MAP.put(CLIENT_RM_PREFIX, RmProperties.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.seata.spring.boot.autoconfigure;

import java.util.concurrent.atomic.AtomicBoolean;
import io.seata.spring.boot.autoconfigure.properties.LogProperties;
import io.seata.spring.boot.autoconfigure.properties.ShutdownProperties;
import io.seata.spring.boot.autoconfigure.properties.ThreadFactoryProperties;
Expand Down Expand Up @@ -67,39 +68,49 @@
/**
* @author [email protected]
* @author wang.liang
* @author funkye
*/
public class SeataCoreEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {

private static final AtomicBoolean INIT = new AtomicBoolean(false);

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
PROPERTY_BEAN_MAP.put(CONFIG_PREFIX, ConfigProperties.class);
PROPERTY_BEAN_MAP.put(CONFIG_FILE_PREFIX, ConfigFileProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_PREFIX, RegistryProperties.class);

PROPERTY_BEAN_MAP.put(CONFIG_NACOS_PREFIX, ConfigNacosProperties.class);
PROPERTY_BEAN_MAP.put(CONFIG_CONSUL_PREFIX, ConfigConsulProperties.class);
PROPERTY_BEAN_MAP.put(CONFIG_ZK_PREFIX, ConfigZooKeeperProperties.class);
PROPERTY_BEAN_MAP.put(CONFIG_APOLLO_PREFIX, ConfigApolloProperties.class);
PROPERTY_BEAN_MAP.put(CONFIG_ETCD3_PREFIX, ConfigEtcd3Properties.class);
PROPERTY_BEAN_MAP.put(CONFIG_CUSTOM_PREFIX, ConfigCustomProperties.class);

PROPERTY_BEAN_MAP.put(REGISTRY_CONSUL_PREFIX, RegistryConsulProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_ETCD3_PREFIX, RegistryEtcd3Properties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_EUREKA_PREFIX, RegistryEurekaProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_NACOS_PREFIX, RegistryNacosProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_REDIS_PREFIX, RegistryRedisProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_SOFA_PREFIX, RegistrySofaProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_ZK_PREFIX, RegistryZooKeeperProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_CUSTOM_PREFIX, RegistryCustomProperties.class);

PROPERTY_BEAN_MAP.put(THREAD_FACTORY_PREFIX, ThreadFactoryProperties.class);
PROPERTY_BEAN_MAP.put(TRANSPORT_PREFIX, TransportProperties.class);
PROPERTY_BEAN_MAP.put(SHUTDOWN_PREFIX, ShutdownProperties.class);
PROPERTY_BEAN_MAP.put(LOG_PREFIX, LogProperties.class);
init();
}

@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}

public static void init() {
if (INIT.compareAndSet(false, true)) {
PROPERTY_BEAN_MAP.put(CONFIG_PREFIX, ConfigProperties.class);
PROPERTY_BEAN_MAP.put(CONFIG_FILE_PREFIX, ConfigFileProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_PREFIX, RegistryProperties.class);

PROPERTY_BEAN_MAP.put(CONFIG_NACOS_PREFIX, ConfigNacosProperties.class);
PROPERTY_BEAN_MAP.put(CONFIG_CONSUL_PREFIX, ConfigConsulProperties.class);
PROPERTY_BEAN_MAP.put(CONFIG_ZK_PREFIX, ConfigZooKeeperProperties.class);
PROPERTY_BEAN_MAP.put(CONFIG_APOLLO_PREFIX, ConfigApolloProperties.class);
PROPERTY_BEAN_MAP.put(CONFIG_ETCD3_PREFIX, ConfigEtcd3Properties.class);
PROPERTY_BEAN_MAP.put(CONFIG_CUSTOM_PREFIX, ConfigCustomProperties.class);

PROPERTY_BEAN_MAP.put(REGISTRY_CONSUL_PREFIX, RegistryConsulProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_ETCD3_PREFIX, RegistryEtcd3Properties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_EUREKA_PREFIX, RegistryEurekaProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_NACOS_PREFIX, RegistryNacosProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_REDIS_PREFIX, RegistryRedisProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_SOFA_PREFIX, RegistrySofaProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_ZK_PREFIX, RegistryZooKeeperProperties.class);
PROPERTY_BEAN_MAP.put(REGISTRY_CUSTOM_PREFIX, RegistryCustomProperties.class);

PROPERTY_BEAN_MAP.put(THREAD_FACTORY_PREFIX, ThreadFactoryProperties.class);
PROPERTY_BEAN_MAP.put(TRANSPORT_PREFIX, TransportProperties.class);
PROPERTY_BEAN_MAP.put(SHUTDOWN_PREFIX, ShutdownProperties.class);
PROPERTY_BEAN_MAP.put(LOG_PREFIX, LogProperties.class);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ public interface StarterConstants {
* The following special keys need to be normalized.
*/
String SPECIAL_KEY_GROUPLIST = "grouplist";
String SPECIAL_KEY_SERVICE = "service";
String SPECIAL_KEY_VGROUP_MAPPING = "vgroupMapping";
}
Loading

0 comments on commit 1ddde40

Please sign in to comment.