diff --git a/commons/common-util/src/main/java/com/schbrain/common/util/StreamUtils.java b/commons/common-util/src/main/java/com/schbrain/common/util/StreamUtils.java index 05f9cc0e9cbf9404e412e87d8aa2a45bc7477ea5..8bc00ea88ba7d9ed9c631cfc6d52d0e41d6d31d2 100644 --- a/commons/common-util/src/main/java/com/schbrain/common/util/StreamUtils.java +++ b/commons/common-util/src/main/java/com/schbrain/common/util/StreamUtils.java @@ -16,51 +16,51 @@ import static java.util.stream.Collectors.*; @Slf4j public class StreamUtils { - public static List filterToList(Collection data, Predicate predicate) { + public static List filterToList(Iterable data, Predicate predicate) { return filter(data, predicate, Collectors.toList()); } - public static Set filterToSet(Collection data, Predicate predicate) { + public static Set filterToSet(Iterable data, Predicate predicate) { return filter(data, predicate, Collectors.toSet()); } - public static > C filter(Collection data, Predicate predicate, Collector collector) { - return Optional.ofNullable(data).orElse(emptyList()).stream().filter(predicate).collect(collector); + public static > C filter(Iterable data, Predicate predicate, Collector collector) { + return from(data).filter(predicate).collect(collector); } - public static List toList(Collection data, Function mapper) { + public static List toList(Iterable data, Function mapper) { return toList(data, mapper, false); } - public static List toList(Collection data, Function mapper, boolean distinct) { + public static List toList(Iterable data, Function mapper, boolean distinct) { return toList(data, mapper, distinct, false); } - public static List toList(Collection data, Function mapper, boolean distinct, boolean ignoreNull) { + public static List toList(Iterable data, Function mapper, boolean distinct, boolean ignoreNull) { return extract(data, mapper, distinct, ignoreNull, Collectors.toList()); } - public static Set toSet(Collection data, Function mapper) { + public static Set toSet(Iterable data, Function mapper) { return toSet(data, mapper, false); } - public static Set toSet(Collection data, Function mapper, boolean ignoreNull) { + public static Set toSet(Iterable data, Function mapper, boolean ignoreNull) { return extract(data, mapper, ignoreNull, false, Collectors.toSet()); } - public static Map toMap(Collection data, Function keyMapper) { + public static Map toMap(Iterable data, Function keyMapper) { return toMap(data, keyMapper, false); } - public static Map toMap(Collection data, Function keyMapper, boolean ordered) { + public static Map toMap(Iterable data, Function keyMapper, boolean ordered) { return toMap(data, keyMapper, Function.identity(), ordered); } - public static Map toMap(Collection data, Function keyMapper, Function valueMapper) { + public static Map toMap(Iterable data, Function keyMapper, Function valueMapper) { return toMap(data, keyMapper, valueMapper, false); } - public static Map toMap(Collection data, Function keyMapper, Function valueMapper, boolean ordered) { + public static Map toMap(Iterable data, Function keyMapper, Function valueMapper, boolean ordered) { Supplier> mapFactory = HashMap::new; if (ordered) { mapFactory = LinkedHashMap::new; @@ -68,74 +68,67 @@ public class StreamUtils { return toMap(data, keyMapper, valueMapper, mapFactory); } - public static > Map toMap(Collection data, Function keyMapper, Function valueMapper, Supplier mapFactory) { - return Optional.ofNullable(data) - .orElse(emptyList()) - .stream() - .collect(Collectors.toMap(keyMapper, valueMapper, (oldValue, newValue) -> { - // Could not get the key when mergeFunction invoke - log.warn("There are multiple values with the same key when toMap, return the old one"); - return oldValue; - }, mapFactory)); + public static > Map toMap(Iterable data, Function keyMapper, Function valueMapper, Supplier mapFactory) { + return from(data).collect(Collectors.toMap(keyMapper, valueMapper, (oldValue, newValue) -> { + // Could not get the key when mergeFunction invoke + log.warn("There are multiple values with the same key when toMap, return the old one"); + return oldValue; + }, mapFactory)); } - public static Map> groupBy(Collection data, Function mapper) { + public static Map> groupBy(Iterable data, Function mapper) { return groupBy(data, mapper, false); } - public static Map> groupBy(Collection data, Function keyMapper, boolean ignoreNullKey) { + public static Map> groupBy(Iterable data, Function keyMapper, boolean ignoreNullKey) { return groupBy(data, keyMapper, Collectors.toList(), ignoreNullKey); } - public static Map groupBy(Collection data, Function mapper, Collector collectors) { + public static Map groupBy(Iterable data, Function mapper, Collector collectors) { return groupBy(data, mapper, collectors, false); } - public static Map groupBy(Collection data, Function mapper, Collector collectors, boolean ignoreNullKey) { + public static Map groupBy(Iterable data, Function mapper, Collector collectors, boolean ignoreNullKey) { return groupBy(data, mapper, Function.identity(), collectors, ignoreNullKey); } - public static Map> groupBy(Collection data, Function keyMapper, Function valueMapper) { + public static Map> groupBy(Iterable data, Function keyMapper, Function valueMapper) { return groupBy(data, keyMapper, valueMapper, Collectors.toList(), false); } - public static Map> groupBy(Collection data, Function keyMapper, Function valueMapper, boolean ignoreNullKey) { + public static Map> groupBy(Iterable data, Function keyMapper, Function valueMapper, boolean ignoreNullKey) { return groupBy(data, keyMapper, valueMapper, Collectors.toList(), ignoreNullKey); } - public static Map groupBy(Collection data, Function keyMapper, Function valueMapper, Collector collector) { + public static Map groupBy(Iterable data, Function keyMapper, Function valueMapper, Collector collector) { return groupBy(data, keyMapper, valueMapper, collector, false); } - public static Map groupBy(Collection data, Function keyMapper, Function valueMapper, Collector collector, boolean ignoreNullKey) { + public static Map groupBy(Iterable data, Function keyMapper, Function valueMapper, Collector collector, boolean ignoreNullKey) { return groupBy(data, keyMapper, valueMapper, collector, ignoreNullKey, HashMap::new); } - public static Map groupBy(Collection data, Function keyMapper, Function valueMapper, Collector collector, boolean ignoreNullKey, Supplier> mapSupplier) { - Stream stream = Optional.ofNullable(data) - .orElse(emptyList()) - .stream(); + public static Map groupBy(Iterable data, Function keyMapper, Function valueMapper, Collector collector, boolean ignoreNullKey, Supplier> mapSupplier) { + Stream stream = from(data); if (ignoreNullKey) { stream = stream.filter(item -> null != keyMapper.apply(item)); } return stream.collect(groupingBy(keyMapper, mapSupplier, mapping(valueMapper, collector))); } - public static String join(Collection data, CharSequence delimiter) { + public static String join(Iterable data, CharSequence delimiter) { return join(data, delimiter, Objects::toString); } - public static String join(Collection data, CharSequence delimiter, Function toStringFunction) { + public static String join(Iterable data, CharSequence delimiter, Function toStringFunction) { return join(data, delimiter, "", "", toStringFunction); } - public static String join(Collection data, CharSequence delimiter, String prefix, String suffix, Function toStringFunction) { - return Optional.ofNullable(data) - .orElse(emptyList()) - .stream().map(toStringFunction).collect(joining(delimiter, prefix, suffix)); + public static String join(Iterable data, CharSequence delimiter, String prefix, String suffix, Function toStringFunction) { + return from(data).map(toStringFunction).collect(joining(delimiter, prefix, suffix)); } - public static R extract(Collection data, Function mapper, boolean distinct, boolean ignoreNull, Collector collector) { + public static R extract(Iterable data, Function mapper, boolean distinct, boolean ignoreNull, Collector collector) { Predicate predicate = null; if (ignoreNull) { predicate = Objects::nonNull; @@ -143,11 +136,8 @@ public class StreamUtils { return extract(data, mapper, predicate, distinct, collector); } - public static R extract(Collection data, Function mapper, Predicate predicate, boolean distinct, Collector collector) { - Stream stream = Optional.ofNullable(data) - .orElse(emptyList()) - .stream() - .map(mapper); + public static R extract(Iterable data, Function mapper, Predicate predicate, boolean distinct, Collector collector) { + Stream stream = from(data).map(mapper); if (distinct) { stream = stream.distinct(); } @@ -157,4 +147,13 @@ public class StreamUtils { return stream.collect(collector); } + public static Stream from(Iterable iterable) { + return from(iterable, false); + } + + public static Stream from(Iterable iterable, boolean parallel) { + Iterable source = Optional.ofNullable(iterable).orElse(emptyList()); + return StreamSupport.stream(source.spliterator(), parallel); + } + } \ No newline at end of file diff --git a/commons/common-util/src/main/java/com/schbrain/common/util/properties/SchbrainMapPropertySource.java b/commons/common-util/src/main/java/com/schbrain/common/util/properties/SchbrainMapPropertySource.java index 1b8c0b817dff25cf1ad37ce0c172cece12406a52..072c252b2c0531200a606ed0ec37665494805b57 100644 --- a/commons/common-util/src/main/java/com/schbrain/common/util/properties/SchbrainMapPropertySource.java +++ b/commons/common-util/src/main/java/com/schbrain/common/util/properties/SchbrainMapPropertySource.java @@ -1,8 +1,8 @@ package com.schbrain.common.util.properties; -import com.schbrain.common.util.ConfigurationPropertiesUtils; import org.springframework.core.env.MapPropertySource; +import java.util.LinkedHashMap; import java.util.Map; /** @@ -13,12 +13,12 @@ import java.util.Map; */ public class SchbrainMapPropertySource extends MapPropertySource { - public SchbrainMapPropertySource(String name, Object source) { - this(name, ConfigurationPropertiesUtils.toMap(source)); + public SchbrainMapPropertySource(String name, Map source) { + super(name, new LinkedHashMap<>(source)); } - public SchbrainMapPropertySource(String name, Map source) { - super(name, source); + public void addProperties(Map properties) { + getSource().putAll(properties); } } \ No newline at end of file diff --git a/commons/common-util/src/main/java/com/schbrain/common/util/support/ConfigurableProperties.java b/commons/common-util/src/main/java/com/schbrain/common/util/support/ConfigurableProperties.java index 7c79ec436f477f2c13e9008c1e6a437208913791..06550872c33d9624ff9712f2ebdef5cca1884602 100644 --- a/commons/common-util/src/main/java/com/schbrain/common/util/support/ConfigurableProperties.java +++ b/commons/common-util/src/main/java/com/schbrain/common/util/support/ConfigurableProperties.java @@ -2,55 +2,39 @@ package com.schbrain.common.util.support; import com.schbrain.common.util.ConfigurationPropertiesUtils; import lombok.Data; -import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.boot.context.properties.bind.BindHandler; -import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.boot.context.properties.bind.*; +import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; -import java.beans.Introspector; - /** - * WARNING!!! - *

- * If you want to use a subclass of this class before {@link BeanPostProcessor}, - * please load it through {@link com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils} - * * @author liaozan * @since 2022/1/10 */ @Data -public abstract class ConfigurableProperties { +public abstract class ConfigurableProperties implements Ordered { /** - * the namespace of remote config - */ - protected String namespace = getDefaultNamespace(); - - /** - * the prefix of properties + * get the namespace of remote config */ - protected String prefix = getPossiblePrefix(); + public abstract String getDefaultNamespace(); /** - * the name of propertySource + * bind properties */ - protected String name = Introspector.decapitalize(getClass().getSimpleName()); - - public String getDefaultNamespace() { - return "application"; - } - - @SuppressWarnings({"unchecked", "unused"}) - public T bindOrCreate(ConfigurableEnvironment environment, boolean afterMerge) { - return (T) Binder.get(environment, bindHandler()).bindOrCreate(getPrefix(), getClass()); + public ConfigurableProperties bind(ConfigurableEnvironment environment) { + return Binder.get(environment, bindHandler()).bindOrCreate(getPropertiesPrefix(), Bindable.ofInstance(this)); } - protected BindHandler bindHandler() { - return BindHandler.DEFAULT; + @Override + public int getOrder() { + return Ordered.LOWEST_PRECEDENCE; } - private String getPossiblePrefix() { + /** + * the prefix of properties + */ + protected String getPropertiesPrefix() { ConfigurationProperties annotation = getClass().getAnnotation(ConfigurationProperties.class); if (annotation == null) { String className = ConfigurationProperties.class.getName(); @@ -60,4 +44,11 @@ public abstract class ConfigurableProperties { return ConfigurationPropertiesUtils.getPrefix(getClass()); } + /** + * get the {@link org.springframework.boot.context.properties.bind.BindHandler} for bind + */ + protected BindHandler bindHandler() { + return BindHandler.DEFAULT; + } + } \ No newline at end of file diff --git a/commons/common/src/main/java/com/schbrain/common/annotation/IgnoreLogin.java b/commons/common/src/main/java/com/schbrain/common/annotation/IgnoreLogin.java index 2a3b44d1e2f8a4d07049a9f82a42cf2b8c10b976..9f06148abd4ab4b4c747614b4f37bf98d3c42805 100644 --- a/commons/common/src/main/java/com/schbrain/common/annotation/IgnoreLogin.java +++ b/commons/common/src/main/java/com/schbrain/common/annotation/IgnoreLogin.java @@ -9,10 +9,10 @@ import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface IgnoreLogin { + /** * 是否忽略登录 - * @return */ boolean ignore() default true; -} +} \ No newline at end of file diff --git a/commons/pom.xml b/commons/pom.xml index 7f5bebff376a06d190d61ad5b508365a0b14907e..bc2c96e378d88794722aeb3c1034eb40dd678f03 100644 --- a/commons/pom.xml +++ b/commons/pom.xml @@ -1,6 +1,6 @@ - 4.0.0 diff --git a/commons/web-common/src/main/resources/META-INF/spring-configuration-metadata.json b/commons/web-common/src/main/resources/META-INF/spring-configuration-metadata.json index b90d57bc6a039e50cbbaa4ccf09ed309fdc49d92..e80031b486aef43f8800e51dbae76590e70fe5a6 100644 --- a/commons/web-common/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/commons/web-common/src/main/resources/META-INF/spring-configuration-metadata.json @@ -34,21 +34,6 @@ "description": "encoding for request\/response", "sourceType": "com.schbrain.common.web.properties.WebProperties" }, - { - "name": "schbrain.web.name", - "type": "java.lang.String", - "sourceType": "com.schbrain.common.web.properties.WebProperties" - }, - { - "name": "schbrain.web.namespace", - "type": "java.lang.String", - "sourceType": "com.schbrain.common.web.properties.WebProperties" - }, - { - "name": "schbrain.web.prefix", - "type": "java.lang.String", - "sourceType": "com.schbrain.common.web.properties.WebProperties" - }, { "name": "schbrain.web.wrap-response", "type": "java.lang.Boolean", diff --git a/commons/web-common/src/main/resources/META-INF/spring.factories b/commons/web-common/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000000000000000000000000000000000..fc04be370abe63c5359943778811566a6667b2fe --- /dev/null +++ b/commons/web-common/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +com.schbrain.common.util.support.ConfigurableProperties=\ + com.schbrain.common.web.properties.WebProperties \ No newline at end of file diff --git a/integration/integration-maven-plugin/README.md b/integration/integration-maven-plugin/README.md index 51ac6654e9a766051f2cac1dbf40e62d7361101e..617cc537118d4c060312e2646d254bba17af7742 100644 --- a/integration/integration-maven-plugin/README.md +++ b/integration/integration-maven-plugin/README.md @@ -26,7 +26,7 @@ ``` -**Second** specify build-script repository. +**Second** specify build-script repository. ```.xml @@ -56,4 +56,4 @@ The completion config like this: -``` +``` \ No newline at end of file diff --git a/integration/integration-maven-plugin/pom.xml b/integration/integration-maven-plugin/pom.xml index 0c42b97e5274ed11aeee0f21e8dcfc111fcb0abc..f5f4798ba9576269d12d37bbd2e67b97013aa6c2 100644 --- a/integration/integration-maven-plugin/pom.xml +++ b/integration/integration-maven-plugin/pom.xml @@ -1,5 +1,5 @@ - 4.0.0 diff --git a/integration/pom.xml b/integration/pom.xml index c48c39278465079b189cc070fdcc73046eb17dc3..7cc1b42df0cff596ab53bf217d0e97a0d13f832c 100644 --- a/integration/pom.xml +++ b/integration/pom.xml @@ -1,6 +1,6 @@ - 4.0.0 diff --git a/pom.xml b/pom.xml index ce4204abd8d6bdbe456c6f009408d2523cf1888e..5568400be7cbb5a74b6358fec61727bd94e7ace4 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,8 @@ ${revision} ${revision} ${revision} + ${revision} + ${revision} ${revision} ${revision} ${revision} @@ -151,8 +153,13 @@ com.schbrain.framework - schbrain-spring-support - ${schbrain-spring-support.version} + apollo-spring-boot-starter + ${schbrain-apollo.version} + + + com.schbrain.framework + cache-spring-boot-starter + ${schbrain-cache.version} com.schbrain.framework @@ -161,19 +168,24 @@ com.schbrain.framework - mybatis-spring-boot-starter - ${schbrain-mybatis.version} + elasticsearch-spring-boot-starter + ${schbrain-elasticsearch.version} com.schbrain.framework - apollo-spring-boot-starter - ${schbrain-apollo.version} + kafka-spring-boot-starter + ${schbrain-kafka.version} com.schbrain.framework logger-spring-boot-starter ${schbrain-logger.version} + + com.schbrain.framework + mybatis-spring-boot-starter + ${schbrain-mybatis.version} + com.schbrain.framework oss-spring-boot-starter @@ -186,13 +198,13 @@ com.schbrain.framework - cache-spring-boot-starter - ${schbrain-cache.version} + schbrain-base-dao + ${schbrain-base-dao.version} com.schbrain.framework - schbrain-base-dao - ${schbrain-base-dao.version} + schbrain-spring-support + ${schbrain-spring-support.version} @@ -650,6 +662,32 @@ + + org.apache.maven.plugins + maven-resources-plugin + + + copy-configuration-metadata + compile + + copy-resources + + + + + ${project.build.outputDirectory}/META-INF + + spring-configuration-metadata.json + + true + + + ${project.basedir}/src/main/resources/META-INF + true + + + + org.apache.maven.plugins maven-source-plugin diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/SchbrainApolloAutoConfiguration.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloAutoConfiguration.java similarity index 68% rename from starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/SchbrainApolloAutoConfiguration.java rename to starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloAutoConfiguration.java index 40853e34a552ba6ce66323411369ed9e548e6a40..671c64b53ec5ed3f86851c40302f6a5dab2e57bb 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/SchbrainApolloAutoConfiguration.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloAutoConfiguration.java @@ -1,10 +1,8 @@ package com.schbrain.framework.autoconfigure.apollo; import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; -import com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration; import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants; import com.schbrain.framework.autoconfigure.apollo.properties.ApolloProperties; -import com.schbrain.framework.autoconfigure.apollo.properties.ConfigurationPropertiesRegistry; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -15,10 +13,10 @@ import org.springframework.context.annotation.Import; * @since 2021/11/15 */ @EnableApolloConfig -@Import({SchbrainPropertySourcesProcessor.class, ConfigurationPropertiesRegistry.class}) +@Import(ConfigurationPropertiesRegistry.class) @EnableConfigurationProperties(ApolloProperties.class) -@AutoConfiguration(before = ApolloAutoConfiguration.class) +@AutoConfiguration(before = com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration.class) @ConditionalOnProperty(value = PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, matchIfMissing = true) -public class SchbrainApolloAutoConfiguration { +public class ApolloAutoConfiguration { } \ No newline at end of file diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ApolloPropertiesPreparer.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializer.java similarity index 69% rename from starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ApolloPropertiesPreparer.java rename to starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializer.java index 0dfb7739a2bed8bdd4d6d92d9fa76ce518a39610..a9ed19406a57f4cc3a57e22751962d5d96714152 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ApolloPropertiesPreparer.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloConfigurationInitializer.java @@ -1,14 +1,13 @@ -package com.schbrain.framework.autoconfigure.apollo.properties; +package com.schbrain.framework.autoconfigure.apollo; import com.ctrip.framework.foundation.Foundation; import com.schbrain.common.util.ApplicationName; import com.schbrain.common.util.EnvUtils; -import com.schbrain.framework.support.spring.EnvironmentPostProcessorAdapter; +import com.schbrain.framework.support.spring.EnvironmentPostProcessorLoggerAwareAdapter; +import com.schbrain.framework.support.spring.defaults.DefaultPropertiesEnvironmentPostProcessor; import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.SpringApplication; -import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; import org.springframework.boot.logging.DeferredLogFactory; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.util.StringUtils; @@ -24,34 +23,37 @@ import static com.ctrip.framework.apollo.spring.config.PropertySourcesConstants. * @author liaozan * @since 2021/11/6 */ -public class ApolloPropertiesPreparer extends EnvironmentPostProcessorAdapter implements Ordered { +public class ApolloConfigurationInitializer extends EnvironmentPostProcessorLoggerAwareAdapter implements Ordered { - // get properties after configData loaded - public static final Integer ORDER = ConfigDataEnvironmentPostProcessor.ORDER + 1; - public static final String ENV_KEY = "env"; - private static final Map INIT_PROPERTIES = new LinkedHashMap<>(); + /** + * load properties after set the default properties + */ + public static final Integer DEFAULT_ORDER = DefaultPropertiesEnvironmentPostProcessor.DEFAULT_ORDER + 1; - public ApolloPropertiesPreparer(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) { + private static final String ENV_KEY = "env"; + + private static Map INIT_PROPERTIES = new LinkedHashMap<>(); + + private final ConfigurablePropertiesLoader configurablePropertiesLoader; + + public ApolloConfigurationInitializer(DeferredLogFactory deferredLogFactory, ConfigurableBootstrapContext bootstrapContext) { super(deferredLogFactory, bootstrapContext); + this.configurablePropertiesLoader = new ConfigurablePropertiesLoader(getDeferredLogFactory()); } @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { if (disabled(environment)) { - print("apollo is disabled"); + print(APOLLO_BOOTSTRAP_ENABLED + " is disabled"); return; } setRequiredProperty(environment); + configurablePropertiesLoader.load(environment, application); } @Override public int getOrder() { - return ORDER; - } - - @Override - protected void onBootstrapContextClosed(ConfigurableApplicationContext context) { - context.getBeanFactory().addBeanPostProcessor(new ConfigurablePropertiesBeanPostProcessor(context)); + return DEFAULT_ORDER; } private boolean disabled(ConfigurableEnvironment environment) { @@ -71,8 +73,11 @@ public class ApolloPropertiesPreparer extends EnvironmentPostProcessorAdapter im saveProperty(APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, true); saveProperty(APOLLO_BOOTSTRAP_ENABLED, true); + saveProperty(APOLLO_CACHE_FILE_ENABLE, true); saveProperty(APOLLO_PROPERTY_ORDER_ENABLE, true); - saveProperty(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, true); + // DO NOT set to true. After caching the property name, SpringBoot may not be able to bind the properties + saveProperty(APOLLO_PROPERTY_NAMES_CACHE_ENABLE, false); + saveProperty(APOLLO_OVERRIDE_SYSTEM_PROPERTIES, false); printProperties(); } @@ -84,6 +89,7 @@ public class ApolloPropertiesPreparer extends EnvironmentPostProcessorAdapter im private void printProperties() { INIT_PROPERTIES.forEach((k, v) -> print(k + " : " + v)); + INIT_PROPERTIES = null; } private void print(String message) { @@ -120,7 +126,11 @@ public class ApolloPropertiesPreparer extends EnvironmentPostProcessorAdapter im } private String getEnv(ConfigurableEnvironment environment) { - return EnvUtils.getProfile(environment); + String profile = EnvUtils.getProfile(environment); + if (profile == null) { + profile = EnvUtils.DEVELOPMENT; + } + return profile; } private String getAppId(ConfigurableEnvironment environment) { diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/SchbrainApolloPropertiesEnvironmentPostProcessor.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloPropertiesReorderEnvironmentPostProcessor.java similarity index 88% rename from starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/SchbrainApolloPropertiesEnvironmentPostProcessor.java rename to starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloPropertiesReorderEnvironmentPostProcessor.java index a10981dd0e226e7c76271be86931a25c104d9a1d..cf7783cc8fd942bfddc2c3afd02c361cf3776750 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/SchbrainApolloPropertiesEnvironmentPostProcessor.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ApolloPropertiesReorderEnvironmentPostProcessor.java @@ -12,7 +12,7 @@ import org.springframework.core.env.ConfigurableEnvironment; * @since 2022/4/19 */ @Order(ApolloApplicationContextInitializer.DEFAULT_ORDER + 1) -public class SchbrainApolloPropertiesEnvironmentPostProcessor implements EnvironmentPostProcessor { +public class ApolloPropertiesReorderEnvironmentPostProcessor implements EnvironmentPostProcessor { @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java new file mode 100644 index 0000000000000000000000000000000000000000..43df32d50786dd08b570bafce7f3eb52213413ba --- /dev/null +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurablePropertiesLoader.java @@ -0,0 +1,92 @@ +package com.schbrain.framework.autoconfigure.apollo; + +import cn.hutool.core.thread.GlobalThreadPool; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigService; +import com.schbrain.common.util.properties.SchbrainMapPropertySource; +import com.schbrain.common.util.support.ConfigurableProperties; +import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEvent; +import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEventListener; +import com.schbrain.framework.autoconfigure.apollo.properties.ApolloProperties; +import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.logging.Log; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.logging.DeferredLogFactory; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ApplicationEventMulticaster; +import org.springframework.context.event.SimpleApplicationEventMulticaster; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.util.ClassUtils; + +import java.util.List; + +import static org.springframework.core.io.support.SpringFactoriesLoader.loadFactories; + +/** + * @author liaozan + * @since 2023-04-29 + */ +class ConfigurablePropertiesLoader { + + /** + * the name of properties propertySource + */ + private static final String PROPERTIES_PROPERTY_SOURCE = "ConfigurablePropertiesPropertySource"; + + private final Log log; + + ConfigurablePropertiesLoader(DeferredLogFactory logFactory) { + this.log = logFactory.getLog(ConfigurablePropertiesLoader.class); + } + + void load(ConfigurableEnvironment environment, SpringApplication application) { + List configurableProperties = loadFactories(ConfigurableProperties.class, getClass().getClassLoader()); + if (CollectionUtils.isEmpty(configurableProperties)) { + log.warn("There is no configuration properties found"); + return; + } + + ApplicationEventMulticaster eventMulticaster = createEventMulticaster(application); + + ApolloProperties apolloProperties = ApolloProperties.get(environment); + + // MUST NOT use CachedCompositePropertySource + CompositePropertySource compositePropertySource = new CompositePropertySource(PROPERTIES_PROPERTY_SOURCE); + if (apolloProperties.isRemoteFirst()) { + environment.getPropertySources().addFirst(compositePropertySource); + } else { + environment.getPropertySources().addLast(compositePropertySource); + } + + configurableProperties.forEach(properties -> { + String namespace = properties.getDefaultNamespace(); + Config config = ConfigService.getConfig(namespace); + SchbrainMapPropertySource propertySource = ConfigUtils.toPropertySource(namespace, config); + if (propertySource == null) { + log.warn("No configuration properties loaded under namespace: " + namespace); + return; + } + ConfigUtils.resolvePlaceHolders(environment, propertySource); + + // early add to environment to support properties bind + compositePropertySource.addPropertySource(propertySource); + + ConfigurableProperties boundProperties = properties.bind(environment); + eventMulticaster.multicastEvent(new PropertiesPreparedEvent(boundProperties, propertySource, environment, application)); + }); + } + + private ApplicationEventMulticaster createEventMulticaster(SpringApplication application) { + SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster(); + eventMulticaster.setTaskExecutor(GlobalThreadPool.getExecutor()); + for (ApplicationListener listener : application.getListeners()) { + if (ClassUtils.isAssignableValue(PropertiesPreparedEventListener.class, listener)) { + eventMulticaster.addApplicationListener(listener); + } + } + return eventMulticaster; + } + +} \ No newline at end of file diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ConfigurationPropertiesRegistry.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurationPropertiesRegistry.java similarity index 97% rename from starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ConfigurationPropertiesRegistry.java rename to starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurationPropertiesRegistry.java index 002b1b0bc706a34fadc684e77fd667594dabc5e2..4f9fa4aaeb8e21bb9acce21d79f1087858705fb3 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ConfigurationPropertiesRegistry.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/ConfigurationPropertiesRegistry.java @@ -1,4 +1,4 @@ -package com.schbrain.framework.autoconfigure.apollo.properties; +package com.schbrain.framework.autoconfigure.apollo; import cn.hutool.core.text.StrPool; import com.ctrip.framework.apollo.spring.property.SpringValue; diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/SchbrainPropertySourcesProcessor.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/SchbrainPropertySourcesProcessor.java deleted file mode 100644 index c3b829c135cdb430587b3f509bd3038b916e6d66..0000000000000000000000000000000000000000 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/SchbrainPropertySourcesProcessor.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.schbrain.framework.autoconfigure.apollo; - -import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor; -import com.schbrain.framework.autoconfigure.apollo.util.PropertySourceOrderUtils; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.core.env.ConfigurableEnvironment; - -/** - * @author liaozan - * @since 2021/12/6 - */ -public class SchbrainPropertySourcesProcessor extends PropertySourcesProcessor { - - @Override - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { - super.postProcessBeanFactory(beanFactory); - PropertySourceOrderUtils.adjustPropertySourceOrder(beanFactory.getBean(ConfigurableEnvironment.class)); - } - -} \ No newline at end of file diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEvent.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..6a29627be2d7c20e69925432f2d7bd148c9332b6 --- /dev/null +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEvent.java @@ -0,0 +1,37 @@ +package com.schbrain.framework.autoconfigure.apollo.listener; + +import com.schbrain.common.util.properties.SchbrainMapPropertySource; +import com.schbrain.common.util.support.ConfigurableProperties; +import lombok.Getter; +import org.springframework.boot.SpringApplication; +import org.springframework.context.ApplicationEvent; +import org.springframework.core.env.ConfigurableEnvironment; + +/** + * @author liaozan + * @since 2023-04-28 + */ +@Getter +public class PropertiesPreparedEvent extends ApplicationEvent { + + private static final long serialVersionUID = 2567291189881702459L; + + private final ConfigurableEnvironment environment; + + private final SchbrainMapPropertySource propertySource; + + private final SpringApplication application; + + public PropertiesPreparedEvent(ConfigurableProperties properties, SchbrainMapPropertySource propertySource, + ConfigurableEnvironment environment, SpringApplication application) { + super(properties); + this.environment = environment; + this.propertySource = propertySource; + this.application = application; + } + + public ConfigurableProperties getConfigurableProperties() { + return (ConfigurableProperties) getSource(); + } + +} \ No newline at end of file diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEventListener.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEventListener.java new file mode 100644 index 0000000000000000000000000000000000000000..f8707b2afa4b30529316220b003c65ad5da82f99 --- /dev/null +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEventListener.java @@ -0,0 +1,11 @@ +package com.schbrain.framework.autoconfigure.apollo.listener; + +import org.springframework.context.ApplicationListener; + +/** + * @author liaozan + * @since 2023-04-29 + */ +public interface PropertiesPreparedEventListener extends ApplicationListener { + +} \ No newline at end of file diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEventListenerAdapter.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEventListenerAdapter.java new file mode 100644 index 0000000000000000000000000000000000000000..91cfeb0d2cfd786de5c5055b82e6c929bffd056e --- /dev/null +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/listener/PropertiesPreparedEventListenerAdapter.java @@ -0,0 +1,34 @@ +package com.schbrain.framework.autoconfigure.apollo.listener; + +import com.schbrain.common.util.support.ConfigurableProperties; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +/** + * @author liaozan + * @since 2023-04-28 + */ +@SuppressWarnings("unchecked") +public class PropertiesPreparedEventListenerAdapter implements PropertiesPreparedEventListener { + + private final Class propertyType; + + public PropertiesPreparedEventListenerAdapter() { + ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass(); + Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); + this.propertyType = (Class) actualTypeArguments[0]; + } + + @Override + public void onApplicationEvent(PropertiesPreparedEvent event) { + if (event.getConfigurableProperties().getClass() == propertyType) { + onPropertiesPrepared(event, (T) event.getConfigurableProperties()); + } + } + + protected void onPropertiesPrepared(PropertiesPreparedEvent event, T configurableProperties) { + + } + +} \ No newline at end of file diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ApolloProperties.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ApolloProperties.java index 1d0536dafc2c47da1e79c4547c72da5104134054..6c2455dab3f83a89778b4c6db4110c03bf27fa96 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ApolloProperties.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ApolloProperties.java @@ -25,4 +25,4 @@ public class ApolloProperties { return Binder.get(environment).bindOrCreate(PREFIX, ApolloProperties.class); } -} +} \ No newline at end of file diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ConfigurablePropertiesBeanPostProcessor.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ConfigurablePropertiesBeanPostProcessor.java deleted file mode 100644 index e51ec637525b285f2c2cf964609a8d7920d4cf75..0000000000000000000000000000000000000000 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/properties/ConfigurablePropertiesBeanPostProcessor.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.schbrain.framework.autoconfigure.apollo.properties; - -import com.schbrain.common.util.support.ConfigurableProperties; -import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils; -import com.schbrain.framework.support.spring.BeanPostProcessorAdapter; -import org.springframework.context.ConfigurableApplicationContext; - -/** - * @author liaozan - * @since 2022/9/16 - */ -public class ConfigurablePropertiesBeanPostProcessor extends BeanPostProcessorAdapter { - - public ConfigurablePropertiesBeanPostProcessor(ConfigurableApplicationContext applicationContext) { - this.setApplicationContext(applicationContext); - } - - @Override - protected ConfigurableProperties doPostProcessBeforeInstantiation(Class beanClass) { - return ConfigUtils.loadConfig(environment, beanClass); - } - -} \ No newline at end of file diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/ConfigUtils.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/ConfigUtils.java index c546f8d68d9f2392b2aa03253054f1bfbeb3e19b..d77f0c52e05936606b1cd56e387dee3144fb1bc1 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/ConfigUtils.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/ConfigUtils.java @@ -1,111 +1,38 @@ package com.schbrain.framework.autoconfigure.apollo.util; import com.ctrip.framework.apollo.Config; -import com.ctrip.framework.apollo.ConfigService; -import com.ctrip.framework.apollo.enums.ConfigSourceType; -import com.schbrain.common.util.ConfigurationPropertiesUtils; +import com.google.common.collect.Maps; import com.schbrain.common.util.properties.SchbrainMapPropertySource; -import com.schbrain.common.util.support.ConfigurableProperties; -import com.schbrain.framework.autoconfigure.apollo.properties.ApolloProperties; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.beans.BeanUtils; -import org.springframework.boot.DefaultPropertiesPropertySource; -import org.springframework.boot.context.properties.source.ConfigurationPropertySources; -import org.springframework.core.env.*; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.env.ConfigurableEnvironment; -import java.util.LinkedHashMap; +import javax.annotation.Nullable; import java.util.Map; import java.util.Map.Entry; - -import static com.ctrip.framework.apollo.core.ApolloClientSystemConsts.APP_ID; +import java.util.Set; /** * @author liaozan * @since 2021/12/6 */ +@Slf4j public class ConfigUtils { - private static final Log LOGGER = LogFactory.getLog(ConfigUtils.class); - - private static final boolean APOLLO_DISABLED = System.getProperty(APP_ID) == null; - - public static boolean isApolloDisabled() { - return APOLLO_DISABLED; - } - - public static T loadConfig(ConfigurableEnvironment environment, Class propertyClass) { - // load default and local properties - T target = BeanUtils.instantiateClass(propertyClass).bindOrCreate(environment, false); - Map defaultProperties = ConfigurationPropertiesUtils.toMap(target); - // load remote config - Map loadedProperties = loadConfig(target.getNamespace(), target.getPrefix()); - // merge - ApolloProperties apolloProperties = ApolloProperties.get(environment); - Map mergedProperties = mergeProperties(loadedProperties, defaultProperties, apolloProperties.isRemoteFirst()); - // add to environment - addToEnvironment(environment, target.getName(), mergedProperties); - // rebind after addToEnvironment - return target.bindOrCreate(environment, true); - } - - public static Map loadConfig(String namespace) { - return loadConfig(namespace, null); - } - - public static Map loadConfig(String namespace, String prefix) { - if (isApolloDisabled()) { - return new LinkedHashMap<>(); - } - Config config = ConfigService.getConfig(namespace); - if (config.getSourceType() == ConfigSourceType.LOCAL) { - LOGGER.warn(String.format("Failed to get config from Apollo namespace: %s, will use the local cache value", namespace)); - } - - Map configs = new LinkedHashMap<>(); - for (String propertyName : config.getPropertyNames()) { - if (prefix != null) { - if (!propertyName.startsWith(prefix)) { - continue; - } - } - String propertyValue = config.getProperty(propertyName, null); - configs.put(propertyName, propertyValue); + @Nullable + public static SchbrainMapPropertySource toPropertySource(String name, Config config) { + Set propertyNames = config.getPropertyNames(); + if (propertyNames.isEmpty()) { + return null; } - return configs; - } - - public static void addToEnvironment(ConfigurableEnvironment environment, T properties) { - addToEnvironment(environment, new SchbrainMapPropertySource(properties.getName(), properties)); - } - - public static void addToEnvironment(ConfigurableEnvironment environment, String name, Map properties) { - addToEnvironment(environment, new SchbrainMapPropertySource(name, properties)); - } - - public static void addToEnvironment(ConfigurableEnvironment environment, MapPropertySource propertySource) { - MutablePropertySources propertySources = environment.getPropertySources(); - String propertySourceName = propertySource.getName(); - - boolean alreadyExist = propertySources.contains(propertySourceName); - if (alreadyExist) { - PropertySource existing = propertySources.get(propertySourceName); - if (existing instanceof MapPropertySource) { - Map existingSource = ((MapPropertySource) existing).getSource(); - existingSource.putAll(propertySource.getSource()); - } else { - LOGGER.warn("Existing propertySource is not an instance of MapPropertySource, overwrite existing..."); - propertySources.replace(propertySourceName, propertySource); - } - } else { - propertySources.addLast(propertySource); + Map configs = Maps.newLinkedHashMapWithExpectedSize(propertyNames.size()); + for (String propertyName : propertyNames) { + String property = config.getProperty(propertyName, null); + configs.put(propertyName, property); } - resolvePlaceHolders(environment, propertySource); - ConfigurationPropertySources.attach(environment); - DefaultPropertiesPropertySource.moveToEnd(environment); + return new SchbrainMapPropertySource(name, configs); } - public static void resolvePlaceHolders(ConfigurableEnvironment environment, MapPropertySource propertySource) { + public static void resolvePlaceHolders(ConfigurableEnvironment environment, SchbrainMapPropertySource propertySource) { Map source = propertySource.getSource(); for (Entry entry : source.entrySet()) { Object value = entry.getValue(); @@ -116,18 +43,4 @@ public class ConfigUtils { } } - private static Map mergeProperties(Map loadedProperties, - Map defaultProperties, - boolean remoteFirst) { - Map mergedProperties; - if (remoteFirst) { - defaultProperties.putAll(loadedProperties); - mergedProperties = defaultProperties; - } else { - loadedProperties.putAll(defaultProperties); - mergedProperties = loadedProperties; - } - return mergedProperties; - } - } \ No newline at end of file diff --git a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/PropertySourceOrderUtils.java b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/PropertySourceOrderUtils.java index a15af5cee72a9b380f5fceeb7699bf44df279ea0..64c6a1835d5bfa76691ae7909bc71593029d6376 100644 --- a/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/PropertySourceOrderUtils.java +++ b/starters/apollo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/apollo/util/PropertySourceOrderUtils.java @@ -1,10 +1,9 @@ package com.schbrain.framework.autoconfigure.apollo.util; import com.schbrain.framework.autoconfigure.apollo.properties.ApolloProperties; +import org.apache.commons.collections4.CollectionUtils; import org.springframework.boot.DefaultPropertiesPropertySource; -import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.core.env.*; -import org.springframework.util.CollectionUtils; import java.util.List; import java.util.Set; @@ -47,7 +46,6 @@ public class PropertySourceOrderUtils { // Make sure the default configurations always in the end DefaultPropertiesPropertySource.moveToEnd(environment); - ConfigurationPropertySources.attach(environment); } } \ No newline at end of file diff --git a/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring.factories index 4d43360da8a95c6ca17a07eda5dc4cba79654449..955c229eaef25503e95ec8f4dda39019f3770f7c 100644 --- a/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1,3 +1,3 @@ org.springframework.boot.env.EnvironmentPostProcessor=\ - com.schbrain.framework.autoconfigure.apollo.properties.ApolloPropertiesPreparer,\ - com.schbrain.framework.autoconfigure.apollo.SchbrainApolloPropertiesEnvironmentPostProcessor \ No newline at end of file + com.schbrain.framework.autoconfigure.apollo.ApolloConfigurationInitializer,\ + com.schbrain.framework.autoconfigure.apollo.ApolloPropertiesReorderEnvironmentPostProcessor \ No newline at end of file diff --git a/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index a7f3cb2fc05b6d2b96a05f459229bffd5093c3e3..cbbf227dfcad51ec939d0d3d41644b82ca581b04 100644 --- a/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/starters/apollo-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1 @@ -com.schbrain.framework.autoconfigure.apollo.SchbrainApolloAutoConfiguration \ No newline at end of file +com.schbrain.framework.autoconfigure.apollo.ApolloAutoConfiguration \ No newline at end of file diff --git a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/SchbrainCacheAutoConfiguration.java b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/CacheAutoConfiguration.java similarity index 79% rename from starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/SchbrainCacheAutoConfiguration.java rename to starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/CacheAutoConfiguration.java index cf6800f53b1ced5e68c4d749a6170051fff1738e..6056e849f5ddcb3dd450c4f42608b09b7750d894 100644 --- a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/SchbrainCacheAutoConfiguration.java +++ b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/CacheAutoConfiguration.java @@ -3,7 +3,7 @@ package com.schbrain.framework.autoconfigure.cache; import com.schbrain.framework.autoconfigure.cache.properties.CacheProperties; import com.schbrain.framework.autoconfigure.cache.provider.CacheProvider; import com.schbrain.framework.autoconfigure.cache.provider.CacheProviderDelegate; -import com.schbrain.framework.autoconfigure.cache.provider.redis.SchbrainRedisCacheConfiguration; +import com.schbrain.framework.autoconfigure.cache.provider.redis.RedisCacheConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; @@ -18,15 +18,14 @@ import org.springframework.core.env.Environment; * @author zhuyf * @since 2022/7/25 */ +@Import(RedisCacheConfiguration.class) @AutoConfiguration(after = RedisAutoConfiguration.class) @EnableConfigurationProperties(CacheProperties.class) -@Import(SchbrainRedisCacheConfiguration.class) -public class SchbrainCacheAutoConfiguration { +public class CacheAutoConfiguration { @Bean @ConditionalOnBean(CacheProvider.class) - public CacheProviderDelegate cacheServiceDelegate(CacheProvider cacheProvider, CacheProperties cacheProperties, - Environment environment) { + public CacheProvider cacheProvider(CacheProvider cacheProvider, CacheProperties cacheProperties, Environment environment) { CacheProviderDelegate delegate = new CacheProviderDelegate(cacheProperties, cacheProvider, environment); CacheUtils.setCacheProvider(delegate); return delegate; diff --git a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/properties/CacheProperties.java b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/properties/CacheProperties.java index 7eca43c63003f5ccec30b4338a48bdd75304c043..f9994bb390b3a6ed6d4a412507244283b1944a8e 100644 --- a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/properties/CacheProperties.java +++ b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/properties/CacheProperties.java @@ -1,6 +1,8 @@ package com.schbrain.framework.autoconfigure.cache.properties; +import com.schbrain.common.util.support.ConfigurableProperties; import lombok.Data; +import lombok.EqualsAndHashCode; import org.springframework.boot.context.properties.ConfigurationProperties; /** @@ -8,8 +10,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * @since 2022/7/26 */ @Data +@EqualsAndHashCode(callSuper = true) @ConfigurationProperties(prefix = "schbrain.cache") -public class CacheProperties { +public class CacheProperties extends ConfigurableProperties { /** * cache prefix @@ -24,4 +27,9 @@ public class CacheProperties { */ private boolean appendPrefix = true; + @Override + public String getDefaultNamespace() { + return "cache-common"; + } + } \ No newline at end of file diff --git a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/SchbrainRedisCacheConfiguration.java b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCacheConfiguration.java similarity index 57% rename from starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/SchbrainRedisCacheConfiguration.java rename to starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCacheConfiguration.java index e3bebe1043d95d6649c028382c63d3d3094ae66b..67a877d63f58a3d4021aaeb9f0d4f6433136a213 100644 --- a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/SchbrainRedisCacheConfiguration.java +++ b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCacheConfiguration.java @@ -1,7 +1,9 @@ package com.schbrain.framework.autoconfigure.cache.provider.redis; import com.schbrain.framework.autoconfigure.cache.provider.CacheProvider; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.*; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.StringRedisTemplate; @@ -13,13 +15,14 @@ import org.springframework.data.redis.core.StringRedisTemplate; * @since 2022/8/7 */ @ConditionalOnClass(RedisConnectionFactory.class) -public class SchbrainRedisCacheConfiguration { +@EnableConfigurationProperties(RedisProperties.class) +public class RedisCacheConfiguration { @Bean @ConditionalOnBean(RedisConnectionFactory.class) @ConditionalOnMissingBean(RedisCacheProvider.class) - public CacheProvider schbrainRedisCacheProvider(RedisConnectionFactory redisConnectionFactory) { - StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory); + public CacheProvider redisCacheProvider(RedisConnectionFactory redisConnectionFactory, ObjectProvider redisTemplate) { + StringRedisTemplate stringRedisTemplate = redisTemplate.getIfAvailable(() -> new StringRedisTemplate(redisConnectionFactory)); return new RedisCacheProvider(stringRedisTemplate); } diff --git a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisProperties.java b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisProperties.java new file mode 100644 index 0000000000000000000000000000000000000000..2c1d8821890816baa8846d7e5a3c4d4aed9d1d29 --- /dev/null +++ b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisProperties.java @@ -0,0 +1,22 @@ +package com.schbrain.framework.autoconfigure.cache.provider.redis; + +import com.schbrain.common.util.support.ConfigurableProperties; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * @author liaozan + * @since 2023-04-27 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@ConfigurationProperties(prefix = "spring.redis") +public class RedisProperties extends ConfigurableProperties { + + @Override + public String getDefaultNamespace() { + return "redis-common"; + } + +} \ No newline at end of file diff --git a/starters/cache-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json b/starters/cache-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json index 8365f8e9f5140dfdc6bf297c9389c13c7cbc4de7..c77cb88f8e5325a3e56b8a03ab2b2023c430196b 100644 --- a/starters/cache-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/starters/cache-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json @@ -4,6 +4,11 @@ "name": "schbrain.cache", "type": "com.schbrain.framework.autoconfigure.cache.properties.CacheProperties", "sourceType": "com.schbrain.framework.autoconfigure.cache.properties.CacheProperties" + }, + { + "name": "spring.redis", + "type": "com.schbrain.framework.autoconfigure.cache.provider.redis.RedisProperties", + "sourceType": "com.schbrain.framework.autoconfigure.cache.provider.redis.RedisProperties" } ], "properties": [ diff --git a/starters/cache-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starters/cache-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000000000000000000000000000000000..d1ed64169a69450a7f5fab3b1fb2e4de6ef493d5 --- /dev/null +++ b/starters/cache-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +com.schbrain.common.util.support.ConfigurableProperties=\ + com.schbrain.framework.autoconfigure.cache.properties.CacheProperties,\ + com.schbrain.framework.autoconfigure.cache.provider.redis.RedisProperties \ No newline at end of file diff --git a/starters/cache-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/starters/cache-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 49d430153d767ed5f7dd192ee1d3270e8cd5ff9c..12c43bb73c1fec5e8fdf6274f6ada1a9d1638683 100644 --- a/starters/cache-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/starters/cache-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1 @@ -com.schbrain.framework.autoconfigure.cache.SchbrainCacheAutoConfiguration \ No newline at end of file +com.schbrain.framework.autoconfigure.cache.CacheAutoConfiguration \ No newline at end of file diff --git a/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/listener/DubboPropertiesPreparedEventListener.java b/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/listener/DubboPropertiesPreparedEventListener.java new file mode 100644 index 0000000000000000000000000000000000000000..dc44c56b0e3ea6448dec0c1b4b0cdc78c2f3826d --- /dev/null +++ b/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/listener/DubboPropertiesPreparedEventListener.java @@ -0,0 +1,55 @@ +package com.schbrain.framework.autoconfigure.dubbo.listener; + +import com.alibaba.fastjson2.JSONFactory; +import com.google.common.collect.Maps; +import com.schbrain.common.util.ApplicationName; +import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEvent; +import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEventListenerAdapter; +import com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; +import org.apache.dubbo.config.spring.ConfigCenterBean; +import org.apache.dubbo.config.spring.util.EnvironmentUtils; +import org.springframework.boot.SpringApplication; +import org.springframework.core.env.ConfigurableEnvironment; + +import java.util.Map; +import java.util.SortedMap; + +import static org.apache.dubbo.config.ConfigKeys.DUBBO_SCAN_BASE_PACKAGES; + +/** + * @author liaozan + * @since 2023-04-28 + */ +public class DubboPropertiesPreparedEventListener extends PropertiesPreparedEventListenerAdapter { + + public static final String DUBBO_APPLICATION_NAME = "dubbo.application.name"; + + @Override + protected void onPropertiesPrepared(PropertiesPreparedEvent event, DubboProperties properties) { + ConfigurableEnvironment environment = event.getEnvironment(); + Map requiredProperties = collectRequiredProperties(environment, event.getApplication()); + event.getPropertySource().addProperties(requiredProperties); + injectDubboProperties(environment); + } + + private void injectDubboProperties(ConfigurableEnvironment environment) { + JSONFactory.setUseJacksonAnnotation(false); + SortedMap dubboProperties = EnvironmentUtils.filterDubboProperties(environment); + ConfigCenterBean configCenterBean = new ConfigCenterBean(); + configCenterBean.setExternalConfig(dubboProperties); + DubboBootstrap.getInstance().configCenter(configCenterBean); + } + + private Map collectRequiredProperties(ConfigurableEnvironment environment, SpringApplication application) { + Map dubboRequiredProperties = Maps.newLinkedHashMapWithExpectedSize(2); + dubboRequiredProperties.put(DUBBO_SCAN_BASE_PACKAGES, getBasePackage(application)); + dubboRequiredProperties.put(DUBBO_APPLICATION_NAME, ApplicationName.get(environment)); + return dubboRequiredProperties; + } + + private String getBasePackage(SpringApplication application) { + return application.getMainApplicationClass().getPackage().getName(); + } + +} \ No newline at end of file diff --git a/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/properties/DubboProperties.java b/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/properties/DubboProperties.java index 9353d17f920a9287523a950e8d57607880d57b2a..19c8581a757c37eccf39b30094691cd3bdb6be3c 100644 --- a/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/properties/DubboProperties.java +++ b/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/properties/DubboProperties.java @@ -3,12 +3,7 @@ package com.schbrain.framework.autoconfigure.dubbo.properties; import com.schbrain.common.util.support.ConfigurableProperties; import lombok.Data; import lombok.EqualsAndHashCode; -import org.apache.dubbo.config.spring.util.EnvironmentUtils; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.core.env.ConfigurableEnvironment; - -import java.util.LinkedHashMap; -import java.util.Map; /** * used to fetch dubbo.* config from remote @@ -21,27 +16,9 @@ import java.util.Map; @ConfigurationProperties(prefix = "dubbo") public class DubboProperties extends ConfigurableProperties { - private Map externalConfigurations; - @Override public String getDefaultNamespace() { return "dubbo-common"; } - @Override - @SuppressWarnings("unchecked") - public DubboProperties bindOrCreate(ConfigurableEnvironment environment, boolean afterMerge) { - DubboProperties dubboProperties = super.bindOrCreate(environment, afterMerge); - if (afterMerge) { - Map externalConfigurations = new LinkedHashMap<>(EnvironmentUtils.filterDubboProperties(environment)); - Map configuredProperties = dubboProperties.getExternalConfigurations(); - if (configuredProperties == null) { - configuredProperties = new LinkedHashMap<>(); - } - externalConfigurations.putAll(configuredProperties); - dubboProperties.setExternalConfigurations(externalConfigurations); - } - return dubboProperties; - } - } \ No newline at end of file diff --git a/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/properties/DubboPropertiesPreparer.java b/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/properties/DubboPropertiesPreparer.java deleted file mode 100644 index 52beb3d5ea6e3cf8a3d6842e5413863a064b2de4..0000000000000000000000000000000000000000 --- a/starters/dubbo-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/dubbo/properties/DubboPropertiesPreparer.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.schbrain.framework.autoconfigure.dubbo.properties; - -import com.alibaba.fastjson2.JSONFactory; -import com.schbrain.common.exception.BaseException; -import com.schbrain.common.util.ApplicationName; -import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils; -import org.apache.dubbo.config.bootstrap.DubboBootstrap; -import org.apache.dubbo.config.spring.ConfigCenterBean; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.env.EnvironmentPostProcessor; -import org.springframework.core.Ordered; -import org.springframework.core.env.ConfigurableEnvironment; - -import java.util.LinkedHashMap; -import java.util.Map; - -import static org.apache.dubbo.config.ConfigKeys.DUBBO_SCAN_BASE_PACKAGES; - -/** - * @author liaozan - * @since 2021/10/10 - */ -public class DubboPropertiesPreparer implements EnvironmentPostProcessor, Ordered { - - public static final String DUBBO_APPLICATION_NAME = "dubbo.application.name"; - - public static final Integer DEFAULT_ORDER = Ordered.LOWEST_PRECEDENCE; - - @Override - public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { - DubboProperties dubboProperties = ConfigUtils.loadConfig(environment, DubboProperties.class); - addAdditionalProperties(environment, application, dubboProperties); - setUpConfigCenter(dubboProperties); - JSONFactory.setUseJacksonAnnotation(false); - } - - @Override - public int getOrder() { - return DEFAULT_ORDER; - } - - private void setUpConfigCenter(DubboProperties dubboProperties) { - ConfigCenterBean configCenterConfig = buildConfigCenter(dubboProperties); - DubboBootstrap.getInstance().configCenter(configCenterConfig); - } - - private ConfigCenterBean buildConfigCenter(DubboProperties dubboProperties) { - ConfigCenterBean configCenter = new ConfigCenterBean(); - configCenter.setExternalConfig(dubboProperties.getExternalConfigurations()); - return configCenter; - } - - private void addAdditionalProperties(ConfigurableEnvironment environment, SpringApplication application, DubboProperties dubboProperties) { - Map configurations = dubboProperties.getExternalConfigurations(); - configurations.put(DUBBO_SCAN_BASE_PACKAGES, getBasePackage(application)); - if (!configurations.containsKey(DUBBO_APPLICATION_NAME)) { - configurations.put(DUBBO_APPLICATION_NAME, ApplicationName.get(environment)); - } - Map properties = new LinkedHashMap<>(dubboProperties.getExternalConfigurations()); - ConfigUtils.addToEnvironment(environment, dubboProperties.getName(), properties); - } - - private String getBasePackage(SpringApplication application) { - return application.getAllSources() - .stream() - .filter(Class.class::isInstance) - .map(source -> (Class) source) - .map(Class::getPackage) - .map(Package::getName) - .findFirst() - .orElseThrow(() -> new BaseException("should never go here")); - } - -} \ No newline at end of file diff --git a/starters/dubbo-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json b/starters/dubbo-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json index 178c182a2a290efc81b6e25ff55e581c42e8153c..1d76dfde8fa51c69152796d959a5db8bd033ff84 100644 --- a/starters/dubbo-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/starters/dubbo-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json @@ -6,27 +6,6 @@ "sourceType": "com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties" } ], - "properties": [ - { - "name": "dubbo.external-configurations", - "type": "java.util.Map", - "sourceType": "com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties" - }, - { - "name": "dubbo.name", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties" - }, - { - "name": "dubbo.namespace", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties" - }, - { - "name": "dubbo.prefix", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties" - } - ], + "properties": [], "hints": [] } \ No newline at end of file diff --git a/starters/dubbo-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starters/dubbo-spring-boot-starter/src/main/resources/META-INF/spring.factories index 8ec6885efb5177ed8d83e87a85da493eaedb4184..f5fa98c5b43e63dba49816c5fa265e070c4e76da 100644 --- a/starters/dubbo-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/starters/dubbo-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1 +1,4 @@ -org.springframework.boot.env.EnvironmentPostProcessor=com.schbrain.framework.autoconfigure.dubbo.properties.DubboPropertiesPreparer \ No newline at end of file +com.schbrain.common.util.support.ConfigurableProperties=\ + com.schbrain.framework.autoconfigure.dubbo.properties.DubboProperties +org.springframework.context.ApplicationListener=\ + com.schbrain.framework.autoconfigure.dubbo.listener.DubboPropertiesPreparedEventListener \ No newline at end of file diff --git a/starters/elasticsearch-spring-boot-starter/pom.xml b/starters/elasticsearch-spring-boot-starter/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..577e71bff14334ec2cf0c11d0e03d0c4f6ca3ed9 --- /dev/null +++ b/starters/elasticsearch-spring-boot-starter/pom.xml @@ -0,0 +1,27 @@ + + + + 4.0.0 + + + com.schbrain.framework + starters + ${revision} + + + elasticsearch-spring-boot-starter + + + + com.schbrain.framework + apollo-spring-boot-starter + + + org.springframework.boot + spring-boot-starter-data-elasticsearch + + + + \ No newline at end of file diff --git a/starters/elasticsearch-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/elasticsearch/ElasticsearchAutoConfiguration.java b/starters/elasticsearch-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/elasticsearch/ElasticsearchAutoConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..7f5abc065df6f918bd87b92ebaa8da8e01856d31 --- /dev/null +++ b/starters/elasticsearch-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/elasticsearch/ElasticsearchAutoConfiguration.java @@ -0,0 +1,15 @@ +package com.schbrain.framework.autoconfigure.elasticsearch; + +import com.schbrain.framework.autoconfigure.elasticsearch.properties.ElasticsearchProperties; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; + +/** + * @author liaozan + * @since 2023-04-29 + */ +@AutoConfiguration +@EnableConfigurationProperties(ElasticsearchProperties.class) +public class ElasticsearchAutoConfiguration { + +} \ No newline at end of file diff --git a/starters/elasticsearch-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/elasticsearch/properties/ElasticsearchProperties.java b/starters/elasticsearch-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/elasticsearch/properties/ElasticsearchProperties.java new file mode 100644 index 0000000000000000000000000000000000000000..0c725f73d1065d675821f9a686c7af52d7281e33 --- /dev/null +++ b/starters/elasticsearch-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/elasticsearch/properties/ElasticsearchProperties.java @@ -0,0 +1,22 @@ +package com.schbrain.framework.autoconfigure.elasticsearch.properties; + +import com.schbrain.common.util.support.ConfigurableProperties; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * @author liaozan + * @since 2023-04-29 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@ConfigurationProperties(prefix = "spring.elasticsearch") +public class ElasticsearchProperties extends ConfigurableProperties { + + @Override + public String getDefaultNamespace() { + return "elasticsearch-common"; + } + +} \ No newline at end of file diff --git a/starters/elasticsearch-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json b/starters/elasticsearch-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json new file mode 100644 index 0000000000000000000000000000000000000000..ce3c98a1d0042d308346f54129bda172a226ebcc --- /dev/null +++ b/starters/elasticsearch-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json @@ -0,0 +1,11 @@ +{ + "groups": [ + { + "name": "spring.elasticsearch", + "type": "com.schbrain.framework.autoconfigure.elasticsearch.properties.ElasticsearchProperties", + "sourceType": "com.schbrain.framework.autoconfigure.elasticsearch.properties.ElasticsearchProperties" + } + ], + "properties": [], + "hints": [] +} \ No newline at end of file diff --git a/starters/elasticsearch-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starters/elasticsearch-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000000000000000000000000000000000..b88facaee003b4abe9b20f00e4dbf114285dbf72 --- /dev/null +++ b/starters/elasticsearch-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +com.schbrain.common.util.support.ConfigurableProperties=\ + com.schbrain.framework.autoconfigure.elasticsearch.properties.ElasticsearchProperties \ No newline at end of file diff --git a/starters/elasticsearch-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/starters/elasticsearch-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000000000000000000000000000000000000..100ee2b678c4626ee38ef5a2cff9fcb913e7aac9 --- /dev/null +++ b/starters/elasticsearch-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.schbrain.framework.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration \ No newline at end of file diff --git a/starters/kafka-spring-boot-starter/pom.xml b/starters/kafka-spring-boot-starter/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..fa5e6495d04ff9ed361c209fe49d6984c109bf15 --- /dev/null +++ b/starters/kafka-spring-boot-starter/pom.xml @@ -0,0 +1,27 @@ + + + + 4.0.0 + + + com.schbrain.framework + starters + ${revision} + + + kafka-spring-boot-starter + + + + com.schbrain.framework + apollo-spring-boot-starter + + + org.springframework.kafka + spring-kafka + + + + \ No newline at end of file diff --git a/starters/kafka-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/kafka/KafkaAutoConfiguration.java b/starters/kafka-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/kafka/KafkaAutoConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..7e8e10e5c26ea467f84b2e06d2789df5e7a4de82 --- /dev/null +++ b/starters/kafka-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/kafka/KafkaAutoConfiguration.java @@ -0,0 +1,15 @@ +package com.schbrain.framework.autoconfigure.kafka; + +import com.schbrain.framework.autoconfigure.kafka.properties.KafkaProperties; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; + +/** + * @author liaozan + * @since 2023-04-29 + */ +@AutoConfiguration +@EnableConfigurationProperties(KafkaProperties.class) +public class KafkaAutoConfiguration { + +} \ No newline at end of file diff --git a/starters/kafka-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/kafka/properties/KafkaProperties.java b/starters/kafka-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/kafka/properties/KafkaProperties.java new file mode 100644 index 0000000000000000000000000000000000000000..f0b2098187e942a0738fab1d8192eaeab50057b7 --- /dev/null +++ b/starters/kafka-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/kafka/properties/KafkaProperties.java @@ -0,0 +1,22 @@ +package com.schbrain.framework.autoconfigure.kafka.properties; + +import com.schbrain.common.util.support.ConfigurableProperties; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * @author liaozan + * @since 2023-04-29 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@ConfigurationProperties(prefix = "spring.kafka") +public class KafkaProperties extends ConfigurableProperties { + + @Override + public String getDefaultNamespace() { + return "kafka-common"; + } + +} \ No newline at end of file diff --git a/starters/kafka-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json b/starters/kafka-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json new file mode 100644 index 0000000000000000000000000000000000000000..bc8f2e1e145b88269e6d841ca05091ba8fa0958a --- /dev/null +++ b/starters/kafka-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json @@ -0,0 +1,11 @@ +{ + "groups": [ + { + "name": "spring.kafka", + "type": "com.schbrain.framework.autoconfigure.kafka.properties.KafkaProperties", + "sourceType": "com.schbrain.framework.autoconfigure.kafka.properties.KafkaProperties" + } + ], + "properties": [], + "hints": [] +} \ No newline at end of file diff --git a/starters/kafka-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starters/kafka-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000000000000000000000000000000000..d46ece18a423f2e78547f134c107acdd9169e134 --- /dev/null +++ b/starters/kafka-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +com.schbrain.common.util.support.ConfigurableProperties=\ + com.schbrain.framework.autoconfigure.kafka.properties.KafkaProperties \ No newline at end of file diff --git a/starters/kafka-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/starters/kafka-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000000000000000000000000000000000000..bd0b7edeb86a2973ad849e4f2652433b7393b0a3 --- /dev/null +++ b/starters/kafka-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.schbrain.framework.autoconfigure.kafka.KafkaAutoConfiguration \ No newline at end of file diff --git a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/LoggerAutoConfiguration.java b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/LoggerAutoConfiguration.java index ed9c4bd1cdb67eb5cc28770656d104160528ea0c..ffa870bd821ee179b4dd2bb84aaa30db78dde8e7 100644 --- a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/LoggerAutoConfiguration.java +++ b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/LoggerAutoConfiguration.java @@ -1,7 +1,7 @@ package com.schbrain.framework.autoconfigure.logger; import com.schbrain.framework.autoconfigure.logger.apollo.DynamicLoggerConfiguration; -import com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties; +import com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -15,7 +15,7 @@ import org.springframework.core.Ordered; @AutoConfiguration @Import(DynamicLoggerConfiguration.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) -@EnableConfigurationProperties(LoggingFileProperties.class) +@EnableConfigurationProperties(LoggerProperties.class) public class LoggerAutoConfiguration { } \ No newline at end of file diff --git a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/JsonLoggerInitializer.java b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/LoggerConfigurationInitializer.java similarity index 75% rename from starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/JsonLoggerInitializer.java rename to starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/LoggerConfigurationInitializer.java index 22226e9e1922386dcb98046ad78154d5aee1cca9..10e38e2705d051856e30364916f9d09fb371f01b 100644 --- a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/JsonLoggerInitializer.java +++ b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/LoggerConfigurationInitializer.java @@ -9,10 +9,8 @@ import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; import cn.hutool.json.JSONObject; import com.schbrain.common.util.*; import com.schbrain.common.util.InetUtils.HostInfo; -import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils; -import com.schbrain.framework.autoconfigure.logger.logstash.SchbrainLogstashEncoder; -import com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties; -import lombok.Setter; +import com.schbrain.framework.autoconfigure.logger.logstash.EnhancedLogstashEncoder; +import com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties; import lombok.extern.slf4j.Slf4j; import net.logstash.logback.appender.LogstashTcpSocketAppender; import net.logstash.logback.encoder.LogstashEncoder; @@ -20,8 +18,6 @@ import net.logstash.logback.fieldnames.ShortenedFieldNames; import org.apache.commons.collections4.IteratorUtils; import org.slf4j.LoggerFactory; import org.springframework.boot.cloud.CloudPlatform; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; @@ -37,25 +33,23 @@ import java.util.List; * @since 2021/12/11 */ @Slf4j -@Setter -public class JsonLoggerInitializer implements ApplicationContextInitializer { - - /** - * 暴露 set 方法以便单独启动日志服务时进行配置 - */ - private ConfigurableEnvironment environment; - private LoggingFileProperties loggingFileProperties; - private String applicationName; - - @Override - public void initialize(ConfigurableApplicationContext applicationContext) { - this.environment = applicationContext.getEnvironment(); - this.loggingFileProperties = ConfigUtils.loadConfig(environment, LoggingFileProperties.class); +public class LoggerConfigurationInitializer { + + private final ConfigurableEnvironment environment; + private final LoggerProperties properties; + private final String applicationName; + + public LoggerConfigurationInitializer(ConfigurableEnvironment environment, LoggerProperties properties) { + this.environment = environment; + this.properties = properties; this.applicationName = ApplicationName.get(environment); this.init(); } public void init() { + if (properties == null) { + return; + } LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); for (Logger logger : context.getLoggerList()) { registerAppender(logger, context); @@ -68,18 +62,18 @@ public class JsonLoggerInitializer implements ApplicationContextInitializer appender = buildFileAppender(context); logger.addAppender(appender); } - if (loggingFileProperties.isEnableJsonConsoleOutput()) { + if (properties.isEnableJsonConsoleOutput()) { Appender appender = buildConsoleAppender(context); logger.addAppender(appender); } - if (loggingFileProperties.isEnableJsonLogWriteToLogstash() || EnvUtils.runningOnCloudPlatform(environment)) { - if (!StringUtils.hasText(loggingFileProperties.getLogstashAddress())) { + if (properties.isEnableJsonLogWriteToLogstash() || EnvUtils.runningOnCloudPlatform(environment)) { + if (!StringUtils.hasText(properties.getLogstashAddress())) { log.warn("logstash address is unset, will NOT write log to logstash"); return; } @@ -91,7 +85,7 @@ public class JsonLoggerInitializer implements ApplicationContextInitializer buildLogstashAppender(LoggerContext context) { LogstashTcpSocketAppender appender = new LogstashTcpSocketAppender(); appender.setContext(context); - appender.addDestination(loggingFileProperties.getLogstashAddress()); + appender.addDestination(properties.getLogstashAddress()); appender.setEncoder(createJsonEncoder(context)); appender.start(); return appender; @@ -110,7 +104,7 @@ public class JsonLoggerInitializer implements ApplicationContextInitializer createRollingPolicy(Context context, FileAppender appender) { TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy<>(); - rollingPolicy.setMaxHistory(loggingFileProperties.getMaxHistory()); + rollingPolicy.setMaxHistory(properties.getMaxHistory()); rollingPolicy.setFileNamePattern(getPathLocation("json/json-%d{yyyy-MM-dd}.log")); rollingPolicy.setContext(context); rollingPolicy.setParent(appender); diff --git a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/apollo/DynamicLoggerConfiguration.java b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/apollo/DynamicLoggerConfiguration.java index eb6bd00941ad70d4079e8ac1613340d019d97883..fce20a6ddd377a11ba1f64d6bdddf50c09eb5b75 100644 --- a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/apollo/DynamicLoggerConfiguration.java +++ b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/apollo/DynamicLoggerConfiguration.java @@ -1,15 +1,11 @@ package com.schbrain.framework.autoconfigure.logger.apollo; -import com.ctrip.framework.apollo.*; -import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; -import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils; -import com.schbrain.framework.autoconfigure.logger.apollo.listener.LoggingConfigFileChangeListener; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigService; import com.schbrain.framework.autoconfigure.logger.apollo.listener.LoggingLevelChangeListener; -import com.schbrain.framework.autoconfigure.logger.properties.LoggingNamespaceProperties; +import com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties; import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.logging.LoggingSystem; -import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.util.StringUtils; /** @@ -19,47 +15,14 @@ import org.springframework.util.StringUtils; * @since 2021/11/19 **/ @Slf4j -@EnableConfigurationProperties(LoggingNamespaceProperties.class) public class DynamicLoggerConfiguration { - private final ConfigurableEnvironment environment; - private final LoggingSystem loggingSystem; - private final LoggingNamespaceProperties loggerProperties; - - public DynamicLoggerConfiguration(ConfigurableEnvironment environment, LoggingSystem loggingSystem, - LoggingNamespaceProperties loggerProperties) { - this.environment = environment; - this.loggingSystem = loggingSystem; - this.loggerProperties = loggerProperties; - this.init(); - } - - private void init() { - if (ConfigUtils.isApolloDisabled()) { - return; - } - listenToLoggingLevelChange(); - listenToLoggingConfigFileChange(); - } - - private void listenToLoggingConfigFileChange() { - String loggerConfigFileNamespace = loggerProperties.getLoggerConfigFile(); - if (!StringUtils.hasText(loggerConfigFileNamespace)) { - log.debug("logger config file reload is disabled"); - return; - } - - log.debug("init logger config file listener, config file namespace: {}", loggerConfigFileNamespace); - - ConfigFile loggingConfiguration = ConfigService.getConfigFile(loggerConfigFileNamespace, ConfigFileFormat.XML); - if (!loggingConfiguration.hasContent()) { - return; - } - loggingConfiguration.addChangeListener(new LoggingConfigFileChangeListener(loggingSystem, environment, loggerConfigFileNamespace)); + public DynamicLoggerConfiguration(LoggingSystem loggingSystem, LoggerProperties loggerProperties) { + this.listenToLoggingLevelChange(loggingSystem, loggerProperties); } - private void listenToLoggingLevelChange() { - String loggerNamespace = loggerProperties.getLogger(); + private void listenToLoggingLevelChange(LoggingSystem loggingSystem, LoggerProperties loggerProperties) { + String loggerNamespace = loggerProperties.getDefaultNamespace(); if (!StringUtils.hasText(loggerNamespace)) { log.debug("logger level reload is disabled"); return; diff --git a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/apollo/listener/LoggingConfigFileChangeListener.java b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/apollo/listener/LoggingConfigFileChangeListener.java deleted file mode 100644 index fcc563bb49f84dd5ac377d85bfe394052b6f8e89..0000000000000000000000000000000000000000 --- a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/apollo/listener/LoggingConfigFileChangeListener.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.schbrain.framework.autoconfigure.logger.apollo.listener; - -import cn.hutool.extra.spring.SpringUtil; -import com.ctrip.framework.apollo.ConfigFileChangeListener; -import com.ctrip.framework.apollo.model.ConfigFileChangeEvent; -import com.schbrain.framework.autoconfigure.logger.JsonLoggerInitializer; -import com.schbrain.framework.autoconfigure.logger.util.LoggerUtils; -import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.logging.*; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.util.StringUtils; - -import java.util.List; - -/** - * @author liaozan - * @since 2021/11/8 - */ -@Slf4j -public class LoggingConfigFileChangeListener implements ConfigFileChangeListener { - - private final LoggingSystem loggingSystem; - private final ConfigurableEnvironment environment; - private final String loggerFileName; - - public LoggingConfigFileChangeListener(LoggingSystem loggingSystem, ConfigurableEnvironment environment, String loggerFileName) { - this.loggingSystem = loggingSystem; - this.environment = environment; - this.loggerFileName = loggerFileName; - } - - @Override - public void onChange(ConfigFileChangeEvent changeEvent) { - String content = changeEvent.getNewValue(); - if (!StringUtils.hasText(content)) { - log.warn("Empty logging configuration, reInitialize loggingSystem is disabled"); - return; - } - String configurationLocation = LoggerUtils.storeConfiguration(loggerFileName, content); - if (configurationLocation == null) { - return; - } - reinitialize(configurationLocation); - log.debug("ReInitialize loggingSystem, configFile location: {}", configurationLocation); - } - - private void reinitialize(String configLocation) { - List configurations = loggingSystem.getLoggerConfigurations(); - loggingSystem.cleanUp(); - loggingSystem.initialize(new LoggingInitializationContext(environment), configLocation, null); - configurations.forEach(configuration -> loggingSystem.setLogLevel(configuration.getName(), configuration.getConfiguredLevel())); - // reInitialize json logger - new JsonLoggerInitializer().initialize(SpringUtil.getBean(ConfigurableApplicationContext.class)); - } - -} \ No newline at end of file diff --git a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/listener/LoggerPropertiesPreparedEventListener.java b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/listener/LoggerPropertiesPreparedEventListener.java new file mode 100644 index 0000000000000000000000000000000000000000..a7b3660d3e338dbfe4b8bf4aa847a8ec3c915d5a --- /dev/null +++ b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/listener/LoggerPropertiesPreparedEventListener.java @@ -0,0 +1,81 @@ +package com.schbrain.framework.autoconfigure.logger.listener; + +import cn.hutool.system.SystemUtil; +import com.ctrip.framework.apollo.ConfigFile; +import com.ctrip.framework.apollo.ConfigService; +import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; +import com.google.common.collect.Maps; +import com.schbrain.common.util.InetUtils; +import com.schbrain.common.util.InetUtils.HostInfo; +import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEvent; +import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEventListenerAdapter; +import com.schbrain.framework.autoconfigure.logger.LoggerConfigurationInitializer; +import com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.util.StringUtils; + +import java.io.IOException; +import java.nio.file.*; +import java.util.Map; + +import static org.springframework.boot.context.logging.LoggingApplicationListener.CONFIG_PROPERTY; + +/** + * @author liaozan + * @since 2023-04-28 + */ +@Slf4j +public class LoggerPropertiesPreparedEventListener extends PropertiesPreparedEventListenerAdapter { + + @Override + protected void onPropertiesPrepared(PropertiesPreparedEvent event, LoggerProperties properties) { + ConfigurableEnvironment environment = event.getEnvironment(); + Map hostInfoProperties = buildHostInfoProperties(); + event.getPropertySource().addProperties(hostInfoProperties); + configLoggingFileLocation(environment); + new LoggerConfigurationInitializer(environment, properties).init(); + } + + private Map buildHostInfoProperties() { + HostInfo hostInfo = InetUtils.findFirstNonLoopBackHostInfo(); + Map properties = Maps.newHashMapWithExpectedSize(2); + properties.put("application.hostname", hostInfo.getHostname()); + properties.put("application.ipAddress", hostInfo.getIpAddress()); + return properties; + } + + /** + * Add {@link org.springframework.boot.context.logging.LoggingApplicationListener#CONFIG_PROPERTY} property to SystemProperty + * + * @see org.springframework.boot.context.logging.LoggingApplicationListener#initializeSystem(ConfigurableEnvironment, org.springframework.boot.logging.LoggingSystem, org.springframework.boot.logging.LogFile) + */ + @SuppressWarnings("JavadocReference") + private void configLoggingFileLocation(ConfigurableEnvironment environment) { + if (environment.containsProperty(CONFIG_PROPERTY)) { + return; + } + ConfigFile loggingConfiguration = ConfigService.getConfigFile("logback-spring", ConfigFileFormat.XML); + String content = loggingConfiguration.getContent(); + if (!StringUtils.hasText(content)) { + log.warn("empty logging configuration, reinitialize loggingSystem is disabled"); + return; + } + + String loggerConfigurationLocation = null; + String tempDir = SystemUtil.getUserInfo().getTempDir(); + Path storeLocation = Paths.get(tempDir, "logback-spring.xml"); + try { + loggerConfigurationLocation = Files.writeString(storeLocation, content).toString(); + } catch (IOException e) { + log.warn("failed to write logging file, will not behave as expected", e); + } + + if (loggerConfigurationLocation == null) { + return; + } + System.setProperty(CONFIG_PROPERTY, loggerConfigurationLocation); + log.debug(String.format("%s is set to %s", CONFIG_PROPERTY, loggerConfigurationLocation)); + } + +} \ No newline at end of file diff --git a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/SchbrainLogstashEncoder.java b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/EnhancedLogstashEncoder.java similarity index 76% rename from starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/SchbrainLogstashEncoder.java rename to starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/EnhancedLogstashEncoder.java index f8b16d3d820e7e65de8d9e1fabae6ba019044008..3a1f4a41d5ef88fb60b74925b9f50d5aed96754d 100644 --- a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/SchbrainLogstashEncoder.java +++ b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/EnhancedLogstashEncoder.java @@ -8,11 +8,11 @@ import net.logstash.logback.encoder.LogstashEncoder; * @author liaozan * @since 2022/1/4 */ -public class SchbrainLogstashEncoder extends LogstashEncoder { +public class EnhancedLogstashEncoder extends LogstashEncoder { @Override protected AbstractCompositeJsonFormatter createFormatter() { - return new SchbrainLogstashFormatter(this); + return new EnhancedLogstashFormatter(this); } -} +} \ No newline at end of file diff --git a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/SchbrainLogstashFormatter.java b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/EnhancedLogstashFormatter.java similarity index 94% rename from starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/SchbrainLogstashFormatter.java rename to starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/EnhancedLogstashFormatter.java index bd1de64a65cf41be5411c890c476fb13264a1ac1..09404b7bd85045903862f937715a5883786ddb68 100644 --- a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/SchbrainLogstashFormatter.java +++ b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/logstash/EnhancedLogstashFormatter.java @@ -15,9 +15,9 @@ import java.util.List; * @author liaozan * @since 2022/1/4 */ -public class SchbrainLogstashFormatter extends LogstashFormatter { +public class EnhancedLogstashFormatter extends LogstashFormatter { - public SchbrainLogstashFormatter(ContextAware declaredOrigin) { + public EnhancedLogstashFormatter(ContextAware declaredOrigin) { super(declaredOrigin); configureProviders(); } @@ -63,4 +63,4 @@ public class SchbrainLogstashFormatter extends LogstashFormatter { return excludeProviders; } -} +} \ No newline at end of file diff --git a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggingFileProperties.java b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggerProperties.java similarity index 71% rename from starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggingFileProperties.java rename to starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggerProperties.java index 0de111ecbaa522a811fe11eae2bd7d1ef6154556..655c5ceeb535d5ce625eac747a88cec20625b349 100644 --- a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggingFileProperties.java +++ b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggerProperties.java @@ -4,6 +4,7 @@ import com.schbrain.common.util.support.ConfigurableProperties; import lombok.Data; import lombok.EqualsAndHashCode; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.core.PriorityOrdered; import java.time.Duration; @@ -14,7 +15,7 @@ import java.time.Duration; @Data @EqualsAndHashCode(callSuper = true) @ConfigurationProperties(prefix = "schbrain.logging.file") -public class LoggingFileProperties extends ConfigurableProperties { +public class LoggerProperties extends ConfigurableProperties implements PriorityOrdered { public static final String DEFAULT_LOG_PATH = "/data/logs"; @@ -30,4 +31,14 @@ public class LoggingFileProperties extends ConfigurableProperties { private int maxHistory = (int) Duration.ofDays(30).toDays(); + @Override + public String getDefaultNamespace() { + return "logger-common"; + } + + @Override + public int getOrder() { + return HIGHEST_PRECEDENCE; + } + } \ No newline at end of file diff --git a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggerPropertiesPreparer.java b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggerPropertiesPreparer.java deleted file mode 100644 index fc03f6efe88b9dc3f82db6604031c4ea1eeedaa0..0000000000000000000000000000000000000000 --- a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggerPropertiesPreparer.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.schbrain.framework.autoconfigure.logger.properties; - -import com.google.common.collect.Maps; -import com.schbrain.common.util.InetUtils; -import com.schbrain.common.util.InetUtils.HostInfo; -import com.schbrain.framework.autoconfigure.apollo.properties.ApolloPropertiesPreparer; -import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils; -import com.schbrain.framework.autoconfigure.logger.util.LoggerUtils; -import com.schbrain.framework.support.spring.EnvironmentPostProcessorAdapter; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.boot.ConfigurableBootstrapContext; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.context.logging.LoggingApplicationListener; -import org.springframework.boot.logging.*; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.core.Ordered; -import org.springframework.core.env.ConfigurableEnvironment; - -import java.util.Map; - -import static com.schbrain.framework.autoconfigure.logger.util.LoggerUtils.getLoggerConfigurationLocation; -import static org.springframework.boot.context.logging.LoggingApplicationListener.CONFIG_PROPERTY; - -/** - * @author liaozan - * @since 2021/11/19 - */ -public class LoggerPropertiesPreparer extends EnvironmentPostProcessorAdapter implements Ordered { - - public LoggerPropertiesPreparer(DeferredLogFactory factory, ConfigurableBootstrapContext bootstrapContext) { - super(factory, bootstrapContext); - } - - @Override - public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { - LoggerUtils.setLogFactory(getDeferredLogFactory()); - bindLoggingProperties(environment); - bindHostInfoProperty(environment); - earlyLoadLoggingConfig(environment); - } - - @Override - public int getOrder() { - // Configure after the apollo property is initialize - return ApolloPropertiesPreparer.ORDER + 1; - } - - @Override - public void onBootstrapContextClosed(ConfigurableApplicationContext applicationContext) { - ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory(); - if (!beanFactory.containsSingleton(HostInfo.NAME)) { - HostInfo hostInfo = InetUtils.findFirstNonLoopBackHostInfo(); - beanFactory.registerSingleton(HostInfo.NAME, hostInfo); - } - } - - private void bindHostInfoProperty(ConfigurableEnvironment environment) { - HostInfo hostInfo = InetUtils.findFirstNonLoopBackHostInfo(); - Map source = Maps.newHashMapWithExpectedSize(2); - source.put("application.hostname", hostInfo.getHostname()); - source.put("application.ipAddress", hostInfo.getIpAddress()); - ConfigUtils.addToEnvironment(environment, HostInfo.NAME, source); - } - - /** - * @see LoggingApplicationListener#setLogLevels(LoggingSystem, ConfigurableEnvironment) - */ - @SuppressWarnings("JavadocReference") - private void earlyLoadLoggingConfig(ConfigurableEnvironment environment) { - LoggingNamespaceProperties properties = ConfigUtils.loadConfig(environment, LoggingNamespaceProperties.class); - // load logging level properties - Map loggingLevelProperties = ConfigUtils.loadConfig(properties.getLogger()); - ConfigUtils.addToEnvironment(environment, "loggingLevelProperties", loggingLevelProperties); - // load logging file properties - ConfigUtils.loadConfig(environment, LoggingFileProperties.class); - } - - /** - * Add {@link LoggingApplicationListener#CONFIG_PROPERTY} property to SystemProperty - * - * @see LoggingApplicationListener#initializeSystem(ConfigurableEnvironment, LoggingSystem, LogFile) - */ - @SuppressWarnings("JavadocReference") - private void bindLoggingProperties(ConfigurableEnvironment environment) { - if (environment.containsProperty(CONFIG_PROPERTY)) { - return; - } - String loggerConfigurationLocation = getLoggerConfigurationLocation(environment); - if (loggerConfigurationLocation == null) { - return; - } - System.setProperty(CONFIG_PROPERTY, loggerConfigurationLocation); - getLog().debug(String.format("%s is set to %s", CONFIG_PROPERTY, loggerConfigurationLocation)); - } - -} \ No newline at end of file diff --git a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggingNamespaceProperties.java b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggingNamespaceProperties.java deleted file mode 100644 index 72b93994067435c44adb7ea00efa3205bb635159..0000000000000000000000000000000000000000 --- a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/properties/LoggingNamespaceProperties.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.schbrain.framework.autoconfigure.logger.properties; - -import com.schbrain.common.util.support.ConfigurableProperties; -import lombok.Data; -import lombok.EqualsAndHashCode; -import org.springframework.boot.context.properties.ConfigurationProperties; - -/** - * @author liaozan - * @since 2021/11/24 - */ -@Data -@EqualsAndHashCode(callSuper = true) -@ConfigurationProperties(prefix = "schbrain.logging.namespace") -public class LoggingNamespaceProperties extends ConfigurableProperties { - - private String logger = "logger-common"; - - private String loggerConfigFile = "logback-spring"; - -} \ No newline at end of file diff --git a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/util/LoggerUtils.java b/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/util/LoggerUtils.java deleted file mode 100644 index af92f920ae66ecff3b9072d03c1c28277b179714..0000000000000000000000000000000000000000 --- a/starters/logger-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/logger/util/LoggerUtils.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.schbrain.framework.autoconfigure.logger.util; - -import cn.hutool.system.SystemUtil; -import com.ctrip.framework.apollo.ConfigFile; -import com.ctrip.framework.apollo.ConfigService; -import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; -import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils; -import com.schbrain.framework.autoconfigure.logger.properties.LoggingNamespaceProperties; -import org.apache.commons.logging.Log; -import org.springframework.boot.logging.DeferredLogFactory; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.util.StringUtils; - -import javax.annotation.Nullable; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.*; - -/** - * @author liaozan - * @since 2021/11/15 - */ -public class LoggerUtils { - - private static Log LOGGER; - - public static void setLogFactory(DeferredLogFactory deferredLog) { - LOGGER = deferredLog.getLog(LoggerUtils.class); - } - - @Nullable - public static String getLoggerConfigurationLocation(ConfigurableEnvironment environment) { - if (ConfigUtils.isApolloDisabled()) { - return null; - } - LoggingNamespaceProperties properties = ConfigUtils.loadConfig(environment, LoggingNamespaceProperties.class); - String namespace = properties.getLoggerConfigFile(); - ConfigFile loggingConfiguration = ConfigService.getConfigFile(namespace, ConfigFileFormat.XML); - String content = loggingConfiguration.getContent(); - if (!StringUtils.hasText(content)) { - LOGGER.warn("empty logging configuration, reinitialize loggingSystem is disabled"); - return null; - } - return storeConfiguration(namespace, content); - } - - @Nullable - public static String storeConfiguration(String fileName, String content) { - String tempDir = SystemUtil.getUserInfo().getTempDir(); - Path storeLocation = Paths.get(tempDir, fileName + ".xml"); - try { - return Files.write(storeLocation, content.getBytes(StandardCharsets.UTF_8)).toString(); - } catch (IOException e) { - LOGGER.warn("failed to write logging file, will not behave as expected", e); - return null; - } - } - -} \ No newline at end of file diff --git a/starters/logger-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json b/starters/logger-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json index 42dc0a309e50018b8bff99add55c44c641716c89..5b5c34cc2ea3ec98590e9ed07461c161f26bc8bb 100644 --- a/starters/logger-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/starters/logger-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json @@ -2,92 +2,45 @@ "groups": [ { "name": "schbrain.logging.file", - "type": "com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties" - }, - { - "name": "schbrain.logging.namespace", - "type": "com.schbrain.framework.autoconfigure.logger.properties.LoggingNamespaceProperties", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingNamespaceProperties" + "type": "com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties", + "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties" } ], "properties": [ { "name": "schbrain.logging.file.enable-json-console-output", "type": "java.lang.Boolean", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties", + "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties", "defaultValue": false }, { "name": "schbrain.logging.file.enable-json-file-output", "type": "java.lang.Boolean", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties", + "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties", "defaultValue": false }, { "name": "schbrain.logging.file.enable-json-log-write-to-logstash", "type": "java.lang.Boolean", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties", + "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties", "defaultValue": false }, { "name": "schbrain.logging.file.log-path", "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties", + "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties", "defaultValue": "\/data\/logs" }, { "name": "schbrain.logging.file.logstash-address", "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties" + "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties" }, { "name": "schbrain.logging.file.max-history", "type": "java.lang.Integer", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties", + "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties", "defaultValue": 0 - }, - { - "name": "schbrain.logging.file.name", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties" - }, - { - "name": "schbrain.logging.file.namespace", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties" - }, - { - "name": "schbrain.logging.file.prefix", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties" - }, - { - "name": "schbrain.logging.namespace.logger", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingNamespaceProperties", - "defaultValue": "logger-common" - }, - { - "name": "schbrain.logging.namespace.logger-config-file", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingNamespaceProperties", - "defaultValue": "logback-spring" - }, - { - "name": "schbrain.logging.namespace.name", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingNamespaceProperties" - }, - { - "name": "schbrain.logging.namespace.namespace", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingNamespaceProperties" - }, - { - "name": "schbrain.logging.namespace.prefix", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.logger.properties.LoggingNamespaceProperties" } ], "hints": [] diff --git a/starters/logger-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starters/logger-spring-boot-starter/src/main/resources/META-INF/spring.factories index 989351df72c37d79ea794acf8cf1d832cdead1c6..2a1715f06bc911e6589ba6ce11aa5295de150738 100644 --- a/starters/logger-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/starters/logger-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1,4 +1,6 @@ org.springframework.context.ApplicationContextInitializer=\ - com.schbrain.framework.autoconfigure.logger.JsonLoggerInitializer,\ com.schbrain.framework.autoconfigure.logger.logback.LogbackTraceIdConfiguration -org.springframework.boot.env.EnvironmentPostProcessor=com.schbrain.framework.autoconfigure.logger.properties.LoggerPropertiesPreparer \ No newline at end of file +org.springframework.context.ApplicationListener=\ + com.schbrain.framework.autoconfigure.logger.listener.LoggerPropertiesPreparedEventListener +com.schbrain.common.util.support.ConfigurableProperties=\ + com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties \ No newline at end of file diff --git a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/MybatisAutoConfiguration.java b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/MybatisAutoConfiguration.java index 936f8c923c4e73edf5419938848560fb3f64f60d..85e401f8bc0f60f840e02a4b2bf0823b63ff8560 100644 --- a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/MybatisAutoConfiguration.java +++ b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/MybatisAutoConfiguration.java @@ -14,8 +14,7 @@ import com.schbrain.framework.autoconfigure.mybatis.datasource.customizer.DataSo import com.schbrain.framework.autoconfigure.mybatis.datasource.customizer.DefaultDataSourceCustomizer; import com.schbrain.framework.autoconfigure.mybatis.datasource.extractor.*; import com.schbrain.framework.autoconfigure.mybatis.listener.TableConstraintCheckerBean; -import com.schbrain.framework.autoconfigure.mybatis.properties.DataSourceConnectionProperties; -import com.schbrain.framework.autoconfigure.mybatis.properties.MybatisProperties; +import com.schbrain.framework.autoconfigure.mybatis.properties.*; import com.schbrain.framework.autoconfigure.mybatis.sql.injector.DefaultMethodSqlInjector; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.config.BeanDefinition; @@ -33,7 +32,7 @@ import java.util.List; * @since 2021/10/14 */ @AutoConfiguration(before = MybatisPlusAutoConfiguration.class) -@EnableConfigurationProperties({MybatisProperties.class, DataSourceConnectionProperties.class}) +@EnableConfigurationProperties({DataSourceProperties.class, MybatisProperties.class, DataSourceConnectionProperties.class}) @Import({HikariDataSourcePropertiesExtractor.class, DruidDataSourcePropertiesExtractor.class}) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public class MybatisAutoConfiguration { diff --git a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/properties/DataSourceProperties.java b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/properties/DataSourceProperties.java new file mode 100644 index 0000000000000000000000000000000000000000..d332c59ec122b29be948510311eb8ec1790f40a6 --- /dev/null +++ b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/properties/DataSourceProperties.java @@ -0,0 +1,22 @@ +package com.schbrain.framework.autoconfigure.mybatis.properties; + +import com.schbrain.common.util.support.ConfigurableProperties; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * @author liaozan + * @since 2023-04-27 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@ConfigurationProperties(prefix = "spring.datasource") +public class DataSourceProperties extends ConfigurableProperties { + + @Override + public String getDefaultNamespace() { + return "jdbc-common"; + } + +} \ No newline at end of file diff --git a/starters/mybatis-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json b/starters/mybatis-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json index c21b8e95db5a8d96634ee13479711814bfd0c3a0..fe93029d956ad05631b6de1cf9bade08fcc77fc2 100644 --- a/starters/mybatis-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/starters/mybatis-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json @@ -9,6 +9,11 @@ "name": "schbrain.mybatis", "type": "com.schbrain.framework.autoconfigure.mybatis.properties.MybatisProperties", "sourceType": "com.schbrain.framework.autoconfigure.mybatis.properties.MybatisProperties" + }, + { + "name": "spring.datasource", + "type": "com.schbrain.framework.autoconfigure.mybatis.properties.DataSourceProperties", + "sourceType": "com.schbrain.framework.autoconfigure.mybatis.properties.DataSourceProperties" } ], "properties": [ @@ -92,21 +97,6 @@ "description": "是否开启表约束检查", "sourceType": "com.schbrain.framework.autoconfigure.mybatis.properties.MybatisProperties", "defaultValue": true - }, - { - "name": "schbrain.mybatis.name", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.mybatis.properties.MybatisProperties" - }, - { - "name": "schbrain.mybatis.namespace", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.mybatis.properties.MybatisProperties" - }, - { - "name": "schbrain.mybatis.prefix", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.mybatis.properties.MybatisProperties" } ], "hints": [] diff --git a/starters/mybatis-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starters/mybatis-spring-boot-starter/src/main/resources/META-INF/spring.factories index 6911308417b6c1c3f3ddc11503c9d48e427e536a..4589c099ad8ee719383fec101967ad136b8d6533 100644 --- a/starters/mybatis-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/starters/mybatis-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1 +1,5 @@ -org.springframework.boot.diagnostics.FailureAnalyzer=com.schbrain.framework.autoconfigure.mybatis.constraint.TableConstraintCheckFailureAnalyzer \ No newline at end of file +org.springframework.boot.diagnostics.FailureAnalyzer=\ + com.schbrain.framework.autoconfigure.mybatis.constraint.TableConstraintCheckFailureAnalyzer +com.schbrain.common.util.support.ConfigurableProperties=\ + com.schbrain.framework.autoconfigure.mybatis.properties.MybatisProperties,\ + com.schbrain.framework.autoconfigure.mybatis.properties.DataSourceProperties \ No newline at end of file diff --git a/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/OssAutoConfiguration.java b/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/OssAutoConfiguration.java index 4fc070e5016c2eeef73c64aaed72347bcb4101f5..d5eebe957462387abde67ba851e53d8cc236c221 100644 --- a/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/OssAutoConfiguration.java +++ b/starters/oss-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/oss/OssAutoConfiguration.java @@ -1,6 +1,5 @@ package com.schbrain.framework.autoconfigure.oss; -import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils; import com.schbrain.framework.autoconfigure.oss.properties.OssProperties; import com.schbrain.framework.autoconfigure.oss.util.OssUtils; import org.springframework.boot.autoconfigure.AutoConfiguration; @@ -16,13 +15,8 @@ import org.springframework.core.env.ConfigurableEnvironment; @EnableConfigurationProperties(OssProperties.class) public class OssAutoConfiguration { - public OssAutoConfiguration(ConfigurableApplicationContext applicationContext) { - initialize(applicationContext); - } - - private void initialize(ConfigurableApplicationContext applicationContext) { + public OssAutoConfiguration(ConfigurableApplicationContext applicationContext, OssProperties ossProperties) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); - OssProperties ossProperties = ConfigUtils.loadConfig(environment, OssProperties.class); OssUtils.initialize(environment, ossProperties); } diff --git a/starters/oss-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json b/starters/oss-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json index 1381501ea94df5a88a26dc3dcfba30c15485b7c0..b34cc825b0ed4ac107dc154515e91703de75dcee 100644 --- a/starters/oss-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/starters/oss-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json @@ -38,21 +38,6 @@ "type": "java.lang.String", "sourceType": "com.schbrain.framework.autoconfigure.oss.properties.OssProperties" }, - { - "name": "schbrain.oss.name", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.oss.properties.OssProperties" - }, - { - "name": "schbrain.oss.namespace", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.oss.properties.OssProperties" - }, - { - "name": "schbrain.oss.prefix", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.oss.properties.OssProperties" - }, { "name": "schbrain.oss.secret-access-key", "type": "java.lang.String", diff --git a/starters/oss-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starters/oss-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000000000000000000000000000000000..adf067691c4b842cf08cbde8c4962b6351c5dd11 --- /dev/null +++ b/starters/oss-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +com.schbrain.common.util.support.ConfigurableProperties=\ + com.schbrain.framework.autoconfigure.oss.properties.OssProperties \ No newline at end of file diff --git a/starters/pom.xml b/starters/pom.xml index adcb13c9d8e073a5115c27b6f3be385c42f5f4ca..97e34b2346f94555428211c00f94acdce0950fa7 100644 --- a/starters/pom.xml +++ b/starters/pom.xml @@ -1,6 +1,6 @@ - 4.0.0 @@ -19,6 +19,8 @@ apollo-spring-boot-starter cache-spring-boot-starter dubbo-spring-boot-starter + elasticsearch-spring-boot-starter + kafka-spring-boot-starter logger-spring-boot-starter mybatis-spring-boot-starter oss-spring-boot-starter diff --git a/starters/xxl-job-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/xxl/XxlJobAutoConfiguration.java b/starters/xxl-job-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/xxl/XxlJobAutoConfiguration.java index 378b44ce99aeccd01cb55c0bcf1e6d603c6344e7..a29b64f9d3694bda78e3ee015da36792f60d5529 100644 --- a/starters/xxl-job-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/xxl/XxlJobAutoConfiguration.java +++ b/starters/xxl-job-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/xxl/XxlJobAutoConfiguration.java @@ -1,8 +1,7 @@ package com.schbrain.framework.autoconfigure.xxl; import com.schbrain.common.util.ApplicationName; -import com.schbrain.framework.autoconfigure.apollo.util.ConfigUtils; -import com.schbrain.framework.autoconfigure.logger.properties.LoggingFileProperties; +import com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties; import com.schbrain.framework.autoconfigure.xxl.condition.XxlJobShouldAvailableCondition; import com.schbrain.framework.autoconfigure.xxl.properties.XxlJobProperties; import com.xxl.job.core.executor.XxlJobExecutor; @@ -26,9 +25,9 @@ public class XxlJobAutoConfiguration { @Bean @ConditionalOnMissingBean(XxlJobExecutor.class) - public SchbrainXxlJobExecutor schbrainXxlJobSpringExecutor(ConfigurableEnvironment environment) { - XxlJobProperties xxlJobProperties = ConfigUtils.loadConfig(environment, XxlJobProperties.class); - LoggingFileProperties loggingProperties = ConfigUtils.loadConfig(environment, LoggingFileProperties.class); + public SchbrainXxlJobExecutor schbrainXxlJobSpringExecutor(ConfigurableEnvironment environment, + XxlJobProperties xxlJobProperties, + LoggerProperties loggingProperties) { String applicationName = ApplicationName.get(environment); SchbrainXxlJobExecutor executor = new SchbrainXxlJobExecutor(); executor.setAdminAddresses(xxlJobProperties.getAdminAddresses()); diff --git a/starters/xxl-job-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json b/starters/xxl-job-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json index 8c0f67cc9d54f1368c55f6ce6d5835384c8ccd3e..f719548ec20b18b1551eba166aba382260cc90e0 100644 --- a/starters/xxl-job-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/starters/xxl-job-spring-boot-starter/src/main/resources/META-INF/spring-configuration-metadata.json @@ -28,27 +28,12 @@ "sourceType": "com.schbrain.framework.autoconfigure.xxl.properties.XxlJobProperties", "defaultValue": 7 }, - { - "name": "schbrain.xxl.name", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.xxl.properties.XxlJobProperties" - }, - { - "name": "schbrain.xxl.namespace", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.xxl.properties.XxlJobProperties" - }, { "name": "schbrain.xxl.port", "type": "java.lang.Integer", "sourceType": "com.schbrain.framework.autoconfigure.xxl.properties.XxlJobProperties", "defaultValue": -1 }, - { - "name": "schbrain.xxl.prefix", - "type": "java.lang.String", - "sourceType": "com.schbrain.framework.autoconfigure.xxl.properties.XxlJobProperties" - }, { "name": "schbrain.xxl.register", "type": "java.lang.Boolean", diff --git a/starters/xxl-job-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starters/xxl-job-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000000000000000000000000000000000..745d88dbb4ff1068c47140a2a4d797f16a87dc4a --- /dev/null +++ b/starters/xxl-job-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +com.schbrain.common.util.support.ConfigurableProperties=\ + com.schbrain.framework.autoconfigure.xxl.properties.XxlJobProperties \ No newline at end of file diff --git a/support/pom.xml b/support/pom.xml index 5902572a9e47c8a8863700fc777426dec9bb335f..7aa142db846419d58cfd18e99c188f494ed6913a 100644 --- a/support/pom.xml +++ b/support/pom.xml @@ -1,6 +1,6 @@ - 4.0.0 diff --git a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/BootstrapContextListenerComposite.java b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/BootstrapContextListenerComposite.java deleted file mode 100644 index 0f5f3c9fec0933567ba6feb5f82553d2fe52804f..0000000000000000000000000000000000000000 --- a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/BootstrapContextListenerComposite.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.schbrain.framework.support.spring; - -import cn.hutool.core.lang.Singleton; -import org.springframework.boot.BootstrapContextClosedEvent; -import org.springframework.boot.ConfigurableBootstrapContext; -import org.springframework.context.ApplicationListener; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.core.annotation.AnnotationAwareOrderComparator; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author liaozan - * @since 2021/11/22 - */ -public class BootstrapContextListenerComposite implements ApplicationListener { - - private final List adapters = new ArrayList<>(); - - public static BootstrapContextListenerComposite getInstance() { - return Singleton.get(BootstrapContextListenerComposite.class); - } - - public void addListener(EnvironmentPostProcessorAdapter adapter) { - if (adapters.contains(adapter)) { - return; - } - adapters.add(adapter); - } - - @Override - public void onApplicationEvent(BootstrapContextClosedEvent event) { - if (adapters.isEmpty()) { - return; - } - - AnnotationAwareOrderComparator.sort(adapters); - - ConfigurableBootstrapContext bootstrapContext = (ConfigurableBootstrapContext) event.getBootstrapContext(); - ConfigurableApplicationContext applicationContext = event.getApplicationContext(); - - for (EnvironmentPostProcessorAdapter adapter : adapters) { - adapter.onBootstrapContextClosed(bootstrapContext, applicationContext); - } - } - -} diff --git a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/EnvironmentPostProcessorAdapter.java b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/EnvironmentPostProcessorAdapter.java deleted file mode 100644 index 8731ec4208de57b432acd802c5ea1cd7dba5cb83..0000000000000000000000000000000000000000 --- a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/EnvironmentPostProcessorAdapter.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.schbrain.framework.support.spring; - -import lombok.Getter; -import org.apache.commons.logging.Log; -import org.springframework.boot.ConfigurableBootstrapContext; -import org.springframework.boot.env.EnvironmentPostProcessor; -import org.springframework.boot.logging.DeferredLogFactory; -import org.springframework.context.ConfigurableApplicationContext; - -/** - * @author liaozan - * @since 2021/11/22 - */ -@Getter -public abstract class EnvironmentPostProcessorAdapter implements EnvironmentPostProcessor { - - private final Log log; - private final DeferredLogFactory deferredLogFactory; - private final ConfigurableBootstrapContext bootstrapContext; - - public EnvironmentPostProcessorAdapter(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) { - this.log = logFactory.getLog(getClass()); - this.bootstrapContext = bootstrapContext; - this.deferredLogFactory = logFactory; - this.addListener(this.bootstrapContext); - } - - protected void onBootstrapContextClosed(ConfigurableBootstrapContext bootstrapContext, ConfigurableApplicationContext applicationContext) { - onBootstrapContextClosed(applicationContext); - } - - protected void onBootstrapContextClosed(ConfigurableApplicationContext applicationContext) { - - } - - private void addListener(ConfigurableBootstrapContext bootstrapContext) { - BootstrapContextListenerComposite listener = BootstrapContextListenerComposite.getInstance(); - listener.addListener(this); - bootstrapContext.addCloseListener(listener); - } - -} \ No newline at end of file diff --git a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/EnvironmentPostProcessorLoggerAwareAdapter.java b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/EnvironmentPostProcessorLoggerAwareAdapter.java new file mode 100644 index 0000000000000000000000000000000000000000..992860a82c54fefeff2e5e2dbe50f65220d2f92e --- /dev/null +++ b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/EnvironmentPostProcessorLoggerAwareAdapter.java @@ -0,0 +1,26 @@ +package com.schbrain.framework.support.spring; + +import lombok.Getter; +import org.apache.commons.logging.Log; +import org.springframework.boot.ConfigurableBootstrapContext; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.boot.logging.DeferredLogFactory; + +/** + * @author liaozan + * @since 2021/11/22 + */ +@Getter +public abstract class EnvironmentPostProcessorLoggerAwareAdapter implements EnvironmentPostProcessor { + + private final Log log; + private final DeferredLogFactory deferredLogFactory; + private final ConfigurableBootstrapContext bootstrapContext; + + public EnvironmentPostProcessorLoggerAwareAdapter(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) { + this.log = logFactory.getLog(getClass()); + this.bootstrapContext = bootstrapContext; + this.deferredLogFactory = logFactory; + } + +} \ No newline at end of file diff --git a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/defaults/DefaultPropertiesEnvironmentPostProcessor.java b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/defaults/DefaultPropertiesEnvironmentPostProcessor.java index e6e032ebb01f711637ff456c950117eeb4c3ec04..b0286cf36d7c30daa454c929abbcc736c2ed1220 100644 --- a/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/defaults/DefaultPropertiesEnvironmentPostProcessor.java +++ b/support/schbrain-spring-support/src/main/java/com/schbrain/framework/support/spring/defaults/DefaultPropertiesEnvironmentPostProcessor.java @@ -5,10 +5,11 @@ import cn.hutool.core.util.ArrayUtil; import com.schbrain.common.constants.DateTimeFormatters; import com.schbrain.common.util.EnvUtils; import com.schbrain.common.util.PortUtils; -import com.schbrain.framework.support.spring.EnvironmentPostProcessorAdapter; +import com.schbrain.framework.support.spring.EnvironmentPostProcessorLoggerAwareAdapter; import org.springframework.boot.*; import org.springframework.boot.actuate.autoconfigure.health.HealthProperties.Show; import org.springframework.boot.actuate.info.InfoPropertiesInfoContributor.Mode; +import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.boot.web.server.Shutdown; import org.springframework.core.Ordered; @@ -22,7 +23,12 @@ import java.util.*; * @author liaozan * @since 2021/12/18 */ -public class DefaultPropertiesEnvironmentPostProcessor extends EnvironmentPostProcessorAdapter implements Ordered { +public class DefaultPropertiesEnvironmentPostProcessor extends EnvironmentPostProcessorLoggerAwareAdapter implements Ordered { + + /** + * set default properties after configData loaded + */ + public static final Integer DEFAULT_ORDER = ConfigDataEnvironmentPostProcessor.ORDER + 1; private static final String SPRING_PROFILE_ACTIVE = "spring.profiles.active"; private static final String DUBBO_REGISTER_KEY = "dubbo.registry.register"; @@ -67,7 +73,7 @@ public class DefaultPropertiesEnvironmentPostProcessor extends EnvironmentPostPr @Override public int getOrder() { - return Ordered.LOWEST_PRECEDENCE; + return DEFAULT_ORDER; } private void configureActiveProfileIfPresent(ConfigurableEnvironment environment, Map defaultProperties) {