All Products
Search
Document Center

Serverless App Engine:Host Spring Cloud applications to SAE

Last Updated:Dec 18, 2023

The service registration and discovery of Spring Cloud applications are implemented by using components such as Eureka, Consul, and ZooKeeper. This topic describes how to host Spring Cloud applications to Serverless App Engine (SAE), how to implement service registration and service discovery, and how a consumer application consumes services provided by a provider application.

Benefits

The service registration and discovery of Spring Cloud applications are implemented by using components such as Eureka, Consul, ZooKeeper, and Redis. If you host Spring Cloud applications to SAE, you only need to replace the original service registry and configuration center with Alibaba Nacos Discovery to perform service registration and discovery without modifying business code.

The SAE service registry provides all features of Spring Cloud Alibaba Nacos Discovery and relieves you of the dependency on multiple components like Eureka, Consul, ZooKeeper, and Redis.

After you host Spring Cloud applications to SAE, you can focus on building the logic of Spring Cloud applications without worrying about the establishment and maintenance of the registry and configuration center. The features provided by SAE, including auto scaling, quick start and stop of applications in a batch, and application monitoring, can help greatly reduce your development and O&M costs.

Step 1: Obtain a demo

eureka-service-provider and eureka-service-consumer are packages of SAE microservice demo applications. Eureka is integrated into the consumer and provider. The consumer consumes services provided by the provider.

Step 2: Modify the service registry configurations of the provider

To host the cloud-native provider to SAE, you must modify the POM dependency in the provider and specify an IP address for Nacos Server.

Note

You must specify an IP address for Nacos Server in the following scenarios:

  • You host your applications to SAE after they pass the local tests.

  • The SAE service registry is a self-managed Nacos registry.

  1. Add the following dependency to the pom.xml file.

    Open the pom.xml file of the provider. Replace spring-cloud-starter-netflix-eureka-client with spring-cloud-starter-alibaba-nacos-discovery and configure the version of Nacos Server.

    Before the replacement:

    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>                            

    After the replacement:

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>                           
    Note
    • Spring Cloud Greenwich is used in this example. The version of spring-cloud-starter-alibaba-nacos-discovery is 2.1.1.RELEASE.

    • If you use Spring Cloud Finchley, the version of spring-cloud-starter-alibaba-nacos-discovery is 2.0.1.RELEASE.

    • If you use Spring Cloud Edgware, the version of spring-cloud-starter-alibaba-nacos-discovery is 1.5.1.RELEASE. The service of this version has ended. We do not recommend this version. For more information, see Spring Cloud Edgware Release Notes.

  2. Specify an IP address for Nacos Server.

    Open the application.properties file in src\main\resources and specify an IP address for Nacos Server.

    Before the modification:

    spring.application.name=service-provider
    server.port=18081
    eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/                            

    After the modification:

    spring.application.name=service-provider
    server.port=18081
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848                            

    127.0.0.1 is the IP address of Nacos Server. If Nacos Server is deployed on another physical server, change the IP address accordingly. If you have other requirements, add the required configurations in the application.properties file by referring to Configuration items.

  3. Query the application service.

    1. Execute the main function of ProviderApplication in the nacos-service-provider project to start the provider.

    2. Log on to the Nacos Server console that is installed on your computer by visiting 127.0.0.1:8848/nacos. In the left-side navigation pane, choose Service management > Service list.

      Note

      The default username and password for logging on to the Nacos Server console are nacos.

      If service-provider is displayed in Service list, and you can query the details of service-provider, then service-provider is successfully registered.

Step 3: Modify the service registry configurations of the consumer

To host the cloud-native consumer to SAE, you must modify the POM dependency in the consumer and specify an IP address for Nacos Server.

Note

You must specify an IP address for Nacos Server in the following scenarios:

  • You host your applications to SAE after they pass the local tests.

  • The SAE service registry is a self-managed Nacos registry.

  1. Add the following dependency to the pom.xml file.

    Open the pom.xml file of the provider. Replace spring-cloud-starter-netflix-eureka-client with spring-cloud-starter-alibaba-nacos-discovery and configure the version of Nacos Server.

    Before the replacement:

    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>                            

    After the replacement:

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>                           
    Note
    • Spring Cloud Greenwich is used in this example. The version of spring-cloud-starter-alibaba-nacos-discovery is 2.1.1.RELEASE.

    • If you use Spring Cloud Finchley, the version of spring-cloud-starter-alibaba-nacos-discovery is 2.0.1.RELEASE.

    • If you use Spring Cloud Edgware, the version of spring-cloud-starter-alibaba-nacos-discovery is 1.5.1.RELEASE. The service of this version has ended. We do not recommend this version. For more information, see Spring Cloud Edgware Release Notes.

  2. Specify an IP address for Nacos Server.

    Open the application.properties file in src\main\resources and specify an IP address for Nacos Server.

    Before the modification:

    spring.application.name=service-consumer
    server.port=18082
    eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/                            

    After the modification:

    spring.application.name=service-consumer
    server.port=18082
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848                            

    127.0.0.1 is the IP address of Nacos Server. If Nacos Server is deployed on another physical server, change the IP address accordingly. If you have other requirements, add the required configurations in the application.properties file by referring to Configuration items.

  3. Query the application service.

    1. Execute the main function of ConsumerApplication in the nacos-service-consumer project to start the consumer.

    2. Log on to the Nacos Server console that is installed on your computer by visiting 127.0.0.1:8848/nacos. In the left-side navigation pane, choose Service management > Service list.

      Note

      The default username and password for logging on to the Nacos Server console are nacos.

      If service-consumer is displayed in Service list, and you can query the details of service-consumer, then service-consumer is successfully registered.

