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
- Log in to CSE.
- Log in to the management console.
- Click
and select a region.
- Click
in the upper left corner and select Cloud Service Engine in the service list. The CSE console is displayed.
- Obtain the service center address of the exclusive 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 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.
- 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 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
- Deploy the Spring Cloud application.
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 Prerequisites.
- 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