Doing development for so many years, often due to configuration problems caused by the production environment bug. Some years long project, hundreds of dense configuration items, often easy to confuse, sometimes several projects have a lot of the same configuration items, configuration work is also a bother. Fortunately, Ctrip opens up a new generation of configuration center-the Apollo Project, which has recently been used for some time in the project, and it feels really convenient.
First, the local Apollo, please refer to the official website information Https://github.com/ctripcorp/apollo/wiki/Quick-Start.
Enter the management UI for Apollo and create the project:
Once the project is created, you can add key and value to the project:
After you add a key, you need to publish to take effect.
Add a Com.Ctrip.Framework.Apollo.Configuration NuGet package to a. NET Core project (is it written by a Java programmer?). ), modify the Program.cs file and the Appsettings.json file, Appsettings.json file AppID corresponds to the application in the above Id,metaserver address of the Euraka of Apollo:
public static iwebhost buildwebhost ( string [] args) => Webhost.createdefaultbuilder (args). Configureappconfiguration (Hostingcontext, builder) => { Builder. Addapollo (builder. Build (). GetSection ( " apollo " <startup> (). Build ();
{ "Apollo": { "AppId": "SampleApp", "metaserver": "/HTTP/ 192.168.2.168:8080 " }}
This makes it easy to read the configuration items in Apollo in the project.
What if there are some public configuration items that need to be referenced by several projects? Namespace shines! Portal: The "Namespace" of the Apollo core concept
The configuration items that create pulic types of namespace,namespace are visible to each project, and the only thing you need to do is to publish the configuration items in the namespace in a project, and you need to change the Program.cs code again:
Public StaticIwebhost Buildwebhost (string[] args) =Webhost.createdefaultbuilder (args). Configureappconfiguration (Hostingcontext, builder)={Builder. Addapollo (builder. Build (). GetSection ("Apollo")) . Adddefault (). AddNamespace ("Common");//the name of namespace in Apollo }) . Usestartup<Startup>() . Build ();
. NET core uses Apollo for unified configuration Management