Help Center/ Cloud Service Engine/ Getting Started/ Connecting Spring Cloud Applications to Nacos Engines Using Spring Cloud SDK
Updated on 2024-09-30 GMT+08:00

Connecting Spring Cloud Applications to Nacos Engines Using Spring Cloud SDK

This section uses a demo to demonstrate how to connect microservice applications to Nacos engines.

  1. Preparations

    You need to register with Huawei Cloud and complete real-name authentication, top up your account, grant permissions, create a VPC and subnet, obtain the demo package, and prepare the local host for compilation, building, and packaging.

  2. Creating a Registry/Configuration Center

    You can select engine specifications, AZs, and networks when creating an engine.

  3. Connecting Spring Cloud Applications to Nacos Engines

    This section describes how to connect a provider service and a consumer service to a Nacos engine.

Preparations

  1. Sign up for a HUAWEI ID and complete real-name authentication.
    Skip this step if you already have a HUAWEI ID, or perform the following steps to create one.
    1. Log in to the Huawei Cloud official website and click Sign Up.
    2. Sign up for a HUAWEI ID. For details, see Signing up for a HUAWEI ID and Enabling Huawei Cloud Services.

      After your ID is created, the system redirects you to your personal information page.

    3. Complete real-name authentication. For details, see Individual Real-Name Authentication.

      Real-name authentication is required only when you buy or use resources in the Chinese mainland.

  2. Top up your account.

    Ensure that your account has sufficient balance.

  3. Grant permissions.

    You must have the required permissions to create dependent resources and Nacos engines. For details, see Creating a User and Granting Permissions.

    To create a registry/configuration center, you must have the CSE FullAccess and DNS FullAccess permissions.

  4. Create a VPC and subnet.

    Nacos engines run in a VPC and need to be bound to a subnet. You must have a VPC and subnet to create Nacos engines. For details, see Creating a VPC. Skip this step if you already have a VPC and subnet.

  5. The Java JDK and Maven have been installed on the local host for compilation, building, and packaging, and the Maven central library can be accessed.
  6. Download the demo source code from GitHub to the local host and decompress it.

Creating a Registry/Configuration Center

  1. Go to the Buy Registry/Configuration Center Instance page.
  2. In the left navigation pane, choose Registry/Configuration Center.
  3. Click Buy Registry/Configuration Center Instance.
  4. Set parameters according to the following table. Parameters marked with an asterisk (*) are mandatory. For details, see Creating a Registry/Configuration Center.

    Table 1 Creating a registry/configuration center

    Parameter

    Description

    *Billing Mode

    Billing mode. Currently, Pay-per-use is supported. Pay-per-use is postpaid. You use registry/configuration centers and then pay as billed for your usage duration.

    *Enterprise Project

    Select the enterprise project where the Nacos engine is located. Enterprise projects let you manage cloud resources and users by project. default is selected by default.

    *Name

    Enter a Nacos engine name. The name contains 3 to 64 characters, including letters, digits, and hyphens (-), and starts with a letter but cannot end with a hyphen (-). For example, nacos-test.

    *Registry/Configuration Center Instance

    The registry/configuration center supports Nacos engines.

    NOTE:

    Cluster nodes in the registry/configuration center are evenly distributed to different AZs. A failure of a single node does not affect external services. The registry/configuration center does not support AZ-level DR but provides host-level DR.

    *Instances

    Select the required capacity specifications. In this example, the number of instances is 500, and the number of capacity units is 10.

    Version

    Only the latest version can be created.

    *Network

    Select the created VPC and subnet. You can search for and select a VPC and subnet from the drop-down list.

    A VPC enables you to provision logically isolated, configurable, and manageable virtual networks for your engine.

  5. Click Buy Now. The registry/configuration center starts to be created. When the status is Available, the registry/configuration center is created.

