(30) rapid installation and use of asp.net core3.1 integrated Apollo

Time:2021-10-22

1. Introduction

Apollo (Apollo) is a production level configuration center product developed and open sourced by Ctrip framework department. It can centrally manage the configuration of applications in different 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.

2. Architecture and modules

The following is the Apollo architecture module diagram (from the wave micro class):

The figure above briefly describes the overall design of Apollo. We can see it from bottom to top:
● config service provides functions such as reading and pushing configuration, and the service object is Apollo client.
● admin service provides configuration modification, publishing and other functions, and the service object is Apollo portal (Management Interface).
● both config service and admin service are multi instance and stateless deployment, so you need to register yourself in Eureka and keep the heartbeat.
● on Eureka, we built a layer of meta server to encapsulate Eureka’s service discovery interface.
● the client accesses the meta server through the domain name to obtain the config service service list (IP + port), and then directly accesses the service through IP + port. At the same time, the client side will do load balance and error retry.
● the portal accesses the meta server through the domain name to obtain the admin service list (IP + port), and then directly accesses the service through the IP + port. At the same time, it will do load balance and error retry on the portal side.
● in order to simplify deployment, we will actually deploy the three logical roles of config service, Eureka and meta server in the same JVM process.

2.1 user’s real-time push design after configuration release

In the configuration center, an important function is to push the configuration to the client in real time after publishing. Let’s take a brief look at how this is designed and implemented:
● users can configure publishing in portal operation.
● portal invokes the interface of admin service for operation publishing.
● after the admin service publishes the configuration, it sends a releasemessage to each config service.
● after receiving the releasemessage, the config service notifies the corresponding client.

2.2 implementation principle of Apollo client

● the client and the server maintain a long connection, so that the push of configuration update can be obtained at the first time (realized through HTTP long polling).
● the client will regularly pull the latest configuration of the application from the server of Apollo configuration center.
  ○ this is a fallback mechanism to prevent the configuration from being updated due to the failure of the push mechanism.
  ○ the client will report the local version when pulling regularly, so generally, the server will return 304 – not modified for the operation of pulling regularly.
  ○ the timing frequency is pulled every 5 minutes by default. The client can also override it by specifying system property: apollo.refreshinterval at runtime, with the unit of minutes.
● after the client obtains the latest configuration of the application from the server of Apollo configuration center, it will be saved in memory.
● the client will cache a copy of the configuration obtained from the server in the local file system.
  ○ when the service is unavailable or the network is unavailable, the configuration can still be restored locally without affecting the normal operation of the application. The local cache path is located in C: \ opt \ data \ {appid} \ config cache, so please ensure that the C: \ opt \ data \ directory exists and the application has read and write permissions.
● the application can obtain the latest configuration and subscribe to the configuration update notification from the Apollo client.

2.3 environment configuration

Apollo supports that applications have different configurations in different environments, so environment is another important information to obtain configuration from the server. The environment is specified through the configuration file. The file location is C: \ opt \ settings \ server.properties. The file content is as follows:
env=DEV
Currently, env supports the following values (case insensitive):
●DEV:Development environment
●FAT:Feature Acceptance Test environment
●UAT:User Acceptance Test environment
●PRO:Production environment

3. Apollo quick start on Windows

3.1 preparation

3.1.1 Java jdk

● Apollo server: 1.8+
● Apollo client: 1.7+
  Java JDK1.8 download address: https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html , please be reminded that there must be no spaces in the name of the installation directory, otherwise an error will be reported when Apollo starts.
After configuration, you can check through the following command:

java -version


Configure the following environment variables:

3.1.2MySQL

● version requirements: 5.6.5 + (I won’t talk about MySQL server installation, but Baidu)
Apollo’s table structure uses multiple default declarations for timestamp, so it requires versions above 5.6.5.
After connecting to MySQL, you can check through the following command:

SHOW VARIABLES WHERE Variable_name = 'version';

3.1.3 download the quick start installation package

Download the Apollo installation package. You only need to download it locally and you can use it directly, eliminating the compilation and packaging process.

● GitHub Download:https://github.com/nobodyiam/apollo-build-scripts
● Baidu network disk download:https://pan.baidu.com/share/init?surl=Ieelw6y3adECgktO0ea0Gg , extraction code: 9wwe
The installation package is as large as 58m because it is a self bootable jar package, which contains all dependent jar packages and a built-in Tomcat container. After extracting the Apollo installation package, you will see the following files:

3.2 installation steps

3.2.1 creating database

The Apollo server requires two databases: Apollo PORTALDB and Apollo configdb. We have prepared SQL files for the creation of databases, tables and sample data respectively. We only need to import the database.
Create Apollo PORTALDB:Import through various MySQL clientssql/apolloportaldb.sqlJust.
Take MySQL Native Client as an example:

