Help Center/ Cloud Service Engine/ Best Practices/ Registry/Configuration Centers/ Connecting Spring Cloud Eureka Applications to Nacos Engines
Updated on 2024-09-30 GMT+08:00

Connecting Spring Cloud Eureka Applications to Nacos Engines

This section uses a demo to demonstrate how to connect Spring Cloud Eureka applications to Nacos engines.

Connect a provider service and a consumer service to a Nacos engine.

Prerequisites

  • You have created a Nacos engine. For details, see Creating a Registry/Configuration Center.
  • 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.

Restrictions

  • Nacos is compatible with Eureka APIs on the Eureka server side and saves and updates client instance information registered on the service side. Therefore, if you use only Eureka as the registry center, many features of Nacos, such as namespace and configuration management, cannot be used.
  • Eureka is used as the client and can be viewed only on the Service Management page of Nacos. Eureka services are displayed using the default attributes of Nacos.
    • Default namespace: public
    • Default group: DEFAULT_GROUP
  • The value of Protection Threshold for creating a service on the Service Management page of Nacos is a feature of Nacos and does not apply to the Eureka service.

Connecting Spring Cloud Eureka Applications to Nacos Engines

  1. Log in to CSE.
  2. Obtain the registry center 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 registry center address and microservice name in the demo.

    1. Configure the Nacos service center address and microservice name in the application.properties file.
      • Find the eureka-demo-master\eureka-consumer\src\main\resources\application.properties file in the demo source code directory downloaded to the local host and configure the consumer service.
        server.port=9001
        spring.application.name= eureka-client-consumer //Microservice name
        eureka.client.serviceUrl.defaultZone= XXX.nacos.cse.com:8848/nacos/eureka //Service center address of Nacos
        eureka.instance.lease-renewal-interval-in-seconds=15 //Service heartbeat update interval
        eureka.client.registry-fetch-interval-seconds=15 //Interval for pulling the registry center. It is recommended that the value be the same as the heartbeat interval.
      • Find the eureka-demo-master\eureka-provider\src\main\resources\application.properties file in the demo source code directory downloaded to the local host and configure the provider service.
        server.port=9000
        spring.application.name= eureka-client-provider //Microservice name
        eureka.client.serviceUrl.defaultZone= XXX.nacos.cse.com:8848/nacos/eureka //Service center address of Nacos
        eureka.instance.lease-renewal-interval-in-seconds=15 //Service heartbeat update interval
        eureka.client.registry-fetch-interval-seconds=15 //Interval for pulling the registry center. It is recommended that the value be the same as the heartbeat interval.

  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

      Software Package Directory

      Software Package Name

      Description

      \eureka-consumer\target

      eureka-client-consumer-1.0.0-SNAPSHOT.jar

      Service consumer

      \eureka-provider\target

      eureka-client-provider-1.0.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 Prerequisites.
    2. Choose Service Management and check the number of instances of microservices eureka-client-consumer and eureka-client-provider.
      • If Instances is not 0, the demo has been connected to the Nacos engine.
      • If Instances is 0 or the eureka-client-consumer and eureka-client-provider services cannot be found, the demo fails to be connected to the Nacos engine.