Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of integrating Apollo with ASP.NET Core

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points of ASP.NET Core's method of integrating Apollo. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

1. Introduction

Apollo (Apollo) is a production-level configuration center product developed and open source by Ctrip Framework Department. It can centrally manage the configuration of applications in different environments and clusters, and can be pushed to the application side in real time after configuration modification. It also has standardized permissions, process governance and other features, so it is suitable for micro-service configuration management scenarios.

two。 Architecture and Modul

The following is an Apollo architecture module diagram:

The figure above briefly describes the overall design of Apollo, which we can look at from the bottom up:

Config Service provides configured read, push and other functions, and the service object is the Apollo client.

Admin Service provides configuration modification, release and other functions, and the service object is Apollo Portal (management interface).

Both Config Service and Admin Service are multi-instance, stateless deployments, so you need to register yourself with Eureka and keep your heart beating.

We set up a layer of Meta Server service discovery interface on top of Eureka to encapsulate Eureka.

Client obtains the list of Config Service services (IP+Port) by accessing Meta Server by domain name, and then accesses the service directly through IP+Port. At the same time, load balance and error retry will be done on the client side.

Portal obtains the list of Admin Service services (IP+Port) by accessing Meta Server by domain name, and then accesses the service directly through IP+Port. At the same time, load balance and error retry will be done on the Portal side.

To simplify deployment, we will actually deploy the three logical roles 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 configure real-time push to the client after release. Let's take a brief look at how this piece is designed and implemented:

The user configures the publication in the Portal operation.

Portal calls the API of Admin Service to operate the publication.

After Admin Service publishes the configuration, it sends ReleaseMessage to each Config Service.

When Config Service receives the ReleaseMessage, it notifies the corresponding client.

The implementation principle of 2.2Apollo client

The client and the server maintain a long connection so that they can get the push of configuration updates (via Http Long Polling) as soon as possible.

The client also periodically pulls the latest configuration of the application from the Apollo configuration center server.

This is a fallback mechanism to prevent the configuration from being updated due to the failure of the push mechanism.

Regular pull by the client will report the local version, so in general, the server will return 304-Not Modified for the scheduled pull operation.

The timing frequency defaults to pulling every 5 minutes, and the client can also override it by specifying System Property: apollo.refreshInterval at run time, in minutes.

After the client obtains the latest configuration of the application from the Apollo configuration center server, it will be saved in memory.

The client caches a copy of the configuration obtained from the server on the local file system.

When the service is unavailable or the network is not available, 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 make sure that the C:\ opt\ data\ directory exists and that the application has read and write permissions.

Applications can get the latest configuration and subscribe to configuration update notifications from the Apollo client.

2.3 Environment configuration (Environment)

Apollo supports different configurations for applications in different environments, so Environment is another important way to get configuration information from the server. Environment is specified through the configuration file. The file location is C:\ opt\ settings\ server.properties, and 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 launch 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, a reminder here, the installation directory name must not have a space, otherwise Apollo startup will report an error.

After it is configured, you can check it with the following command:

Java-version

Configure the environment variables:

3.1.2MySQL

Version requirements: 5.6.5 + (MySQL server installation I will not say, own Baidu)

Apollo's table structure uses multiple default declarations for timestamp, so version 5.6.5 is required.

After connecting to the MySQL, you can check it with the following command:

SHOW VARIABLES WHERE Variable_name = 'version'

3.1.3 download Quick start installation package

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

Github download: https://github.com/nobodyiam/apollo-build-scripts

The installation package is as big as 58m because it is a self-bootable jar package that contains all dependent jar packages and a built-in tomcat container. After unpacking the Apollo installation package, you will see the following files:

3.2 installation step 3.2.1 create a database

The Apollo server needs two databases: ApolloPortalDB and ApolloConfigDB. We have prepared sql files for database, table creation and sample data respectively, and we only need to import them into the database.

Create ApolloPortalDB: import sql/apolloportaldb.sql through various MySQL clients.

Take the MySQL native client as an example:

Source / your_local_path/sql/apolloportaldb.sql

Create ApolloConfigDB: import sql/apolloconfigdb.sql through various MySQL clients.

Take the MySQL native client as an example:

Source / your_local_path/sql/apolloconfigdb.sql3.2.2 configuration 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 ApolloPortalDB and ApolloConfigDB:

# apollo config db infoapollo_config_db_url=jdbc:mysql://172.168.16.xxx:5621/ApolloConfigDB?characterEncoding=utf8apollo_config_db_username= username apollo_config_db_password= password # apollo portal db infoapollo_portal_db_url=jdbc:mysql://172.168.16.xxx:5621/ApolloPortalDB?characterEncoding=utf8apollo_portal_db_username= username apollo_portal_db_password= password 3.3 launch Apollo configuration Center

The Quick start script starts 3 services locally, using ports 8070, 8080, and 8090, respectively. Please make sure that these three ports are not currently in use.

Under Linux/Mac, you can check with the following command:

Lsof-iPUR 8080

Under Window, ● can be checked by the following command:

Netstat-aon | findstr 8080

Change to the Apollo directory first:

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 can simply open http://localhost:8070:

Default administrator user name: apollo, password: admin, enter to log in successfully! The default port of Eureka is 8080, which means you can open http://localhost:8080/:

Apollo how to create and change user passwords, create users, departments, assign permissions and other operations, I will not say, everyone can go to Baidu or go to the official website to understand, this is not within the scope of this article.

4.ASP.NET Core Integration Apollo Rapid Development of 4.1Apollo Environment configuration

Apollo has two organizational departments by default, which can be modified by entering the key value organizations by logging in to the administrator tool-system parameters. After returning to the main interface, click to create a project, and enter relevant project information according to your own project. The demo project information on my side is as follows:

After creating the project, clicking in may report the following error message:

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

Because the project lacks the environment namespaces configuration, click the "add Namespace" option on the left to add a dev development environment namespaces:

Add the configuration of AspNetCore.Apollo.Test.WebApi project development environment after namespaces is created:

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

Then click publish.

4.2ASP.NET Core integrated Apollo

You can learn from the https://github.com/ctripcorp/apollo.net source code on Github that the sample AspNetCore.Apollo.Test.WebApi application can add Apollo environment configuration information through the appsettings.json file:

{"apollo": {"AppId": "AspNetCore.Apollo.Test.WebApi", / / "Cluster": "test", / No cluster, temporarily hide "MetaServer": "http://localhost:8080/", / /" Secret "ffd9d01130ee4329875ac3441c0bedda", / No ids4 access Temporarily hide / / "Namespaces": ["application.xml", "application.json", "application.yml", "application.yaml", "application"], / No Namespace array Temporarily hide "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/" / for the time being, only the development environment}

Then reference the Com.Ctrip.Framework.Apollo.Configuration Nuget package at the entrance of the Program main program, add the Apollo environment configuration information, and add the 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:

These are all the contents of this article entitled "ASP.NET Core's method of integrating Apollo". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report