Configuration center

What is a configuration center

Configuration is a mechanism used to dynamically modify the behavior of a program

Why use a configuration center

Security: Configuration is kept in the code base along with the source code, which is prone to configuration leakage. Validity: The modification takes effect only after the service is restarted. Limitations: Dynamic adjustment, such as log switch and function switch, is not supported. Therefore, the distributed configuration center came into being!

Configure the center type

Apollo, Java development —– high operation and maintenance cost ratio

Apollo is divided into four modules: MySQL, Config Service, Admin Service and Portal. MySQL stores Apollo metadata and user configuration data. The Config Service provides functions such as reading and pushing configurations. Client requests are sent to the Config Service. Admin Service The Admin Service is used by the Portal to modify and publish configurations. Portal Provides the user configuration management page. Powerful, active community, but more complex, more deployed components, high o&M cost ratio

Consul, go development

Dependent: Does not depend on other components-In-App/out-app: Belongs to an external application and is less intrusive ACP principle: Complies with CP principle (consistency + separation tolerance). Service registration is slow. Due to its consistency, the real Consul is unavailable during the re-election when the Leader fails. Version iteration: version iteration is still implemented. Integration supported: SpringCloud K8S integration access protocol: HTTP/DNS Avalanche protection: avalanche protection is not supported. Integration: SpringCloud integration, K8S integration

Automatic logout example: does not support the interface: English interface, not in line with the people used to start: a bit complex

Nacos, rely on: mysql —–

Rely on: mysql application: internal/external belongs to the external application of invasive small ACP principle: notice follow the principle of CP + separation tolerance (consistency) and the principle of the AP (availability + separation tolerance) version iterations: still version iteration, recently submitted a few days ago integrated support: Supports Dubbo, SpringCloud, and K8S integrated access protocols: HTTP, dynamic DNS, and UDP Avalanche protection: avalanche protection is supported

Spring Cloud Config Java development —– Net support is poor

Automatic logout example: support interface: Domestic services, Chinese interface, in line with the people used to start: Consul is actually a similar product to Nacos. Although Consul’s current focus is on Service Mesh, Service discovery and configuration management are two of Nacos’s main features that Consul initially supports. Although Nacos opened source with a similar deployment architecture after Consul, this does not mean that Nacos mimics Consul in terms of features and architecture, which are derived from a decade of operational evolution experience within Alibaba. Therefore, the comparison of the two will certainly make people understand that their positioning and evolution direction is completely different.

How do I use Consul Configuration Center in Microservices

Consul configuration center download address

Consul address has been used

Consul specifies the configuration center operating principle

As shown in figure

Consul uses a single service single configuration

conditions

1. Team micro-service system

2, Consul

steps

1, RuanMou. MicroService. Download the Core project Nuget Winton. Extensions. The Configuration. The Consul

2. Configure consul address in the configuration file

"Consul_Url" : "http://127.0.0.1:8500",Copy the code

3. Configuration in Program file

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); webBuilder.ConfigureAppConfiguration((hostingContext, Config) = > {/ / load the default Configuration information to the Configuration hostingContext. Configuration = config. The Build (); / / load the consul Configuration center Configuration string consul_url = hostingContext. The Configuration (" consul_url "); Console.WriteLine($"consul_url:{consul_url}"); config.AddConsul( "appsettings.json", options => { options.ConsulConfigurationOptions = cco => { cco.Address = new Uri(consul_url); }; } // 1, Consul address options.Optional = true; // 2. Set options.ReloadOnChange = true; OnLoadException = exceptionContext => {exceptionContext.Ignore = true; }; }); hostingContext.Configuration = config.Build(); }}}}}}}}}}}}}}}}}}}} });Copy the code

4. Dynamically load configuration information

app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync(Configuration["Name"]);
                });
            });	
Copy the code

Consul uses a single configuration with multiple services

conditions

1, the Consul

2. Microservice system

steps

1, RuanMou. MicroService. Download the Core project Nuget Winton. Extensions. The Configuration. The Consul

2. Configure consul address in the configuration file

"Consul_Url" : "http://127.0.0.1:8500",Copy the code

3. Configuration in Program file

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); webBuilder.ConfigureAppConfiguration((hostingContext, Config) = > {/ / load the default Configuration information to the Configuration hostingContext. Configuration = config. The Build (); / / load the consul Configuration center Configuration string consul_url = hostingContext. The Configuration (" consul_url "); Console.WriteLine($"consul_url:{consul_url}"); / / dynamic loading environment information, mainly lies in the dynamic gain. The name of the service and environment var env = hostingContext HostingEnvironment; config.AddConsul( $"{env.ApplicationName}/appsettings.{env.EnvironmentName}.json", options => { options.ConsulConfigurationOptions = cco => { cco.Address = new Uri(consul_url); }; } // 1, Consul address options.Optional = true; // 2. Set options.ReloadOnChange = true; OnLoadException = exceptionContext => {exceptionContext.Ignore = true; }; }); hostingContext.Configuration = config.Build(); }}}}}}}}}}}}}}}}}}}} });Copy the code

4. Dynamically load configuration information

app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync(Configuration["Name"]);
                });
            });	
Copy the code

Consul uses multiple services with multiple configurations

conditions

1, the Consul

2. Microservice system

steps

1, RuanMou. MicroService. Download the Core project Nuget Winton. Extensions. The Configuration. The Consul

2. Configure consul address in the configuration file

"Consul_Url" : "http://127.0.0.1:8500",Copy the code

3. Configuration in Program file

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); webBuilder.ConfigureAppConfiguration((hostingContext, Config) = > {/ / load the default Configuration information to the Configuration hostingContext. Configuration = config. The Build (); / / load the consul Configuration center Configuration string consul_url = hostingContext. The Configuration (" consul_url "); Console.WriteLine($"consul_url:{consul_url}"); / / dynamic loading environment information, mainly lies in the dynamic gain. The name of the service and environment var env = hostingContext HostingEnvironment; config.AddConsul( $"{env.ApplicationName}/appsettings.{env.EnvironmentName}.json", options => { options.ConsulConfigurationOptions = cco => { cco.Address = new Uri(consul_url); }; } // 1, Consul address options.Optional = true; // 2. Set options.ReloadOnChange = true; OnLoadException = exceptionContext => {exceptionContext.Ignore = true; }; }); config.AddConsul( $"{env.ApplicationName}/other.json", options => { options.ConsulConfigurationOptions = cco => { cco.Address = new Uri(consul_url); }; } // 1, Consul address options.Optional = true; // 2. Set options.ReloadOnChange = true; OnLoadException = exceptionContext => {exceptionContext.Ignore = true; }; }); hostingContext.Configuration = config.Build(); }}}}}}}}}}}}}}}}}}}} });Copy the code

4. Dynamically load configuration information

app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync(Configuration["Name"]);
                });
            });	
Copy the code