Skip to content

Commit f444093

Browse files
QilongZhangstraybirdzls
authored andcommitted
Fix cloud compatible (sofastack#268)
* fix cloud compatible
1 parent f1707d8 commit f444093

14 files changed

Lines changed: 208 additions & 41 deletions

File tree

healthcheck-sofa-boot-starter/src/main/java/com/alipay/sofa/healthcheck/core/AfterReadinessCheckCallbackProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import com.alipay.sofa.healthcheck.log.SofaBootHealthCheckLoggerFactory;
2020
import com.alipay.sofa.healthcheck.startup.ReadinessCheckCallback;
21-
import com.alipay.sofa.healthcheck.utils.BinaryOperators;
21+
import com.alipay.sofa.infra.utils.BinaryOperators;
2222
import com.alipay.sofa.healthcheck.utils.HealthCheckUtils;
2323
import com.fasterxml.jackson.databind.ObjectMapper;
2424
import org.springframework.beans.factory.annotation.Autowired;

healthcheck-sofa-boot-starter/src/main/java/com/alipay/sofa/healthcheck/core/HealthCheckerProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.alipay.sofa.healthcheck.core;
1818

1919
import com.alipay.sofa.healthcheck.log.SofaBootHealthCheckLoggerFactory;
20-
import com.alipay.sofa.healthcheck.utils.BinaryOperators;
20+
import com.alipay.sofa.infra.utils.BinaryOperators;
2121
import com.alipay.sofa.healthcheck.utils.HealthCheckUtils;
2222
import com.fasterxml.jackson.core.JsonProcessingException;
2323
import com.fasterxml.jackson.databind.ObjectMapper;

healthcheck-sofa-boot-starter/src/main/java/com/alipay/sofa/healthcheck/core/HealthIndicatorProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import com.alipay.sofa.healthcheck.log.SofaBootHealthCheckLoggerFactory;
2020
import com.alipay.sofa.healthcheck.service.SofaBootHealthIndicator;
21-
import com.alipay.sofa.healthcheck.utils.BinaryOperators;
21+
import com.alipay.sofa.infra.utils.BinaryOperators;
2222
import com.alipay.sofa.healthcheck.utils.HealthCheckUtils;
2323
import com.fasterxml.jackson.databind.ObjectMapper;
2424
import org.springframework.beans.factory.annotation.Autowired;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class SofaBootInfraConstants {
3030
public static final String APP_NAME_KEY = "spring.application.name";
3131

3232
public static final String SOFA_DEFAULT_PROPERTY_SOURCE = "sofaConfigurationProperties";
33+
public static final String SOFA_BOOTSTRAP = "sofaBootstrap";
3334

3435
/**
3536
* {@link org.springframework.boot.ResourceBanner#getVersionsMap}

infra-sofa-boot-starter/src/main/java/com/alipay/sofa/infra/env/EnvironmentCustomizer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.alipay.sofa.infra.autoconfigure.SofaBootInfraAutoConfiguration;
2020
import com.alipay.sofa.infra.constants.SofaBootInfraConstants;
21-
import com.alipay.sofa.infra.utils.SOFABootEnvUtils;
2221
import org.springframework.boot.SpringApplication;
2322
import org.springframework.boot.env.EnvironmentPostProcessor;
2423
import org.springframework.core.env.*;
@@ -52,9 +51,7 @@ public void postProcessEnvironment(ConfigurableEnvironment environment,
5251
/**
5352
* set required properties, {@link MissingRequiredPropertiesException}
5453
**/
55-
if (!SOFABootEnvUtils.isSpringCloudBootstrapEnvironment(environment)) {
56-
environment.setRequiredProperties(SofaBootInfraConstants.APP_NAME_KEY);
57-
}
54+
environment.setRequiredProperties(SofaBootInfraConstants.APP_NAME_KEY);
5855
}
5956

6057
/**
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.*;
20+
import java.util.concurrent.atomic.AtomicBoolean;
21+
import java.util.stream.StreamSupport;
22+
23+
import org.springframework.boot.Banner;
24+
import org.springframework.boot.SpringApplication;
25+
import org.springframework.boot.WebApplicationType;
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.ConfigurableEnvironment;
32+
import org.springframework.core.env.MapPropertySource;
33+
import org.springframework.core.env.PropertySource;
34+
import org.springframework.core.env.StandardEnvironment;
35+
import org.springframework.util.ClassUtils;
36+
import org.springframework.util.StringUtils;
37+
38+
import com.alipay.sofa.infra.constants.SofaBootInfraConstants;
39+
import com.alipay.sofa.infra.utils.SOFABootEnvUtils;
40+
41+
/**
42+
* @author qilong.zql
43+
* @since 3.0.0
44+
*/
45+
public class SofaBootstrapRunListener implements
46+
ApplicationListener<ApplicationEnvironmentPreparedEvent>,
47+
Ordered {
48+
49+
private final static String LOGGING_PATH = "logging.path";
50+
private final static String LOGGING_LEVEL = "logging.level";
51+
private static AtomicBoolean executed = new AtomicBoolean(false);
52+
53+
/**
54+
* config log settings
55+
*/
56+
private void assemblyLogSetting(ConfigurableEnvironment environment) {
57+
if (StringUtils.hasText(environment.getProperty(LOGGING_PATH))) {
58+
System.getProperties().setProperty(LOGGING_PATH, environment.getProperty(LOGGING_PATH));
59+
}
60+
StreamSupport.stream(environment.getPropertySources().spliterator(), false)
61+
.filter(propertySource -> propertySource instanceof MapPropertySource)
62+
.map(propertySource -> Arrays
63+
.asList(((MapPropertySource) propertySource).getPropertyNames()))
64+
.flatMap(Collection::stream).filter(key -> key.startsWith(LOGGING_LEVEL)).forEach(
65+
(key) -> System.getProperties().setProperty(key, environment.getProperty(key)));
66+
}
67+
68+
/**
69+
* config required properties
70+
* @param environment
71+
*/
72+
private void assemblyRequireProperties(ConfigurableEnvironment environment) {
73+
if (StringUtils.hasText(environment.getProperty(SofaBootInfraConstants.APP_NAME_KEY))) {
74+
System.getProperties().setProperty(SofaBootInfraConstants.APP_NAME_KEY,
75+
environment.getProperty(SofaBootInfraConstants.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(SofaBootInfraConstants.SOFA_BOOTSTRAP, new HashMap<>()));
86+
}
87+
88+
/**
89+
* Un-Mark this environment as SOFA bootstrap environment
90+
* @param environment
91+
*/
92+
private void unAssemblyEnvironmentMark(ConfigurableEnvironment environment) {
93+
environment.getPropertySources().remove(SofaBootInfraConstants.SOFA_BOOTSTRAP);
94+
}
95+
96+
@Override
97+
public int getOrder() {
98+
return HIGHEST_PRECEDENCE;
99+
}
100+
101+
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
102+
ConfigurableEnvironment environment = event.getEnvironment();
103+
SpringApplication application = event.getSpringApplication();
104+
if (SOFABootEnvUtils.isSpringCloud() && executed.compareAndSet(false, true)) {
105+
StandardEnvironment bootstrapEnvironment = new StandardEnvironment();
106+
for (PropertySource<?> source : event.getEnvironment().getPropertySources()) {
107+
if (source instanceof PropertySource.StubPropertySource) {
108+
continue;
109+
}
110+
bootstrapEnvironment.getPropertySources().addLast(source);
111+
}
112+
113+
List<Class> sources = new ArrayList<>();
114+
for (Object s : application.getAllSources()) {
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+
122+
SpringApplication bootstrapApplication = new SpringApplicationBuilder()
123+
.profiles(environment.getActiveProfiles()).bannerMode(Banner.Mode.OFF)
124+
.environment(bootstrapEnvironment).sources(sources.toArray(new Class[] {}))
125+
.registerShutdownHook(false).logStartupInfo(false).web(WebApplicationType.NONE)
126+
.listeners().initializers().build(event.getArgs());
127+
128+
ApplicationEnvironmentPreparedEvent bootstrapEvent = new ApplicationEnvironmentPreparedEvent(
129+
bootstrapApplication, event.getArgs(), bootstrapEnvironment);
130+
131+
application.getListeners().stream()
132+
.filter(listener -> listener instanceof ConfigFileApplicationListener)
133+
.forEach(listener -> ((ConfigFileApplicationListener) listener)
134+
.onApplicationEvent(bootstrapEvent));
135+
136+
assemblyLogSetting(bootstrapEnvironment);
137+
assemblyRequireProperties(bootstrapEnvironment);
138+
assemblyEnvironmentMark(environment);
139+
} else {
140+
unAssemblyEnvironmentMark(environment);
141+
}
142+
}
143+
}

healthcheck-sofa-boot-starter/src/main/java/com/alipay/sofa/healthcheck/utils/BinaryOperators.java renamed to infra-sofa-boot-starter/src/main/java/com/alipay/sofa/infra/utils/BinaryOperators.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package com.alipay.sofa.healthcheck.utils;
17+
package com.alipay.sofa.infra.utils;
1818

1919
import java.util.function.BinaryOperator;
2020

@@ -23,7 +23,7 @@
2323
* @since 3.0.0
2424
*/
2525
public class BinaryOperators {
26-
public static BinaryOperator<Boolean> andBoolean(){
26+
public static BinaryOperator<Boolean> andBoolean() {
2727
return (a, b) -> a && b;
2828
}
2929
}

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

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

19+
import com.alipay.sofa.infra.constants.SofaBootInfraConstants;
1920
import org.slf4j.Logger;
2021
import org.slf4j.LoggerFactory;
2122
import org.springframework.context.ApplicationContextInitializer;
2223
import org.springframework.core.env.ConfigurableEnvironment;
2324
import org.springframework.core.env.Environment;
25+
import org.springframework.util.ClassUtils;
2426

2527
/**
2628
* SOFABootEnvUtils
@@ -30,13 +32,7 @@
3032
*/
3133
public class SOFABootEnvUtils {
3234

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);
35+
private final static String SPRING_CLOUD_MARK_NAME = "org.springframework.cloud.bootstrap.BootstrapConfiguration";
4036

4137
/**
4238
* Determine whether the {@link org.springframework.core.env.Environment} is Spring Cloud bootstrap environment.
@@ -52,14 +48,14 @@ public class SOFABootEnvUtils {
5248
*/
5349
public static boolean isSpringCloudBootstrapEnvironment(Environment environment) {
5450
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-
}
51+
return !((ConfigurableEnvironment) environment).getPropertySources().contains(
52+
SofaBootInfraConstants.SOFA_BOOTSTRAP)
53+
&& isSpringCloud();
6254
}
6355
return false;
6456
}
57+
58+
public static boolean isSpringCloud() {
59+
return ClassUtils.isPresent(SPRING_CLOUD_MARK_NAME, null);
60+
}
6561
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ com.alipay.sofa.infra.initializer.SOFABootInfrastructureSpringContextInitializer
66
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
77
com.alipay.sofa.infra.autoconfigure.SofaBootInfraAutoConfiguration
88

9-
# EnvironmentPostProcessor
10-
org.springframework.boot.env.EnvironmentPostProcessor=com.alipay.sofa.infra.env.EnvironmentCustomizer
9+
org.springframework.boot.env.EnvironmentPostProcessor=com.alipay.sofa.infra.env.EnvironmentCustomizer
10+
11+
# SpringApplicationListener
12+
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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
import org.springframework.boot.test.web.client.TestRestTemplate;
2424
import org.springframework.boot.web.server.LocalServerPort;
2525
import org.springframework.context.ApplicationContext;
26+
import org.springframework.context.annotation.PropertySource;
27+
import org.springframework.test.context.TestPropertySource;
2628
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
29+
import org.springframework.test.context.junit4.SpringRunner;
2730

2831
/**
2932
* 参考文档: http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/htmlsingle/#boot-features-testing
@@ -38,23 +41,20 @@
3841
* @author guanchaoyang
3942
* @since 2.3.0
4043
*/
41-
@RunWith(SpringJUnit4ClassRunner.class)
42-
@SpringBootTest(classes = SofaBootWebSpringBootApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, properties = { "management.endpoints.web.exposure.include=*" })
44+
@RunWith(SpringRunner.class)
45+
@SpringBootTest(classes = SofaBootWebSpringBootApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
4346
public abstract class AbstractTestBase {
4447

45-
/**
46-
* 8080
47-
*/
48-
@LocalServerPort
49-
private int definedPort;
50-
5148
@Autowired
5249
public TestRestTemplate testRestTemplate;
53-
5450
@Autowired
5551
public ApplicationContext ctx;
56-
5752
protected String urlHttpPrefix;
53+
/**
54+
* 8080
55+
*/
56+
@LocalServerPort
57+
private int definedPort;
5858

5959
@Before
6060
public void setUp() {

0 commit comments

Comments
 (0)