Step 4: View the results of the consumer consuming services of the provider

View the results of the consumer consuming services provided by the provider on your computer. Start the service, and run IP+port/echo-rest/<Custom variable> or IP+port/echo-feign/<Custom variable> to view the results.

  • For Linux, UNIX, or macOS, run curl http://127.0.0.1:18082/echo-rest/<Custom variable> or curl http://127.0.0.1:18082/echo-feign/<Custom variable>.

  • For Windows, enter http://127.0.0.1:18082/echo-rest/<Custom variable> or http://127.0.0.1:18082/echo-feign/<Custom variable> in the browser.

The following is an example of successful service consumption on a Windows operating system.

Verify the result

Step 5: Deploy applications to SAE

  1. In the pom.xml file of the application, add the following configuration, and then run the mvn clean package command to compile the local program into an executable JAR package.

    <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <executions>
                     <execution>
                         <goals>
                             <goal>repackage</goal>
                         </goals>
                     </execution>
                 </executions>
             </plugin>
         </plugins>
     </build>                        
  2. Deploy the compiled provider and consumer application packages to SAE. For more information, see Deploy microservice applications to SAE.

    Important
    • SAE does not allow you to create an empty application. You must create an application in the console, then you can push data into the application.

    • If you use a JAR package to deploy the applications, set Application Runtime Environment to Standard Java Application Runtime Environment.

    • If you use a WAR package to deploy the applications, set Application Runtime Environment to apache-tomcat-XXX.

    When you deploy applications to SAE, the service registry of SAE automatically sets the Nacos Server IP address, service port, namespace, AccessKey ID, and AccessKey secret at a high priority. You can retain or delete the default configurations based on your needs.

Step 6: Verify the result

  1. Bind an Internet-facing SLB instance to the consumer and enter the public endpoint you specified for the SLB instance in the browser to go to the homepage of the consumer. For more information, see Bind an SLB instance to an application.

  2. On the homepage of the consumer, create a request to call services from the provider. Log on to the SAE console. In the left-side navigation pane, click Applications.

  3. In the top navigation bar, select a region.

  4. On the Applications page, select a consumer and go to the Basic Information page.

  5. In the left-side navigation pane, choose Application Monitoring > Application Overview to view the overall service call data.

    If the data about the request to call the services from the provider is displayed on the Overview page, the services are successfully called.

Configuration items

Configuration item

Key

Default value

Description

Server IP address

spring.cloud.nacos.discovery.server-addr

None

The IP address and the port on which Nacos Server listens.

Service name

spring.cloud.nacos.discovery.service

${spring.application.name}

The name of the service.

Network interface controller (NIC) name

spring.cloud.nacos.discovery.network-interface

None

If no IP addresses are specified, the IP address of the NIC is registered. If this item is not configured, the IP address of the first NIC is used by default.

Registered IP address

spring.cloud.nacos.discovery.ip

None

The registered IP address has higher priority than the ENI name during configuration.

Registered port

spring.cloud.nacos.discovery.port

-1

You do not need to configure the registered port. The system automatically detects the port.

Namespace

spring.cloud.nacos.discovery.namespace

None

The isolation of registration in different environments. For example, the isolation of resources, including configurations and services, in the development, test, and production environments.

Metadata

spring.cloud.nacos.discovery.metadata

None

This metadata must be configured in the JSON Map format. You can specify service-related metadata based on your business requirements.

Cluster

spring.cloud.nacos.discovery.cluster-name

DEFAULT

The name of the Nacos cluster.

Endpoint

spring.cloud.nacos.discovery.endpoint

None

The domain name of a service in the region. You can dynamically retrieve the server IP address based on this domain name. You do not need to specify this item when deploying an application to SAE.

Whether to enable Ribbon integration

ribbon.nacos.enabled

true

Disable Ribbon integration only when you need.

For more information about Spring Cloud Alibaba Nacos Discovery, see Spring Cloud Alibaba Nacos Discovery.

References

The following table describes the operations that you can perform on an application after you deploy the application on SAE.

Operation

References

Lifecycle management operations, such as updating, starting, stopping, and deleting an application, and scaling in or scaling out the instances for an application

Manage the lifecycle of an application

Performance optimization operations, such as configuring auto scaling policies for an application, binding Server Load Balancer (SLB) instances to an application, and starting or stopping applications in batches

Application status-based operations, such as managing logs, configuring monitoring settings, viewing application events, and viewing change records