Skip to content

Commit 0e3d94b

Browse files
QilongZhangstraybirdzls
authored andcommitted
Cherry pick bug fix from 3.x (sofastack#274)
1 parent 3c1321c commit 0e3d94b

File tree

9 files changed

+225
-39
lines changed

9 files changed

+225
-39
lines changed

infra-sofa-boot-starter/src/main/java/com/alipay/sofa/infra/constants/CommonMiddlewareConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class CommonMiddlewareConstants {
2929
*/
3030
public static final String APP_NAME_KEY = "spring.application.name";
3131

32+
public static final String SOFA_BOOTSTRAP = "sofaBootstrap";
33+
3234
/**
3335
* {@link org.springframework.boot.ResourceBanner#getVersionsMap}
3436
*/
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.infra.listener;
18+
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.concurrent.atomic.AtomicBoolean;
23+
24+
import org.springframework.boot.Banner;
25+
import org.springframework.boot.SpringApplication;
26+
import org.springframework.boot.builder.SpringApplicationBuilder;
27+
import org.springframework.boot.context.config.ConfigFileApplicationListener;
28+
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
29+
import org.springframework.context.ApplicationListener;
30+
import org.springframework.core.Ordered;
31+
import org.springframework.core.env.*;
32+
import org.springframework.util.ClassUtils;
33+
import org.springframework.util.StringUtils;
34+
35+
import com.alipay.sofa.infra.constants.CommonMiddlewareConstants;
36+
import com.alipay.sofa.infra.utils.SOFABootEnvUtils;
37+
38+
/**
39+
* @author qilong.zql
40+
* @since 3.0.0
41+
*/
42+
public class SofaBootstrapRunListener implements
43+
ApplicationListener<ApplicationEnvironmentPreparedEvent>,
44+
Ordered {
45+
private final static String LOGGING_PATH = "logging.path";
46+
private final static String LOGGING_LEVEL = "logging.level";
47+
private static AtomicBoolean executed = new AtomicBoolean(false);
48+
49+
/**
50+
* config log settings
51+
*/
52+
private void assemblyLogSetting(ConfigurableEnvironment environment) {
53+
if (StringUtils.hasText(environment.getProperty(LOGGING_PATH))) {
54+
System.getProperties().setProperty(LOGGING_PATH, environment.getProperty(LOGGING_PATH));
55+
}
56+
for (PropertySource propertySource : environment.getPropertySources()) {
57+
if (!(propertySource instanceof EnumerablePropertySource)) {
58+
continue;
59+
}
60+
for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) {
61+
if (key.startsWith(LOGGING_LEVEL)) {
62+
System.setProperty(key, environment.getProperty(key));
63+
}
64+
}
65+
}
66+
}
67+
68+
/**
69+
* config required properties
70+
* @param environment
71+
*/
72+
private void assemblyRequireProperties(ConfigurableEnvironment environment) {
73+
if (StringUtils.hasText(environment.getProperty(CommonMiddlewareConstants.APP_NAME_KEY))) {
74+
System.getProperties().setProperty(CommonMiddlewareConstants.APP_NAME_KEY,
75+
environment.getProperty(CommonMiddlewareConstants.APP_NAME_KEY));
76+
}
77+
}
78+
79+
/**
80+
* Mark this environment as SOFA bootstrap environment
81+
* @param environment
82+
*/
83+
private void assemblyEnvironmentMark(ConfigurableEnvironment environment) {
84+
environment.getPropertySources().addFirst(
85+
new MapPropertySource(CommonMiddlewareConstants.SOFA_BOOTSTRAP,
86+
new HashMap<String, Object>()));
87+
}
88+
89+
/**
90+
* Un-Mark this environment as SOFA bootstrap environment
91+
* @param environment
92+
*/
93+
private void unAssemblyEnvironmentMark(ConfigurableEnvironment environment) {
94+
environment.getPropertySources().remove(CommonMiddlewareConstants.SOFA_BOOTSTRAP);
95+
}
96+
97+
@Override
98+
public int getOrder() {
99+
return HIGHEST_PRECEDENCE;
100+
}
101+
102+
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
103+
ConfigurableEnvironment environment = event.getEnvironment();
104+
SpringApplication application = event.getSpringApplication();
105+
if (SOFABootEnvUtils.isSpringCloud() && executed.compareAndSet(false, true)) {
106+
StandardEnvironment bootstrapEnvironment = new StandardEnvironment();
107+
for (PropertySource<?> source : event.getEnvironment().getPropertySources()) {
108+
if (source instanceof PropertySource.StubPropertySource) {
109+
continue;
110+
}
111+
bootstrapEnvironment.getPropertySources().addLast(source);
112+
}
113+
List<Class> sources = new ArrayList<>();
114+
for (Object s : application.getSources()) {
115+
if (s instanceof Class) {
116+
sources.add((Class) s);
117+
} else if (s instanceof String) {
118+
sources.add(ClassUtils.resolveClassName((String) s, null));
119+
}
120+
}
121+
SpringApplication bootstrapApplication = new SpringApplicationBuilder()
122+
.profiles(environment.getActiveProfiles()).bannerMode(Banner.Mode.OFF)
123+
.environment(bootstrapEnvironment).sources(sources.toArray(new Class[] {}))
124+
.registerShutdownHook(false).logStartupInfo(false).web(false).listeners()
125+
.initializers().build(event.getArgs());
126+
ApplicationEnvironmentPreparedEvent bootstrapEvent = new ApplicationEnvironmentPreparedEvent(
127+
bootstrapApplication, event.getArgs(), bootstrapEnvironment);
128+
for (ApplicationListener listener : application.getListeners()) {
129+
if (listener instanceof ConfigFileApplicationListener) {
130+
listener.onApplicationEvent(bootstrapEvent);
131+
}
132+
}
133+
assemblyLogSetting(bootstrapEnvironment);
134+
assemblyRequireProperties(bootstrapEnvironment);
135+
assemblyEnvironmentMark(environment);
136+
} else {
137+
unAssemblyEnvironmentMark(environment);
138+
}
139+
}
140+
}

infra-sofa-boot-starter/src/main/java/com/alipay/sofa/infra/utils/SOFABootEnvUtils.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
*/
1717
package com.alipay.sofa.infra.utils;
1818

19-
import org.slf4j.Logger;
20-
import org.slf4j.LoggerFactory;
19+
import com.alipay.sofa.infra.constants.CommonMiddlewareConstants;
2120
import org.springframework.context.ApplicationContextInitializer;
2221
import org.springframework.core.env.ConfigurableEnvironment;
2322
import org.springframework.core.env.Environment;
23+
import org.springframework.util.ClassUtils;
2424

2525
/**
2626
* SOFABootEnvUtils
@@ -30,13 +30,7 @@
3030
*/
3131
public class SOFABootEnvUtils {
3232

33-
/**
34-
* org.springframework.cloud.bootstrap.BootstrapApplicationListener#BOOTSTRAP_PROPERTY_SOURCE_NAME
35-
*/
36-
private static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = "bootstrap";
37-
38-
private static final Logger LOGGER = LoggerFactory
39-
.getLogger(SOFABootEnvUtils.class);
33+
private final static String SPRING_CLOUD_MARK_NAME = "org.springframework.cloud.bootstrap.BootstrapConfiguration";
4034

4135
/**
4236
* Determine whether the {@link org.springframework.core.env.Environment} is Spring Cloud bootstrap environment.
@@ -52,14 +46,14 @@ public class SOFABootEnvUtils {
5246
*/
5347
public static boolean isSpringCloudBootstrapEnvironment(Environment environment) {
5448
if (environment instanceof ConfigurableEnvironment) {
55-
ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment;
56-
if (configurableEnvironment.getPropertySources().contains(
57-
BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
58-
//use app logger
59-
LOGGER.debug("Current application context environment is bootstrap");
60-
return true;
61-
}
49+
return !((ConfigurableEnvironment) environment).getPropertySources().contains(
50+
CommonMiddlewareConstants.SOFA_BOOTSTRAP)
51+
&& isSpringCloud();
6252
}
6353
return false;
6454
}
65-
}
55+
56+
public static boolean isSpringCloud() {
57+
return ClassUtils.isPresent(SPRING_CLOUD_MARK_NAME, null);
58+
}
59+
}

infra-sofa-boot-starter/src/main/resources/META-INF/spring.factories

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
77
com.alipay.sofa.infra.autoconfigure.SofaBootInfraAutoConfiguration
88

99
# EnvironmentPostProcessor
10-
org.springframework.boot.env.EnvironmentPostProcessor=com.alipay.sofa.infra.env.EnvironmentCustomizer
10+
org.springframework.boot.env.EnvironmentPostProcessor=com.alipay.sofa.infra.env.EnvironmentCustomizer
11+
12+
# SpringApplicationListener
13+
org.springframework.context.ApplicationListener=com.alipay.sofa.infra.listener.SofaBootstrapRunListener

infra-sofa-boot-starter/src/test/java/com/alipay/sofa/infra/base/AbstractTestBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.boot.test.web.client.TestRestTemplate;
2525
import org.springframework.context.ApplicationContext;
2626
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27+
import org.springframework.test.context.junit4.SpringRunner;
2728

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

infra-sofa-boot-starter/src/test/java/com/alipay/sofa/infra/utils/SOFABootEnvUtilsTest.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package com.alipay.sofa.infra.utils;
1818

1919
import com.alipay.sofa.infra.base.AbstractTestBase;
20+
import com.alipay.sofa.infra.constants.CommonMiddlewareConstants;
2021
import org.junit.Test;
2122
import org.springframework.context.ApplicationContextInitializer;
2223
import org.springframework.context.ConfigurableApplicationContext;
24+
import org.springframework.core.env.ConfigurableEnvironment;
2325
import org.springframework.core.env.Environment;
2426

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

41-
private static AtomicLong bootstrapContext = new AtomicLong(0L);
43+
private static AtomicLong bootstrapContext = new AtomicLong(0L);
4244

43-
private static AtomicLong applicatioinContext = new AtomicLong(0L);
45+
private static AtomicLong applicatioinContext = new AtomicLong(0L);
46+
47+
private static ConfigurableEnvironment bootstrapEnvironment;
48+
49+
private static ConfigurableEnvironment applicationEnvironment;
4450

4551
/**
4652
* Method: isSpringCloudBootstrapEnvironment(Environment environment)
4753
*/
4854
@Test
49-
public void testIsSpringCloudBootstrapEnvironment() throws Exception {
55+
public void testIsSpringCloudBootstrapEnvironment() {
5056
Environment environment = ctx.getEnvironment();
5157
assertFalse(SOFABootEnvUtils.isSpringCloudBootstrapEnvironment(environment));
5258
assertEquals(1L, bootstrapContext.get());
5359
assertEquals(1L, applicatioinContext.get());
5460
assertFalse(SOFABootEnvUtils.isSpringCloudBootstrapEnvironment(null));
61+
assertEquals("infra-test",
62+
bootstrapEnvironment.getProperty(CommonMiddlewareConstants.APP_NAME_KEY));
63+
assertEquals("infra-test",
64+
applicationEnvironment.getProperty(CommonMiddlewareConstants.APP_NAME_KEY));
65+
assertEquals("INFO", bootstrapEnvironment.getProperty("logging.level.com.alipay.test"));
66+
assertEquals("INFO", applicationEnvironment.getProperty("logging.level.com.alipay.test"));
67+
assertEquals("WARN", bootstrapEnvironment.getProperty("logging.level.com.test.demo"));
68+
assertEquals("WARN", applicationEnvironment.getProperty("logging.level.com.test.demo"));
69+
assertEquals("./logs", bootstrapEnvironment.getProperty("logging.path"));
70+
assertEquals("./logs", applicationEnvironment.getProperty("logging.path"));
71+
assertEquals(null, bootstrapEnvironment.getProperty("any.key"));
72+
assertEquals("any.value", applicationEnvironment.getProperty("any.key"));
5573
}
5674

5775
@Override
5876
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
5977
if (SOFABootEnvUtils.isSpringCloudBootstrapEnvironment(configurableApplicationContext
6078
.getEnvironment())) {
79+
bootstrapEnvironment = configurableApplicationContext.getEnvironment();
6180
bootstrapContext.incrementAndGet();
6281
return;
6382
}
83+
applicationEnvironment = configurableApplicationContext.getEnvironment();
6484
applicatioinContext.incrementAndGet();
6585
}
66-
}
86+
}

infra-sofa-boot-starter/src/test/resources/config/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ spring.application.name=infra-test
2020

2121
#this application log level
2222
logging.level.com.alipay.test=INFO
23+
logging.level.com.test.demo=WARN
2324

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

36+
any.key=any.value
3537

3638

