When you deploy a Spring Cloud application or a Dubbo application that is developed in your on-premises environment to Serverless App Engine (SAE), you can use the built-in service registry of SAE or a Microservices Engine (MSE) Nacos registry to register and discover services. This topic describes how to build an MSE Nacos registry and deploy applications to SAE for hosting.
Prerequisites
A virtual private cloud (VPC) is created and available. For more information, see Create and manage a VPC.
The access port of your Nacos registry, such as port 8848, is added to your security group before you run your application. For more information, see Add a security group rule.
Background information
A startup company needs to deploy a microservices application on SAE for management but expects to use an MSE Nacos registry instead of the built-in service registry of SAE.
For information about how to deploy an application in cluster mode, see Cluster deployment instructions.
If you have a large number of microservices applications, you can use one of the following types of service registries that are listed in descending order by recommendation level:
MSE Nacos registry
Self-managed service registry
Built-in service registry of SAE
If you want to implement features such as application service discovery and configuration management by using a self-managed Nacos engine, you must purchase the required resources to create and maintain the Nacos engine. This process is time-consuming and labor-intensive. If you use MSE to create a Nacos engine, you need to only focus on the location, version, network type, and specifications of the Nacos engine. You do not need to focus on the creation and maintenance of the Nacos engine. MSE allows you to focus more on your business.
To ensure that you can connect to the MSE Nacos registry, we recommend that you use a Nacos client of V2.2.2 or later. If you use a Nacos client of a version earlier than V2.2.2, applications are always connected to the built-in service registry of SAE, but not the MSE Nacos registry.
Step 1: Purchase and build a Nacos engine
For more information about how to create a Nacos engine for applications in MSE, see Create a Nacos engine.
We recommend that you purchase a Nacos engine that resides in the same VPC as your SAE applications. You can also use other methods to ensure the network connectivity.
Step 2: Register and discover services
After the Nacos engine is started, you must specify a service registry on your application to use the service registration and discovery feature. After you run your application, the system automatically registers and discovers services based on the specified service registry.
Before you run your application, make sure that the access port of your Nacos registry, such as port 8848, is added to your security group.
Specify a registry on each application and run your service application and consumer application.
Open the application.properties file in src\main\resources, and specify the IP address of the Nacos server.
If you use an MSE Nacos registry, perform the following operations: Log on to the MSE console. On the Instances page, click the required instance. On the Basic Information page of the instance, obtain the internal endpoint of the registry.
Provider
Original code:
spring.application.name=service-provider server.port=18081 eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/
Modified code:
spring.application.name=service-provider server.port=18081 spring.cloud.nacos.discovery.server-addr=192.168.XX.XX:8848 # Replace the value with the endpoint of the Nacos registry that you purchased. Example: mse:**********:8848.
Consumer
Original code:
spring.application.name=service-consumer server.port=18082 eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/
Modified code:
spring.application.name=service-consumer server.port=18082 spring.cloud.nacos.discovery.server-addr=192.168.XX.XX:8848 # Replace the value with the endpoint of the Nacos registry that you purchased. Example: mse:**********:8848.
Step 3: Deploy applications to SAE
Compile your on-premises provider application and consumer application into WAR packages, JAR packages, or images, deploy the applications to SAE, and then use one of the following methods to connect the applications to the Nacos registry. For more information, see the following topics:
Method 1: Configure service registration and discovery
SAE provides the automatic addressing capabilities of Nacos Server for Java microservices applications. You can use the service registration and discovery feature of SAE to configure a service registry. For more information, see Use the SAE built-in Nacos registry.
Method 2: Configure startup parameters
If you configure startup parameters to connect to the Nacos registry, take note of the following items:
Make sure that your SAE applications and the MSE Nacos registry are interconnected.
- If you use a self-managed service registry, we recommend that you use an image or a JAR package to deploy an application, and configure the following startup parameters:
-Dnacos.use.endpoint.parsing.rule=false
and-Dnacos.use.cloud.namespace.parsing=false
.Important To use a non-SAE built-in registry, you must add the required startup parameters before-jar
.- If you use an image to deploy an application, add
-Dnacos.use.endpoint.parsing.rule=false
and-Dnacos.use.cloud.namespace.parsing=false
to the startup command of the image file. For information about how to create a Docker image, see Create an image to deploy a Java application.Sample code:
RUN echo 'eval exec java -Dnacos.use.endpoint.parsing.rule=false -Dnacos.use.cloud.namespace.parsing=false -jar $CATALINA_OPTS /home/admin/app/hello-edas-0.0.1-SNAPSHOT.jar'> /home/admin/start.sh && chmod +x /home/admin/start.sh
- If you use a JAR package to deploy an application, go to the Startup Command Settings section in the SAE console. Then, enter
-Dnacos.use.endpoint.parsing.rule=false -Dnacos.use.cloud.namespace.parsing=false
in the options Settings field. The following figure shows the startup command that is configured to deploy a Java application in the Open JDK 8 runtime environment. For more information, see Configure a startup command.
- If you use an image to deploy an application, add
- To prevent the command from being invalid, make sure that you do not use the
-D
and-XX
parameters at the same time. Sample code:- Original code:
java -Dalicloud.deployment.mode=EDAS_MANAGED -XX:+UseContainerSupport -XX:InitialRAMPercentage=70.0 -XX:MaxRAMPercentage=70.0 -XX:+UnlockExperimentalVMOptions -XX:+UseWisp2 -Dio.netty.transport.noNative=true -XX:+UseG1GC -Dspring.profiles.active=yace -Dnacos.use.endpoint.parsing.rule=false -Dnacos.use.cloud.namespace.parsing=false -jar /home/admin/app/xx-server.jar
- Modified code:
java -XX:+UseContainerSupport -XX:InitialRAMPercentage=70.0 -XX:MaxRAMPercentage=70.0 -XX:+UnlockExperimentalVMOptions -XX:+UseWisp2 -Dio.netty.transport.noNative=true -XX:+UseG1GC -Dspring.profiles.active=yace -Dnacos.use.endpoint.parsing.rule=false -Dnacos.use.cloud.namespace.parsing=false -jar /home/admin/app/xx-server.jar
- Original code:
Verify the result
In this example, the consumer application is used to verify the result. On the Instance deployment Information tab of the Basic Information page, click Webshell in the Actions column in the instance list. In the Webshell window, run the following commands to check whether the application is deployed. For more information, see Use the webshell feature to check the health status of applications.
Verify whether the service is registered.
curl -X GET 'http://192.168.XX.XX:8848/nacos/v1/ns/instance/list?serviceName=service-provider'
service-provider
: the name of the service.192.168.XX.XX:8848
: the IP address and port number of the server on which Nacos is installed. Replace192.168.XX.XX
with the internal endpoint of the MSE Nacos registry. For information about how to obtain the internal endpoint, see Step 2: Register and discover services.
If the following output is returned, the service is registered.
Verify whether the access URL of the microservices application is normal.
Run one of the following commands based on your business requirements.
127.0.0.1:18082
indicates the IP address and access port of the server on which the provider application and the consumer application run.curl http://127.0.0.1:18082/echo-rest/{Custom variable}
curl http://127.0.0.1:18082/echo-feign/{Custom variable}
The following output indicates that the consumer application called the provider application and the access URL to the microservices application is normal.
References
If an application fails to be deployed, see the following topics to identify the failure cause: