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.
- 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.
- Creating a Registry/Configuration Center
You can select engine specifications, AZs, and networks when creating an engine.
- 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
- 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.
- Log in to the Huawei Cloud official website and click Sign Up.
- 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.
- 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.
- Top up your account.
Ensure that your account has sufficient balance.
- For details about registry/configuration center prices, see CSE Pricing Details.
- For details about top-up, see Top-Up and Repayment.
- 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.
- 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.
- 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.
- Download the demo source code from GitHub to the local host and decompress it.
Creating a Registry/Configuration Center
- Go to the Buy Registry/Configuration Center Instance page.
- In the left navigation pane, choose Registry/Configuration Center.
- Click Buy Registry/Configuration Center Instance.
- 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.
- 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
- Log in to CSE.
- Obtain the registry and discovery address of a Nacos engine.
- In the left navigation pane, choose Registry/Configuration Center and click the Nacos engine instance.
- In the Connection Information area on the Basic Information page, obtain the service center address.
- Change the configuration center address, service center address, and microservice name in the demo.
- 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.
- 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.
- 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.
- Configure the Nacos configuration center in bootstrap.properties.
- Pack the demo source code into a JAR package.
- 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.
- 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
- Deploy Spring Cloud applications.
Deploy provider and consumer on the ECS node in the VPC where the Nacos engine is located.
- 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.
- Install JRE to provide a running environment for services.
- Upload the JAR package generated in 4 to the ECS node.
- Run the java -jar {JAR package} command to run the generated JAR package.
- Confirm the deployment results.
- Optional: On the CSE console, choose Registry/Configuration Center and click the Nacos engine created in Creating a Registry/Configuration Center.
- 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.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot