==============
implementation “com.ctrip.framework.apollo:apollo-client:1.6.0”
2、配置 application.properties
===========================
apollo 自身的配置共包含 9 项,必要配置只有 3 项,其他的都是可选的配置。apollo 在 spring-boot 环境下的配置命名和 System 参数的命名保持了一直,最终 spring 的配置会注入到 System 中,具体的逻辑下文分析。
必须配置
====
#应用的ID
app.id = java-project
apollo 的 config-service 服务发现地址
apollo.meta = http://apollo.meta
启用 apollo
apollo.bootstrap.enabled = true
可选配置
====
在日志系统初始化前加载 apollo 配置
apollo.bootstrap.eagerLoad.enabled=true
加载的命名空间,默认加载 application ,多个以逗号隔开
apollo.bootstrap.namespaces = application
apollo 的安全拉取 secret 配置
apollo.accesskey.secret = xx
集群配置
apollo.cluster = hk
缓存路径
apollo.cacheDir = /opt
是否保持和 apollo 配置页面的配置顺序一致
apollo.property.order.enable = true
加载过程解析
======
public class ApolloApplicationContextInitializer implements
ApplicationContextInitializer , EnvironmentPostProcessor, Ordered {
public static final int DEFAULT_ORDER = 0;
private static final Logger logger = LoggerFactory.getLogger(ApolloApplicationContextInitializer.class);
private static final Splitter NAMESPACE_SPLITTER = Splitter.on(“,”).omitEmptyStrings().trimResults();
private static final String[] APOLLO_SYSTEM_PROPERTIES = {“app.id”, ConfigConsts.APOLLO_CLUSTER_KEY,
“apollo.cacheDir”, “apollo.accesskey.secret”, ConfigConsts.APOLLO_META_KEY, PropertiesFactory.APOLLO_PROPERTY_ORDER_ENABLE};
private final ConfigPropertySourceFactory configPropertySourceFactory = SpringInjector.getInstance(ConfigPropertySourceFactory.class);
private int order = DEFAULT_ORDER;
@Override
public void initialize(ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = context.getEnvironment();
if (!environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, Boolean.class, false)) {
logger.debug(“Apollo bootstrap config is not enabled for context {}, see property: ${{}}”, context, PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED);
return;
}
logger.debug(“Apollo bootstrap config is enabled for context {}”, context);
initialize(environment);
}
/**
-
Initialize Apollo Configurations Just after environment is ready.
-
@param environment
*/
protected void initialize(ConfigurableEnvironment environment) {
if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
//already initialized
return;
}
String namespaces = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION);
logger.debug(“Apollo bootstrap namespaces: {}”, namespaces);
List namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces);
CompositePropertySource composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
for (String namespace : namespaceList) {
Config config = ConfigService.getConfig(namespace);
composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));
}
environment.getPropertySources().addFirst(composite);
}
/**
- To fill system properties from environment config
*/
void initializeSystemProperty(ConfigurableEnvironment environment) {
for (String propertyName : APOLLO_SYSTEM_PROPERTIES) {
fillSystemPropertyFromEnvironment(environment, propertyName);
}
}
private void fillSystemPropertyFromEnvironment(ConfigurableEnvironment environment, String propertyName) {
if (System.getProperty(propertyName) != null) {
return;
}
String propertyValue = environment.getProperty(propertyName);
if (Strings.isNullOrEmpty(propertyValue)) {
return;
}
System.setProperty(propertyName, propertyValue);
}
/**
-
In order to load Apollo configurations as early as even before Spring loading logging system phase,
-
this EnvironmentPostProcessor can be called Just After ConfigFileApplicationListener has succeeded.
-
-
The processing sequence would be like this:
-
Load Bootstrap properties and application properties -----> load Apollo configuration properties ----> Initialize Logging systems
-
@param configurableEnvironment
-
@param springApplication
*/
@Override
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
// should always initialize system properties like app.id in the first place
initializeSystemProperty(configurableEnvironment);
Boolean eagerLoadEnabled = configurableEnvironment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, Boolean.class, false);
//EnvironmentPostProcessor should not be triggered if you don’t want Apollo Loading before Logging System Initialization
if (!eagerLoadEnabled) {
return;
}
Boolean bootstrapEnabled = configurableEnvironment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, Boolean.class, false);
最后
即使是面试跳槽,那也是一个学习的过程。只有全面的复习,才能让我们更好的充实自己,武装自己,为自己的面试之路不再坎坷!今天就给大家分享一个Github上全面的Java面试题大全,就是这份面试大全助我拿下大厂Offer,月薪提至30K!
我也是第一时间分享出来给大家,希望可以帮助大家都能去往自己心仪的大厂!为金三银四做准备!
一共有20个知识点专题,分别是:
Dubbo面试专题
JVM面试专题
Java并发面试专题
Kafka面试专题
MongDB面试专题
MyBatis面试专题
MySQL面试专题
Netty面试专题
RabbitMQ面试专题
Redis面试专题
Spring Cloud面试专题
SpringBoot面试专题
zookeeper面试专题
常见面试算法题汇总专题
计算机网络基础专题
设计模式专题
ing Cloud面试专题**
[外链图片转存中…(img-7crV5VTp-1723361685003)]
SpringBoot面试专题
[外链图片转存中…(img-e6pwpqu3-1723361685003)]
zookeeper面试专题
[外链图片转存中…(img-E8aCt4DA-1723361685004)]
常见面试算法题汇总专题
[外链图片转存中…(img-64TY4S4b-1723361685004)]
计算机网络基础专题
[外链图片转存中…(img-bT2Jdet7-1723361685005)]
设计模式专题
[外链图片转存中…(img-YQyjTKU3-1723361685005)]