Apollo is a distributed configuration center developed by the Framework department of Ctrip. It can centrally manage configurations in different environments and clusters, and push configurations to applications in real time after modification. It has standardized permissions and process governance features, and is suitable for micro-service configuration management scenarios. The server is developed based on Spring Boot and Spring Cloud, and can run directly after packaging, without additional installation of Application containers such as Tomcat.

Apollo open Source address: github.com/ctripcorp/a…

See the Apollo Configuration Center introduction for more product information. This article describes how to use the Apollo Configuration Center in a.NET environment.

Quick Start

  • Local deployment: github.com/ctripcorp/a…
  • Docker deployment: github.com/ctripcorp/a…
  • Distributed deployment: github.com/ctripcorp/a…

For convenience, I choose to deploy Docker this time for quick application.

It should also be noted that Quick Start, whether deployed in Docker mode or regular mode, is just a Quick Start to learn about Apollo. If deploying Apollo is used in your company, refer to Distributed Deployment.

Please make sure that the development environment has docker environment, and then download docker-compose. Yml and the required SQL file, and then execute docker-compose up in the root directory, one execution will trigger the operation such as downloading the image, you need to wait some time.

version: '2'

services:
  apollo-quick-start:
    image: nobodyiam/apollo-quick-start
    container_name: apollo-quick-start
    depends_on:
      - apollo-db
    ports:
      - "8080:8080"
      - "8070:8070"
    links:
      - apollo-db

  apollo-db:
    image: Mysql: 5.7
    container_name: apollo-db
    environment:
      TZ: Asia/Shanghai
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    depends_on:
      - apollo-dbdata
    ports:
      - "13306:3306"
    volumes:
      - ./sql:/docker-entrypoint-initdb.d
    volumes_from:
      - apollo-dbdata

  apollo-dbdata:
    image: alpine:latest
    container_name: apollo-dbdata
    volumes:
      - /var/lib/mysql
Copy the code

Search for all apollo-quick-start logs. If the following logs are displayed, the startup is successful:

apollo-quick-start    | Waiting for config service startup.....
apollo-quick-start    | Config service started. You may visit http://localhost:8080 for service status now!
apollo-quick-start    | Waiting for admin service startup.
apollo-quick-start    | Admin service started
apollo-quick-start    | ==== starting portal ====
apollo-quick-start    | Portal logging file is ./portal/apollo-portal.log
apollo-quick-start    | Started [239]
apollo-quick-start    | Waiting for portal startup....
apollo-quick-start    | Portal started. You can visit http://localhost:8070 now!
Copy the code

The database port is mapped to 13306, so if you want to access the database from the host, you can use localhost:13306, user name is root, and password is left blank. To see more service logs, log in to docker exec-it Apollo-quick-start bash. Then go to /apollo-quick-start/service and /apollo-quick-start/portal to view log information.

Visit http://localhost:8070 to see ApolloUI interface, default account password: Apollo /admin

The.net Core access

To create a new ASP.NET Core API project, ApolloDemo, add the component Com. Ctrip. Framework. Apollo. The Configuration.

Install-Package Com.Ctrip.Framework.Apollo.Configuration
Copy the code

Apollo Configuration Center.net integration package open source address: github.com/ctripcorp/a…

The Apollo service address is configured in appsettings.json.

{
  "Apollo": {
    "AppId": "ApolloDemo"."Env": "DEV"."MetaServer": "http://localhost:8080"."ConfigServer": [ "http://localhost:8080"]}}Copy the code

The configuration can be found in the documentation: github.com/ctripcorp/a…

Why address port 8080 instead of 8070?

Because in docker-comedy.yml we expose two ports: 8080 8070, which is our Apollo configuration center administration interface, and port 8080, which is the Spring Eureka service registry. Therefore, the server address should be configured.

Then apply the configuration to program.cs with the following code:

using Com.Ctrip.Framework.Apollo;
using Com.Ctrip.Framework.Apollo.Logging;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace ApolloDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, builder) =>
                {
                    LogManager.UseConsoleLogging(LogLevel.Trace);

                    builder.AddApollo(builder.Build().GetSection("Apollo")).AddDefault(); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }}Copy the code

We then create a new project ApolloDemo in the Apollo administration interface with the same name as the AppId configured in appSettings. json.

Apollo has a core concept: Namespace.

  • NamespaceA collection of configuration items, similar to the concept of a configuration file.
  • NamespaceThere are three types of types: private types, public types, and associated types (inherited types).
  • NamespaceAccess permissions are divided into two types: private (private) and public (public), where the access permissions are relative to Apollo client.

Configuration files are available in various formats, such as Properties, XML, YML, YAML, JSON, etc. Namespace also has these formats. In the Portal UI, you can see a “Properties” TAB on the Namespace of “Application”, indicating that “Application” is in properties format.

The namespace the format of the properties, the client needs to call when using ConfigService. GetConfigFile (String namespace, ConfigFileFormat ConfigFileFormat) to obtain, If you call the namespace using the Http interface, add the suffix to the namespace name, for example, datasources.json. Apollo-client 1.3.0 starts with better yamL/YML support, using the same format as properties: Config Config = configService.getConfig (“application.yml”); And Spring’s injection is the same as properties.

Documentation for Namespace: github.com/ctripcorp/a…

Go ahead and add a few configuration properties and publish them.

Now write an interface to read configuration in Apollo in real time.

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace ApolloDemo.Controllers{[Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase{[HttpGet]
        [Route("Apollo")]
        public IActionResult ApolloTest([FromServices] IConfiguration configuration, string key)
        {
            return Content(configuration.GetValue<string>(key)); }}}Copy the code

And it’s very simple to use, because it gets the value that we configured in Apollo, depending on the key that was passed in, and it’s hot updated, and it changes dynamically in real time.

Then change the values of name and age, and see in the call interface. Note that changes made in Apollo need to be published to take effect.

The Apollo configuration Center for.NET Core is available at Github, where you can check out the Github Wiki for more information.