Taking a Spring Cloud application that contains a service provider and a service consumer as an example, this topic describes how to develop, including add dependencies and required configuration items, and test an application locally and then deploy it to EDAS to implement service registration and discovery, as well as consumer calls to providers.
Even if you know nothing about Spring Cloud and only the basics about Spring and Maven, after going through this article, you will learn how to implement service registration and discovery for Spring Cloud applications, as well as consumer calls to providers through Spring Cloud Alibaba Nacos Discovery.
If you are familiar with service registration components in Spring Cloud such as Eureka, Consul, and ZooKeeper, but have not used the service registration component Nacos Discovery of Spring Cloud Alibaba, you only need to replace the dependencies and configuration items of these service registration components with Spring Cloud Alibaba Nacos Discovery, without modifying any code.
Spring Cloud Alibaba Nacos Discovery also implements the standard interfaces and specifications of Spring Cloud Registry, which are basically the same as the methods that you use to access service registration and discovery in Spring Cloud.
If you are familiar with how to use the open source Spring Cloud Alibaba Nacos Discovery component to register and discover services for Spring Cloud applications, you can directly deploy the applications to EDAS to use the commercial version of service registration and discovery capabilities provided by EDAS. For more information about how to deploy applications to EDAS, see Deploy an application to EDAS.
Spring Cloud Alibaba Nacos Discovery is the commercial version of the open source Nacos Server provided by the EDAS service registry. It allows you to directly use the business edition of service registry provided by EDAS.
The commercial EDAS service registry has the following advantages over Nacos, Eureka, and Consul:
- Components sharing, which saves you the costs of deploying, operating, and maintaining Nacos, Eureka, or Consul.
- Your services are protected from being discovered by unauthorized applications by link encryption for the calls to use service registration and discovery.
- The EDAS service registry is tightly integrated with other EDAS components to provide you with a complete set of microservice solutions, including environment isolation, smooth connection and disconnection, and canary deployment.
Local development
This section describes key information for developing Spring Cloud applications locally. For more information about Spring Cloud, download service-provider and service-consumer.
Preparations
Before you start developing, be sure to complete the following tasks:
- Download Maven and set environment variables.
- Download the latest version of Nacos Server.
- To start Nacos Server, follow these steps.
- Decompress the downloaded Nacos Server package.
- Go to the
nacos/bin
directory and start Nacos Server.- For Linux/UNIX/Mac: Run the
sh startup.sh -m standalone
command. - For Windows: Double-click the
startup.cmd
file to run it.
- For Linux/UNIX/Mac: Run the
Create a service provider
Create a local service provider application project, add dependencies, enable service registration and discovery, and specify Nacos Server as the registry.
Procedure
Create a Maven project named
Nacos-service-provider
.Add dependencies to the
pom.xml
file.Take Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1 as an example. The dependencies are as follows:
<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.0.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>
This example uses Spring Cloud Greenwich, and the corresponding Spring Cloud Alibaba version is 2.1.0.RELEASE.
- If you are using Spring Cloud Finchley, the corresponding Spring Cloud Alibaba version is 2.0.0.RELEASE.
- If you are using Spring Cloud Edgware, the corresponding Spring Cloud Alibaba version is 1.5.0.RELEASE.
Note: The Spring Cloud Edgware release will reach end-of-life in August 2019. We do not recommend that you use this release to develop applications.
Create a package named
com.aliware.edas
insrc\main\java
.In the
com.aliware.edas
package, create a startup class 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
, and specify {/echo/{String} as the URL mapping and GET as the HTTP method. Retrieve the method parameter from the URL, 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 namedapplication.properties
and add the following configuration toapplication.properties
to specify the Nacos Server address.127.0.0.1
is the IP address of Nacos Server. If your Nacos Server is deployed on another machine, change the IP address to the corresponding one. If you have other requirements, refer to Configuration item reference for adding configuration items to theapplication.properties
file.spring.application.name=service-provider
server.port=18081
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
Verify the result
Run the
main
function ofProviderApplication
innacos-service-provider
to start the application.Log on to the Nacos Server console at
http://127.0.0.1:8848/nacos
while noting that both the default username and password of the local Nacos console are “nacos”. In the left-side navigation pane, choose Service Management > Services. You can seeservice-provider
in the list of services and query the details of the service in Details.
Create a service consumer
This section demonstrates the service registration function and explains how Nacos service discovery works with the RestTemplate and FeignClient clients.
Procedure
Create a Maven project 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.0.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 namedcom.aliware.edas
.In the
com.aliware.edas
package, set RestTemplate and FeignClient.Create an interface class named
EchoService
in ‘com.aliware.edas’, add the@FeignClient
annotation, and configure the corresponding 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);
}
Create a startup class named
ConsumerApplication
in thecom.aliware.edas
package and add related configuration items.- 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);
}
}
- Use the
Create a class named
TestController
in thecom.aliware.edas
package 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 namedapplication.properties
and add the following configuration to specify the address of Nacos Server.127.0.0.1:8848
is the IP address of Nacos Server. If your Nacos Server is deployed on another machine, change the IP address to the corresponding one. If you have other requirements, refer to Configuration item reference for adding configuration items to theapplication.properties
file.spring.application.name=service-consumer
server.port=18082
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
Verify the result
Run the
main
function ofConsumerApplication
innacos-service-consumer
to start the application.Log on to the Nacos Server console that starts locally at
http://127.0.0.1:8848/nacos
. Note that both the default username and password of the local Nacos console are “nacos”. In the left-side navigation pane, choose Service Management > Services. You can seeservice-consumer
in the list and query the details of the service in Details.
Test the result locally
Test the result of service call to the provider made by the consumer in a local environment.
For Linux/UNIX/Mac: Run
curl http://127.0.0.1:18082/echo-rest/rest-rest
andcurl http://127.0.0.1:18082/echo-feign/feign-rest
.For Windows: In the address bar of your browser, enter
http://127.0.0.1:18082/echo-rest/rest-rest
andhttp://127.0.0.1:18082/echo-feign/feign-rest
.
Deploy an application to EDAS
After developing and testing your application locally, you can package and deploy it to EDAS. You can choose to deploy your Spring Cloud application to an ECS cluster, Container Service Kubernetes cluster, or EDAS Serverless cluster based on your needs. For detailed steps to deploy an application, see Application deployment overview.
Note: We recommend that you use the console for your initial deployment. If you choose to use JAR packages for this purpose, make sure to select Standard Java application runtime environment for Application Runtime Environment when creating an application.
The EDAS service registry provides a GA release of Nacos Server. When you deploy an application to EDAS, EDAS sets the IP address and service port of Nacos Server, as well as other information such as the namespace, access-key, secret-key, and context-path with higher priority. You do not need to make any additional configurations, and you can choose to retain or delete your original configuration.
Verify the result
After the deployment is completed, in the left-side navigation pane of the EDAS console, choose Microservice Management > Service Query. On the Service Query page, select Region and Namespace , and then search for your deployed application by entering service-provider
and service-consumer
.
Configuration item reference
Configuration item | Key | Default value | Description |
---|---|---|---|
Server address | spring.cloud.nacos.discovery.server-addr | None | The IP address and port of the server that Nacos Server listens to. |
Service name | spring.cloud.nacos.discovery.service | ${spring.application.name} | The name of the current service. |
Network interface name | sspring.cloud.nacos.discovery.network-interface | None | The registered IP address is the IP address of the corresponding network interface when an IP is not configured. If this item is not configured, the IP address of the first network interface is used by default. |
Registered IP address | spring.cloud.nacos.discovery.ip | None | Highest priority |
Registered port | spring.cloud.nacos.discovery.port | -1 | No configuration is required by default. The system automatically detects the port. |
Namespace | spring.cloud.nacos.discovery.namespace | None | One of the common use cases is the isolation of registration in different environments, for example, the isolation of the resources (such as configurations and services) in development, test, and production environments. |
Metadata | spring.cloud.nacos.discovery.metadata | None | This item is configured in the Map format. You can customize metadata information related to your services as needed. |
Cluster | spring.cloud.nacos.discovery.cluster-name | DEFAULT | Set it to the name of a Nacos cluster. |
Endpoint | spring.cloud.nacos.discovery.endpoint | UTF-8 | The domain name of the entry of a service in the region. You can dynamically retrieve the server address through this domain name. This configuration item is not required when an application is deployed to EDAS. |
Enable Ribbon integration | ribbon.nacos.enabled | true | You do not need to modify this item in most cases. |
For more information about Spring Cloud Alibaba Nacos Discovery, see Spring Cloud Alibaba Nacos Discovery documentation.
FAQ
Use other versions
This example uses Spring Cloud Greenwich, and the corresponding Spring Cloud Alibaba version is 2.1.0.RELEASE. Spring Cloud Finchley must be used with Spring Cloud Alibaba 2.0.0.RELEASE, and Spring Cloud Edgware with Spring Cloud Alibaba 1.5.0.RELEASE.
Note: The Spring Cloud Edgware release will reach end-of-life in August 2019. We do not recommend that you use this release to develop applications.
Migrate from ANS
The EDAS registry configures the data structures of ANS and Nacos in a way that the two are compatible on servers. In the same namespace and when a group is not set up in Nacos, the Nacos client and the ANS client can discover each other’s registration services.