Enterprise Distributed Application Service (EDAS) provides the general availability (GA) version of the Nacos registry. Applications developed in Nacos can use this shared registry of EDAS without code modification. You need only to deploy the applications to EDAS. This topic describes how to develop a pair of Spring Cloud sample microservice applications in an on-premises environment based on Nacos. The pair includes a service provider and a service consumer.
Background information
Microservice applications use registries to implement service registration and discovery. When you develop an application, you can select a registry based on your actual needs.
You can use Nacos that is described in this topic as a registry to implement service registration and discovery for your application. You can also use other types of registries, such as Eureka, ZooKeeper, and Consul that are user-created or managed in Microservice Engine (MSE). After you deploy your application to EDAS, you can use the application management, microservice governance, and cloud native Platform as a Service (PaaS) features of EDAS regardless of the registry type.
You can implement service registration and discovery for your application based on the instructions in this topic. You can also download the application demos: service-provider and service-consumer.
Preparations
Before you develop an application, make sure that you have completed the following operations:
- Download Maven and set the environment variables.
- Download the latest version of Nacos Server.
- Perform the following steps to start Nacos Server:
- Decompress the downloaded Nacos Server package.
- Go to the
nacos/bin
directory to start Nacos Server.- On Linux, UNIX, or macOS, run the
sudo sh startup.sh -m standalone
command. - On Windows, double-click the
startup.cmd
file to run the file.
- On Linux, UNIX, or macOS, run the
Create a service provider
Create a provider application project in an on-premises environment, add dependencies, enable the service registration and discovery feature, and then specify Nacos Server as the registry.
- Create a Maven project that is named
nacos-service-provider
. - Add dependencies to the
pom.xml
file.The following code provides an example on how to add dependencies. Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1 are used in this example.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Spring Cloud Greenwich is used in this example and the Spring Cloud Alibaba version is 2.1.1.RELEASE.
- If you use Spring Cloud Finchley, the Spring Cloud Alibaba version is 2.0.1.RELEASE.
- If you use Spring Cloud Edgware, the Spring Cloud Alibaba version is 1.5.1.RELEASE. Note The Spring Cloud Edgware release has reached the end of its service life. Therefore, we recommend that you do not use this release to develop applications.
- In
src\main\java
, create a package that is namedcom.aliware.edas
. - In the
com.aliware.edas
package, create a startup class that is namedProviderApplication
for the service provider, and add the following code.The
@EnableDiscoveryClient
annotation indicates that the service registration and discovery feature must be enabled for the application.package com.aliware.edas; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
- In the
com.aliware.edas
package, createEchoController
.In
EchoController
, specify/echo/{string}
as the URL mapping and GET as the HTTP method. Retrieve the method parameter from the URL path, and echo the received parameter.package com.aliware.edas; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class EchoController { @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) public String echo(@PathVariable String string) { return string; } }
- In
src\main\resources
, create a file that is namedapplication.properties
and add the following configuration toapplication.properties
to specify the IP address of Nacos Server.spring.application.name=service-provider server.port=18081 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
In the preceding configuration,
127.0.0.1
is the IP address of Nacos Server. If your Nacos Server is deployed on another machine, you must change the value to the IP address of the machine. If you have other requirements, add configuration items to theapplication.properties
file. For more information, see Configuration items for reference. - Verify the result.
Create a service consumer
This section describes the service registration feature and explains how Nacos Server works with RestTemplate and FeignClient.
- Create a Maven project that is named
nacos-service-consumer
. - Add dependencies to the
pom.xml
file.<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
- In
src\main\java
, create a package that is namedcom.aliware.edas
. - In the
com.aliware.edas
package, configure RestTemplate and FeignClient. - In the
com.aliware.edas
package, create a class that is namedTestController
to demonstrate and verify the service discovery feature.package com.aliware.edas; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class TestController { @Autowired private RestTemplate restTemplate; @Autowired private EchoService echoService; @RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET) public String rest(@PathVariable String str) { return restTemplate.getForObject("http://service-provider/echo/" + str, String.class); } @RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET) public String feign(@PathVariable String str) { return echoService.echo(str); } }
- In
src\main\resources
, create a file that is namedapplication.properties
and add the following configuration toapplication.properties
to specify the IP address of Nacos Server.spring.application.name=service-consumer server.port=18082 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
In the preceding configuration,
127.0.0.1
is the IP address of Nacos Server. If your Nacos Server is deployed on another machine, you must change the value to the IP address of the machine. If you have other requirements, add configuration items to theapplication.properties
file. For more information, see Configuration items for reference. - Verify the result.
Test the result in an on-premises environment
Initiate a call from the consumer to the provider in an on-premises environment and test the result.
- On Linux, UNIX, or macOS, run the following commands:
curl http://127.0.0.1:18082/echo-rest/rest-rest curl http://127.0.0.1:18082/echo-feign/feign-rest
- On Windows, enter http://127.0.0.1:18082/echo-rest/rest-rest and http://127.0.0.1:18082/echo-feign/feign-rest in the browser.
What to do next
After applications are developed, they can be deployed to EDAS. For more information, see Overview and Overview.
Configuration items for reference
Configuration item | Key | Default value | Description |
---|---|---|---|
Server 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 current service. |
Network interface controller (NIC) name | spring.cloud.nacos.discovery.network-interface | None | If no IP addresses are specified, the registered IP address is the IP address of the NIC. If this configuration item is not specified, the IP address of the first NIC is used by default. |
Registered IP address | spring.cloud.nacos.discovery.ip | None | This configuration item has the highest priority. |
Registered port | spring.cloud.nacos.discovery.port | -1 | No configurations are required by default. The system automatically detects the port. |
Namespace | spring.cloud.nacos.discovery.namespace | None | Namespaces are widely used to isolate resources in different environments. For example, you can use namespaces to isolate the resources, such as configurations and services, in the development, test, and production environments. |
Metadata | spring.cloud.nacos.discovery.metadata | None | This item must be configured in the Map format. You can specify metadata information that is related to the service based on your needs. |
Cluster | spring.cloud.nacos.discovery.cluster-name | DEFAULT | The name of the Nacos cluster. |
Endpoint | spring.cloud.nacos.discovery.endpoint | UTF-8 | The domain name of the service in the region. You can dynamically retrieve the server address by using this domain name. You do not need to specify this configuration item when you deploy the service to EDAS. |
Enable Ribbon integration | ribbon.nacos.enabled | true | Change the value only when you need. |
For more information about Spring Cloud Alibaba Nacos Discovery, see the open source version of Nacos Discovery.