Updated on 2024-04-11 GMT+08:00

Connecting Spring Cloud Applications to Nacos Engines

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

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

Prerequisites

  • You have created a Nacos engine. For details, see Creating a Nacos Engine.
  • You have downloaded the demo source code from GitHub to the local host and decompressed it.
  • 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.

Procedure

  1. Log in to CSE.

    1. Log in to the management console.
    2. Click and select a region.
    3. Click in the upper left corner and select Cloud Service Engine in the service list. The CSE console is displayed.

  2. Obtain the service center address of the exclusive 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 Naocs 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 1.
      Table 1 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 the Spring Cloud application.

    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 Prerequisites.
    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.