1. Introduction to Nacos

Nacos is dedicated to helping you discover, configure, and manage microservices. Nacos provides an easy-to-use feature set that helps you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management.

Nacos helps you build, deliver, and manage microservices platforms more agile and easily. Nacos is the service infrastructure for building modern “service” centric application architectures (e.g., microservices paradigm, cloud native paradigm).

1.1 nacOS features

  • Service discovery and service health monitoring

Nacos supports DNs-based and RPC-based service discovery. After a Service provider registers a Service using a native SDK, OpenAPI, or a separate Agent TODO, Service consumers can use DNS TODO or HTTP&API to find and discover services. Nacos provides real-time health checks on services to prevent requests from being sent to unhealthy hosts or service instances. Nacos supports health checks at the transport layer (PING or TCP) and the application layer (e.g. HTTP, MySQL, user-defined). Nacos provides two health check modes: Agent report mode and server active check mode for complex cloud environments and network topologies, such as VPCS and edge networks. Nacos also provides a unified health check dashboard to help you manage service availability and traffic based on health status.

  • Dynamically configured service

Dynamically configured services allow you to manage application configuration and service configuration for all environments in a centralized, external, and dynamic manner.

Dynamic configuration eliminates the need to redeploy applications and services when configuration changes, making configuration management more efficient and agile.

Centralized configuration management makes it easier to implement stateless services and make it easier for services to scale flexibly on demand.

Nacos provides an easy-to-use UI (sample console Demo) to help you manage the configuration of all your services and applications. Nacos also provides a number of out-of-the-box configuration management features including configuration version tracking, Canary publishing, one-click rollback configuration, and client configuration update status tracking to help you more securely manage configuration changes and reduce the risks associated with configuration changes in a production environment.

  • Dynamic DNS Service

The dynamic DNS service supports weighted routing, enabling you to implement load balancing at the middle layer, flexible routing policies, traffic control, and simple DNS resolution services on the data center Intranet. Dynamic DNS services also make it easier for you to implement DNS protocol-based service discovery to help eliminate the risk of coupling to vendor-proprietary service discovery apis.

Nacos provides some simple DNS APIs TODO to help you manage your service’s associated domain name and available IP:PORT list.

  • Services and their metadata management

Nacos enables you to manage all services and metadata in your data center from a microservices platform construction perspective, including managing service descriptions, life cycles, static dependency analysis of services, health of services, traffic management of services, routing and security policies, SLA of services, and most importantly metrics statistics.

1.2 NACOS Ecology

2. Add nacOS dependencies

  <! -- Dubbo Registry Nacos -->
      <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-registry-nacos</artifactId>
        <version>${dubbo-registry-nacos}</version>
      </dependency>
      <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
        <version>${com.alibaba.nacos}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>${dubbo-registry-nacos}</version>
      </dependency>

      <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-config-spring-boot-starter</artifactId>
        <version>${nacos.config.version}</version>
      </dependency>

      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        <version>${spring.cloud.starter.alibaba.nacos.config.version}</version>
      </dependency>
Copy the code

3. Add the configuration

Registering with NACOS requires the use of bootstrap.yml

Properties bootstrap.yml Application. properties application.yml Application

The bootstrap. Yml:

Spring: Application: name: test-service-center cloud: nacos: discovery: 127.0.0.1:8841127.00 0.1:8842 # # to register a service registry to registry - enabled: true config: server - addr: 127.0.0.1:8841127.00 0.1:8842 file - the extension: yaml # specify prefix for yaml file configuration file type: test # - service - center manually specify prefix group configuration file: DEFAULT_GROUP profiles: active: PRD # indicates the development environmentCopy the code

4. Build Nacos cluster

Refer to the scaffolding method in the previous article.

5. Create a configuration file

The test – service – the center – dev. Yaml:

Test: user: Zhang SAN 11111Copy the code

The test – service – the center – PRD. Yaml:

Test: user: Zhang SAN 22222Copy the code

6. Modify the test class

    @Value(value = "${test.user}")
    private String userName;


    @Override
    public String sayHello(String str) {
        return "from server , i am : " + userName;
    }
Copy the code

7, test,

Modify the bootstrap. Yml profiles. Active