Skip to content

Commit

Permalink
Cherry pick bug fix from 3.x (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongZhang authored and straybirdzls committed Nov 16, 2018
1 parent 3c1321c commit 0e3d94b
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class CommonMiddlewareConstants {
*/
public static final String APP_NAME_KEY = "spring.application.name";

public static final String SOFA_BOOTSTRAP = "sofaBootstrap";

/**
* {@link org.springframework.boot.ResourceBanner#getVersionsMap}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.infra.listener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.*;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import com.alipay.sofa.infra.constants.CommonMiddlewareConstants;
import com.alipay.sofa.infra.utils.SOFABootEnvUtils;

/**
* @author qilong.zql
* @since 3.0.0
*/
public class SofaBootstrapRunListener implements
ApplicationListener<ApplicationEnvironmentPreparedEvent>,
Ordered {
private final static String LOGGING_PATH = "logging.path";
private final static String LOGGING_LEVEL = "logging.level";
private static AtomicBoolean executed = new AtomicBoolean(false);

/**
* config log settings
*/
private void assemblyLogSetting(ConfigurableEnvironment environment) {
if (StringUtils.hasText(environment.getProperty(LOGGING_PATH))) {
System.getProperties().setProperty(LOGGING_PATH, environment.getProperty(LOGGING_PATH));
}
for (PropertySource propertySource : environment.getPropertySources()) {
if (!(propertySource instanceof EnumerablePropertySource)) {
continue;
}
for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) {
if (key.startsWith(LOGGING_LEVEL)) {
System.setProperty(key, environment.getProperty(key));
}
}
}
}

/**
* config required properties
* @param environment
*/
private void assemblyRequireProperties(ConfigurableEnvironment environment) {
if (StringUtils.hasText(environment.getProperty(CommonMiddlewareConstants.APP_NAME_KEY))) {
System.getProperties().setProperty(CommonMiddlewareConstants.APP_NAME_KEY,
environment.getProperty(CommonMiddlewareConstants.APP_NAME_KEY));
}
}

/**
* Mark this environment as SOFA bootstrap environment
* @param environment
*/
private void assemblyEnvironmentMark(ConfigurableEnvironment environment) {
environment.getPropertySources().addFirst(
new MapPropertySource(CommonMiddlewareConstants.SOFA_BOOTSTRAP,
new HashMap<String, Object>()));
}

/**
* Un-Mark this environment as SOFA bootstrap environment
* @param environment
*/
private void unAssemblyEnvironmentMark(ConfigurableEnvironment environment) {
environment.getPropertySources().remove(CommonMiddlewareConstants.SOFA_BOOTSTRAP);
}

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

public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
SpringApplication application = event.getSpringApplication();
if (SOFABootEnvUtils.isSpringCloud() && executed.compareAndSet(false, true)) {
StandardEnvironment bootstrapEnvironment = new StandardEnvironment();
for (PropertySource<?> source : event.getEnvironment().getPropertySources()) {
if (source instanceof PropertySource.StubPropertySource) {
continue;
}
bootstrapEnvironment.getPropertySources().addLast(source);
}
List<Class> sources = new ArrayList<>();
for (Object s : application.getSources()) {
if (s instanceof Class) {
sources.add((Class) s);
} else if (s instanceof String) {
sources.add(ClassUtils.resolveClassName((String) s, null));
}
}
SpringApplication bootstrapApplication = new SpringApplicationBuilder()
.profiles(environment.getActiveProfiles()).bannerMode(Banner.Mode.OFF)
.environment(bootstrapEnvironment).sources(sources.toArray(new Class[] {}))
.registerShutdownHook(false).logStartupInfo(false).web(false).listeners()
.initializers().build(event.getArgs());
ApplicationEnvironmentPreparedEvent bootstrapEvent = new ApplicationEnvironmentPreparedEvent(
bootstrapApplication, event.getArgs(), bootstrapEnvironment);
for (ApplicationListener listener : application.getListeners()) {
if (listener instanceof ConfigFileApplicationListener) {
listener.onApplicationEvent(bootstrapEvent);
}
}
assemblyLogSetting(bootstrapEnvironment);
assemblyRequireProperties(bootstrapEnvironment);
assemblyEnvironmentMark(environment);
} else {
unAssemblyEnvironmentMark(environment);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
package com.alipay.sofa.infra.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alipay.sofa.infra.constants.CommonMiddlewareConstants;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.util.ClassUtils;

/**
* SOFABootEnvUtils
Expand All @@ -30,13 +30,7 @@
*/
public class SOFABootEnvUtils {

/**
* org.springframework.cloud.bootstrap.BootstrapApplicationListener#BOOTSTRAP_PROPERTY_SOURCE_NAME
*/
private static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = "bootstrap";

private static final Logger LOGGER = LoggerFactory
.getLogger(SOFABootEnvUtils.class);
private final static String SPRING_CLOUD_MARK_NAME = "org.springframework.cloud.bootstrap.BootstrapConfiguration";

/**
* Determine whether the {@link org.springframework.core.env.Environment} is Spring Cloud bootstrap environment.
Expand All @@ -52,14 +46,14 @@ public class SOFABootEnvUtils {
*/
public static boolean isSpringCloudBootstrapEnvironment(Environment environment) {
if (environment instanceof ConfigurableEnvironment) {
ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment;
if (configurableEnvironment.getPropertySources().contains(
BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
//use app logger
LOGGER.debug("Current application context environment is bootstrap");
return true;
}
return !((ConfigurableEnvironment) environment).getPropertySources().contains(
CommonMiddlewareConstants.SOFA_BOOTSTRAP)
&& isSpringCloud();
}
return false;
}
}

public static boolean isSpringCloud() {
return ClassUtils.isPresent(SPRING_CLOUD_MARK_NAME, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alipay.sofa.infra.autoconfigure.SofaBootInfraAutoConfiguration

# EnvironmentPostProcessor
org.springframework.boot.env.EnvironmentPostProcessor=com.alipay.sofa.infra.env.EnvironmentCustomizer
org.springframework.boot.env.EnvironmentPostProcessor=com.alipay.sofa.infra.env.EnvironmentCustomizer

# SpringApplicationListener
org.springframework.context.ApplicationListener=com.alipay.sofa.infra.listener.SofaBootstrapRunListener
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;

/**
* 参考文档: http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/htmlsingle/#boot-features-testing
Expand All @@ -37,7 +38,7 @@
* <p/>
* Created by yangguanchao on 16/11/18.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SofaBootWebSpringBootApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public abstract class AbstractTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package com.alipay.sofa.infra.utils;

import com.alipay.sofa.infra.base.AbstractTestBase;
import com.alipay.sofa.infra.constants.CommonMiddlewareConstants;
import org.junit.Test;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -38,29 +40,47 @@ public class SOFABootEnvUtilsTest extends AbstractTestBase
implements
ApplicationContextInitializer<ConfigurableApplicationContext> {

private static AtomicLong bootstrapContext = new AtomicLong(0L);
private static AtomicLong bootstrapContext = new AtomicLong(0L);

private static AtomicLong applicatioinContext = new AtomicLong(0L);
private static AtomicLong applicatioinContext = new AtomicLong(0L);

private static ConfigurableEnvironment bootstrapEnvironment;

private static ConfigurableEnvironment applicationEnvironment;

/**
* Method: isSpringCloudBootstrapEnvironment(Environment environment)
*/
@Test
public void testIsSpringCloudBootstrapEnvironment() throws Exception {
public void testIsSpringCloudBootstrapEnvironment() {
Environment environment = ctx.getEnvironment();
assertFalse(SOFABootEnvUtils.isSpringCloudBootstrapEnvironment(environment));
assertEquals(1L, bootstrapContext.get());
assertEquals(1L, applicatioinContext.get());
assertFalse(SOFABootEnvUtils.isSpringCloudBootstrapEnvironment(null));
assertEquals("infra-test",
bootstrapEnvironment.getProperty(CommonMiddlewareConstants.APP_NAME_KEY));
assertEquals("infra-test",
applicationEnvironment.getProperty(CommonMiddlewareConstants.APP_NAME_KEY));
assertEquals("INFO", bootstrapEnvironment.getProperty("logging.level.com.alipay.test"));
assertEquals("INFO", applicationEnvironment.getProperty("logging.level.com.alipay.test"));
assertEquals("WARN", bootstrapEnvironment.getProperty("logging.level.com.test.demo"));
assertEquals("WARN", applicationEnvironment.getProperty("logging.level.com.test.demo"));
assertEquals("./logs", bootstrapEnvironment.getProperty("logging.path"));
assertEquals("./logs", applicationEnvironment.getProperty("logging.path"));
assertEquals(null, bootstrapEnvironment.getProperty("any.key"));
assertEquals("any.value", applicationEnvironment.getProperty("any.key"));
}

@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
if (SOFABootEnvUtils.isSpringCloudBootstrapEnvironment(configurableApplicationContext
.getEnvironment())) {
bootstrapEnvironment = configurableApplicationContext.getEnvironment();
bootstrapContext.incrementAndGet();
return;
}
applicationEnvironment = configurableApplicationContext.getEnvironment();
applicatioinContext.incrementAndGet();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ spring.application.name=infra-test

#this application log level
logging.level.com.alipay.test=INFO
logging.level.com.test.demo=WARN

#color show:you can delete
spring.output.ansi.enabled=ALWAYS
Expand All @@ -32,5 +33,6 @@ logging.path=./logs
# In NORMAL mode, use soft balancing supported by config server
run.mode=NORMAL

any.key=any.value


52 changes: 38 additions & 14 deletions runtime-sofa-boot-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
<artifactId>runtime-sofa-boot-starter</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand All @@ -56,28 +62,33 @@
<configuration>
<priority>1500</priority>
<pluginName>${ark.plugin.name}</pluginName>
<imported>
</imported>
<activator>com.alipay.sofa.runtime.integration.activator.SofaRuntimeActivator</activator>

<exported>
<packages>
<package>com.alipay.sofa.runtime.api.*</package>
<package>com.alipay.sofa.runtime.model</package>
<package>com.alipay.sofa.runtime.client.*</package>
<package>com.alipay.sofa.runtime.component.*</package>
<package>com.alipay.sofa.runtime.constants.*</package>
<package>com.alipay.sofa.runtime.integration.*</package>
<package>com.alipay.sofa.runtime.model.*</package>
<package>com.alipay.sofa.runtime.service.component</package>
<package>com.alipay.sofa.runtime.service.helper</package>
<package>com.alipay.sofa.runtime.spi.client</package>
<package>com.alipay.sofa.runtime.spi.component</package>
<package>com.alipay.sofa.runtime.spi.health</package>
<package>com.alipay.sofa.runtime.spi.log</package>
<package>com.alipay.sofa.runtime.spi.binding</package>
<package>com.alipay.sofa.runtime.spi.service</package>
<package>com.alipay.sofa.runtime.spi.util</package>
<package>com.alipay.sofa.runtime.client.impl</package>
<package>com.alipay.sofa.runtime.component.impl</package>
<package>org.aopalliance.aop</package>
<package>org.aopalliance.intercept</package>
</packages>

<classes>
<class>com.alipay.sofa.runtime.service.component.AbstractContract</class>
<class>com.alipay.sofa.runtime.service.component.Reference</class>
<class>com.alipay.sofa.runtime.service.component.Service</class>
<class>com.alipay.sofa.runtime.spi.binding.AbstractBinding</class>
<class>com.alipay.sofa.runtime.spi.binding.Binding</class>
<class>com.alipay.sofa.runtime.spi.binding.Contract</class>
<class>com.alipay.sofa.runtime.service.binding.JvmBinding</class>
<class>com.alipay.sofa.runtime.SofaFramework</class>
<class>com.alipay.sofa.runtime.SofaRuntimeProperties</class>
</classes>
</exported>

Expand All @@ -87,9 +98,22 @@
<excludeGroupId>org.apache.tomcat.embed</excludeGroupId>
</excludeGroupIds>

<excludes>
<exclude>com.alipay.sofa:infra-sofa-boot-starter</exclude>
</excludes>
<excludeArtifactIds>
<excludeArtifactId>hibernate-validator</excludeArtifactId>
<excludeArtifactId>classmate</excludeArtifactId>
<excludeArtifactId>jboss-logging</excludeArtifactId>
<excludeArtifactId>jackson-annotations</excludeArtifactId>
<excludeArtifactId>jackson-core</excludeArtifactId>
<excludeArtifactId>jackson-databind</excludeArtifactId>
<excludeArtifactId>jcl-over-slf4j</excludeArtifactId>
<excludeArtifactId>jul-to-slf4j</excludeArtifactId>
<excludeArtifactId>log4j-over-slf4j</excludeArtifactId>
<excludeArtifactId>logback-core</excludeArtifactId>
<excludeArtifactId>logback-classic</excludeArtifactId>
<excludeArtifactId>snakeyaml</excludeArtifactId>
<excludeArtifactId>validation-api</excludeArtifactId>
</excludeArtifactIds>

</configuration>
</execution>
</executions>
Expand Down
Loading

0 comments on commit 0e3d94b

Please sign in to comment.