Introduction to Apollo in Spring project (learning notes)

Posted by bailo81 on Tue, 22 Feb 2022 14:52:56 +0100

Introduction to Apollo

Apollo (Apollo) is a distributed configuration center developed by Ctrip framework department. It can centrally manage the configuration of different application environments and different clusters. After the configuration is modified, it can be pushed to the application end in real time. It also has standardized permissions, process governance and other characteristics. It is suitable for micro service configuration management scenarios.
For details, please refer to GitHub: (all in English)

Import dependency

	<!--Apollo configuration center dependency-->

Note that when importing, you should rely on both client and core, otherwise the following errors may appear:

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.ctrip.framework.apollo.tracer.Tracer
      at com.ctrip.framework.apollo.ConfigService.getManager(
      at com.ctrip.framework.apollo.ConfigService.getConfig(

The picture shows the details of Apollo page

to configure

When using java to connect to apollo, users need to create an app Properties is in the META-INF folder, and META-INF must be under resources. In addition, aopollo env properties.

Appollo. Appollo is the unique ID of each item in the configuration center In properties, you need to determine which project configuration information you need to use

Because apollo also needs to remotely connect the data of apollo configuration information in java, it can be added to apollo Meta = {IP}: 8080, because the apollo port is generally 8070, but the port we obtain data is generally 8080. For this, we need to know their configuration with the operation and maintenance department. This method can also obtain the specific data of apollo. It seems that we can connect to apollo to obtain the data. It feels like the apollo env in the previous figure Properties doesn't seem to work, but in actual development, we can't have only one environment. There is a development environment for development, a test environment for testing, and a production environment for production. Therefore, we generally don't write apollo Meta, using the app in the figure above ID, different environments only have different servers, but we keep app ID is consistent. is used to configure different environments and access different Apollo configuration centers. Apollo allows users to configure four different environments: dev development environment, fat function test environment, uat user test environment and pro production environment.

It seems that there is another problem. How does the server know what environment it is in? Therefore, it also needs a configuration to tell apollo what environment its computer is in

Therefore, developers need to configure server in the folder of C:\opt\settings properties.

On the server In properties, tell apollo what environment it is in:

env=FAT Functional test environment
env=UAT User test environment
env=PRO production environment 

There are four environments in apollo. If you are a developer, you can honestly configure the DEV environment

Get remote configuration

After the remote configuration can be obtained, it needs to be configured in spring before it can be used

public class AppPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
try {
//Get all configuration information from apollo
Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
Set<String> fieldnames = config.getPropertyNames();
//Traverse configuration information
for(String fieldname : fieldnames){
String attributeName=fieldname;
String attributeValue = config.getProperty(fieldname,"");
System.out.println("attributeName:"+attributeName + "; attributeValue:" + attributeValue );
} catch (Exception e) {
e.printStackTrace();"obtain apollo Configuration failed");

super.processProperties(beanFactoryToProcess, props);

In this way, you can store all the configurations in the spring container, but don't forget to configure the xml file.
Follow up is learning...

Topics: Java Spring