source /your_local_path/sql/apolloportaldb.sql

● create Apollo configdb:Import through various MySQL clientssql/apolloconfigdb.sqlJust.
Take MySQL Native Client as an example:

source /your_local_path/sql/apolloconfigdb.sql
3.2.2 configuring database connection information

The Apollo server needs to know how to connect to the database you created earlier, so you need to edit demo.sh in the Apollo directory and modify the database connection string information related to Apollo PORTALDB and Apollo configdb:

# apollo config db info
apollo_config_db_url=jdbc:mysql://172.168.16.xxx:5621/ApolloConfigDB?characterEncoding=utf8
apollo_ config_ db_ Username = username
apollo_ config_ db_ Password = password

# apollo portal db info
apollo_portal_db_url=jdbc:mysql://172.168.16.xxx:5621/ApolloPortalDB?characterEncoding=utf8
apollo_ portal_ db_ Username = username
apollo_ portal_ db_ Password = password

3.3 start Apollo configuration center

The quick start script will start three services locally, using ports 8070, 8080 and 8090 respectively. Please ensure that these three ports are not currently used.
● under linux / Mac, you can check through the following commands:

lsof -i:8080

● under window, you can check through the following commands:

netstat -aon|findstr 8080

First switch to the Apollo Directory:

cd D:\StudyDocuments\technology\Apollo\apollo-build-scripts-master

Then execute the startup script:

./demo.sh start


Because I deploy the Apollo server locally, I open it directly http://localhost:8070 You can:

The default administrator user name is Apollo and password is admin. You can log in successfully by entering it! The default port of Eureka is 8080, which is open http://localhost:8080/ You can:

Apollo how to create and modify user passwords, create users, departments, assign permissions and other operations, I won’t say. We can learn from Baidu or go to the official website, which is not within the scope of this article.

4. Asp.net core integration and rapid development of Apollo

4.1 Pollo environment configuration

Apollo has two organization departments by default. You can add and modify organizations by logging in to the Administrator Tool – system parameter and entering the key value. Return to the main interface, click create project, and enter relevant project information according to your own project. The demo project information here is as follows:

After creating a project, click in, and the following error message may be reported:

This error is generally caused by incorrect configuration parameters of Eureka service in the database! Because I deploy the Apollo server locally, there is already a site in IIS that occupies the default port 8080 of Eureka service, so after stopping the site, enter demo.sh stop-demo.sh start to restart Apollo. After restarting Apollo, you may also be prompted with the following information:

Because the project lacks environment namespaces configuration, click the “add namespace” option on the left to add a dev development environment namespaces:


After creating namespaces, add aspnetcore.apollo.test.webapi project development environment configuration:

The above configuration is equivalent to the following configuration in the core project appsettings.json:

Then click publish.

4.2 asp.net core integration Apollo

Via GitHub https://github.com/ctripcorp/apollo.net It can be seen from the source code that the sample aspnetcore.apollo.test.webapi application can add the Apollo environment configuration information through the appsettings.json file:

{
  "apollo": {
    "AppId": "AspNetCore.Apollo.Test.WebApi",
    //"Cluster": "test", // / no cluster, temporarily hidden
    "MetaServer": "http://localhost:8080/",
    //"Secret": "ffd9d01130ee4329875ac3441c0bedda"
    //"Namespaces": ["application. XML", "application. JSON", "application. YML", "application. Yaml", "application"], // / no namespace array, temporarily hidden
    "Namespace": "dev",
    "Env": "Dev",
    "Meta": {
      "DEV": "http://localhost:8080/"
      //"FAT": "http://106.54.227.205:8080/",
      //"UAT": "http://106.54.227.205:8080/",
      //"PRO": "http://106.54.227.205:8080/"
      ////The example is only the development environment for the time being
    }
  }
}

Then, reference com.ctrip.framework.apollo.configuration nuget package at the main program entrance of the program, add Apollo environment configuration information and namespace name. The specific configuration code is as follows:

.ConfigureAppConfiguration(builder =>
{
    var iConfigurationRoot = builder.Build();
    var apolloInfo = iConfigurationRoot.GetSection("apollo");
    var namespaceName = iConfigurationRoot.GetSection("apollo").GetSection("Namespace").Value;
    builder.AddApollo(apolloInfo).AddNamespace(namespaceName);
})

Then run the sample aspnetcore.apollo.test.webapi application, and you will see the following debugging information:


From the above debugging information, we can see that Apollo is successfully integrated in asp.net core!

reference:
Apollo
Apollo Quick-Start
Apollo configuration center design
Microservice architecture ~ analysis of Ctrip Apollo configuration center architecture
. net client user guide