Enterprise Distributed Application Service (EDAS) provides the general availability
(GA) version of the Nacos registry. For the applications developed in Nacos to use
this shared registry of EDAS, you do not need to modify the application code. 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,
as shown in the following figure.
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 hosted in Microservice Engine
(MSE). After you deploy your application to EDAS, you can use the application hosting,
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.
Before you begin
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.
- For Linux, UNIX, and macOS, run the
sh startup.sh -m standalone
command.
- For Windows, double-click the
startup.cmd
file to run the file.
Create a service provider
Create a provider application project in an on-premises environment, add dependencies,
enable the service registration and discovery feature, and 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 named com.aliware.edas
.
- In the
com.aliware.edas
package, create a startup class that is named ProviderApplication
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, create EchoController
.
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 the
src\main\resources
path, create a file that is named application.properties
and add the following configuration to application.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 the application.properties
file. For more information, see Configuration items for reference.
- Verify the result.
- Use the
main
function of ProviderApplication
in nacos-service-provider
to start the application.
- Log on to the on-premises Nacos Server console at
http://127.0.0.1:8848/nacos
. The default username and password of the on-premises Nacos Server console are both
nacos.
- In the left-side navigation pane, choose Service Management > Services.
You can view that service-provider
appears in the service list. You can also query detailed information about the service
in Details.
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 named com.aliware.edas
.
- In the
com.aliware.edas
package, configure RestTemplate and FeignClient.
- In the
com.aliware.edas
package, create an interface class that is named EchoService
, add the @FeignClient
annotation, and then configure the HTTP URL and HTTP method. package com.aliware.edas;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name = "service-provider")
public interface EchoService {
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
String echo(@PathVariable("str") String str);
}
- In the
com.aliware.edas
package, create a startup class that is named ConsumerApplication
and add related configurations.
- Use the
@EnableDiscoveryClient
annotation to enable service registration and discovery.
- Use the
@EnableFeignClients
annotation to activate FeignClient.
- Add the
@LoadBalanced
annotation to integrate RestTemplate with service discovery.
package com.aliware.edas;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
- In the
com.aliware.edas
package, create a class that is named TestController
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 the
src\main\resources
path, create a file that is named application.properties
and add the following configuration to application.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 the application.properties
file. For more information, see Configuration items for reference.
- Verify the result.
- Run the
main
function of ConsumerApplication
in nacos-service-consumer
to start the application.
- Log on to the on-premises Nacos Server console at
http://127.0.0.1:8848/nacos
. The default username and password of the on-premises Nacos Server console are both
nacos.
- In the left-side navigation pane, choose Service Management > Services. You can view that
service-consumer
appears in the service list. You can also query the details about the service in
Details.
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.
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.