runtime-sofa-boot-plugin/pom.xml

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
<artifactId>runtime-sofa-boot-starter</artifactId>
4040
<version>2.5.1</version>
4141
</dependency>
42+
<dependency>
43+
<groupId>aopalliance</groupId>
44+
<artifactId>aopalliance</artifactId>
45+
<version>1.0</version>
46+
<optional>true</optional>
47+
</dependency>
4248
</dependencies>
4349

4450
<build>
@@ -56,28 +62,33 @@
5662
<configuration>
5763
<priority>1500</priority>
5864
<pluginName>${ark.plugin.name}</pluginName>
59-
<imported>
60-
</imported>
65+
<activator>com.alipay.sofa.runtime.integration.activator.SofaRuntimeActivator</activator>
66+
6167
<exported>
6268
<packages>
6369
<package>com.alipay.sofa.runtime.api.*</package>
64-
<package>com.alipay.sofa.runtime.model</package>
70+
<package>com.alipay.sofa.runtime.client.*</package>
71+
<package>com.alipay.sofa.runtime.component.*</package>
72+
<package>com.alipay.sofa.runtime.constants.*</package>
73+
<package>com.alipay.sofa.runtime.integration.*</package>
74+
<package>com.alipay.sofa.runtime.model.*</package>
75+
<package>com.alipay.sofa.runtime.service.component</package>
76+
<package>com.alipay.sofa.runtime.service.helper</package>
6577
<package>com.alipay.sofa.runtime.spi.client</package>
6678
<package>com.alipay.sofa.runtime.spi.component</package>
6779
<package>com.alipay.sofa.runtime.spi.health</package>
6880
<package>com.alipay.sofa.runtime.spi.log</package>
81+
<package>com.alipay.sofa.runtime.spi.binding</package>
82+
<package>com.alipay.sofa.runtime.spi.service</package>
6983
<package>com.alipay.sofa.runtime.spi.util</package>
70-
<package>com.alipay.sofa.runtime.client.impl</package>
71-
<package>com.alipay.sofa.runtime.component.impl</package>
84+
<package>org.aopalliance.aop</package>
85+
<package>org.aopalliance.intercept</package>
7286
</packages>
7387

7488
<classes>
75-
<class>com.alipay.sofa.runtime.service.component.AbstractContract</class>
76-
<class>com.alipay.sofa.runtime.service.component.Reference</class>
77-
<class>com.alipay.sofa.runtime.service.component.Service</class>
78-
<class>com.alipay.sofa.runtime.spi.binding.AbstractBinding</class>
79-
<class>com.alipay.sofa.runtime.spi.binding.Binding</class>
80-
<class>com.alipay.sofa.runtime.spi.binding.Contract</class>
89+
<class>com.alipay.sofa.runtime.service.binding.JvmBinding</class>
90+
<class>com.alipay.sofa.runtime.SofaFramework</class>
91+
<class>com.alipay.sofa.runtime.SofaRuntimeProperties</class>
8192
</classes>
8293
</exported>
8394

@@ -87,9 +98,22 @@
8798
<excludeGroupId>org.apache.tomcat.embed</excludeGroupId>
8899
</excludeGroupIds>
89100

90-
<excludes>
91-
<exclude>com.alipay.sofa:infra-sofa-boot-starter</exclude>
92-
</excludes>
101+
<excludeArtifactIds>
102+
<excludeArtifactId>hibernate-validator</excludeArtifactId>
103+
<excludeArtifactId>classmate</excludeArtifactId>
104+
<excludeArtifactId>jboss-logging</excludeArtifactId>
105+
<excludeArtifactId>jackson-annotations</excludeArtifactId>
106+
<excludeArtifactId>jackson-core</excludeArtifactId>
107+
<excludeArtifactId>jackson-databind</excludeArtifactId>
108+
<excludeArtifactId>jcl-over-slf4j</excludeArtifactId>
109+
<excludeArtifactId>jul-to-slf4j</excludeArtifactId>
110+
<excludeArtifactId>log4j-over-slf4j</excludeArtifactId>
111+
<excludeArtifactId>logback-core</excludeArtifactId>
112+
<excludeArtifactId>logback-classic</excludeArtifactId>
113+
<excludeArtifactId>snakeyaml</excludeArtifactId>
114+
<excludeArtifactId>validation-api</excludeArtifactId>
115+
</excludeArtifactIds>
116+
93117
</configuration>
94118
</execution>
95119
</executions>

0 commit comments

Comments
 (0)