Connecting Spring Cloud Applications to Nacos Engines

  1. Log in to CSE.
  2. Obtain the registry and discovery address of a Nacos engine.

    1. In the left navigation pane, choose Registry/Configuration Center and click the Nacos engine instance.
    2. In the Connection Information area on the Basic Information page, obtain the service center address.

  3. Change the configuration center address, service center address, and microservice name in the demo.

    1. Configure the Nacos configuration center in bootstrap.properties.
      Find the nacos-examples-master\nacos-spring-cloud-example\nacos-spring-cloud-discovery-example\nacos-spring-cloud-consumer-example\src\main\resources file in the demo source code directory downloaded to the local host, add the bootstrap.properties file, and configure the Nacos configuration center.
      spring.cloud.nacos.config.server-addr= XXX.nacos.cse.com:8848 //Address of the Nacos configuration center
      spring.cloud.nacos.config.prefix= example //Prefix of the configuration file name
      spring.cloud.nacos.config.file-extension= properties //Extension of the configuration file name
      spring.cloud.nacos.config.group= XXX //Group to which the configuration file belongs. If this parameter is left blank, the default value DEFAULT_GROUP  is used.
      spring.cloud.nacos.config.namespace= XXX  //ID of the namespace to which the configuration file belongs. If this parameter is left blank, the default value public is used.
    2. Configure the Nacos service center address and microservice name in the application.properties file.
      • Find the nacos-examples-master\nacos-spring-cloud-example\nacos-spring-cloud-discovery-example\nacos-spring-cloud-consumer-example\src\main\resources\application.properties file in the demo source code directory downloaded to the local host and configure the consumer service.
        server.port=8080
        spring.application.name= service-consumer //Microservice name
        spring.cloud.nacos.discovery.server-addr= XXX.nacos.cse.com:8848 //Nacos service center address
        spring.cloud.nacos.discovery.group= XXX  //Group to which the microservice belongs. If this parameter is left blank, the default value DEFAULT_GROUP is used.
        spring.cloud.nacos.discovery.namespace= XXX  //ID of the namespace to which the microservice belongs. If this parameter is left blank, the default value public is used.
        spring.cloud.nacos.discovery.cluster-name= XXX  //Name of the cluster to which the microservice belongs. If this parameter is left blank, the default value DEFAULT is used.
      • Find the nacos-examples-master\nacos-spring-cloud-example\nacos-spring-cloud-discovery-example\nacos-spring-cloud-provider-example\src\main\resources\application.properties file in the demo source code directory downloaded to the local host and configure the provider service.
        server.port=8070
        spring.application.name= service-provider //Microservice name
        spring.cloud.nacos.discovery.server-addr= XXX.nacos.cse.com:8848 //Nacos service center address
        spring.cloud.nacos.discovery.group= XXX  //Group to which the microservice belongs. If this parameter is left blank, the default value DEFAULT_GROUP is used.
        spring.cloud.nacos.discovery.namespace= XXX  //ID of the namespace to which the microservice belongs. If this parameter is left blank, the default value public is used.
        spring.cloud.nacos.discovery.cluster-name= XXX  //Name of the cluster to which the microservice belongs. If this parameter is left blank, the default value DEFAULT is used.

  4. Pack the demo source code into a JAR package.

    1. In the root directory of the demo source code, open the Command Prompt and run the mvn clean package command to package and compile the project.
    2. After the compilation is successful, two JAR packages are generated, as shown in Table 2.
      Table 2 Software packages

      Directory Where the Software Package Is Located

      Package Name

      Description

      \nacos-spring-cloud-consumer-example\target

      nacos-spring-cloud-consumer-example-0.2.0-SNAPSHOT.jar

      Service consumer

      \nacos-spring-cloud-provider-example\target

      nacos-spring-cloud-provider-example-0.2.0-SNAPSHOT.jar

      Service provider

  5. Deploy Spring Cloud applications.

    Deploy provider and consumer on the ECS node in the VPC where the Nacos engine is located.
    1. Create an ECS node in the VPC where the engine instance is located and log in to the ECS node. For details, see Purchasing and Logging In to a Linux ECS.
    2. Install JRE to provide a running environment for services.
    3. Upload the JAR package generated in 4 to the ECS node.
    4. Run the java -jar {JAR package} command to run the generated JAR package.

  6. Confirm the deployment results.

    1. Optional: On the CSE console, choose Registry/Configuration Center and click the Nacos engine created in Creating a Registry/Configuration Center.
    2. Choose Service Management and check the number of instances of microservices service-consumer and service-provider.
      • If Instances is not 0, the demo has been connected to the Nacos engine.
      • If Instances is 0 or the service-consumer and service-provider services cannot be found, the demo fails to be connected to the Nacos engine.