All Products
Search
Document Center

Deploy Spring Cloud Applications to EDAS

Last Updated: Sep 29, 2019

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.
    1. Decompress the downloaded Nacos Server package.
    2. 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.

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

  1. Create a Maven project named Nacos-service-provider.

  2. 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:

    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>2.1.4.RELEASE</version>
    5. <relativePath/>
    6. </parent>
    7. <dependencies>
    8. <dependency>
    9. <groupId>com.alibaba.cloud</groupId>
    10. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    11. <version>2.1.0.RELEASE</version>
    12. </dependency>
    13. <dependency>
    14. <groupId>org.springframework.boot</groupId>
    15. <artifactId>spring-boot-starter-web</artifactId>
    16. </dependency>
    17. </dependencies>
    18. <dependencyManagement>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.cloud</groupId>
    22. <artifactId>spring-cloud-dependencies</artifactId>
    23. <version>Greenwich.SR1</version>
    24. <type>pom</type>
    25. <scope>import</scope>
    26. </dependency>
    27. </dependencies>
    28. </dependencyManagement>
    29. <build>
    30. <plugins>
    31. <plugin>
    32. <groupId>org.springframework.boot</groupId>
    33. <artifactId>spring-boot-maven-plugin</artifactId>
    34. </plugin>
    35. </plugins>
    36. </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.

  3. Create a package named com.aliware.edas in src\main\java.

  4. In the com.aliware.edas package, create a startup class 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.

    1. package com.aliware.edas;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    5. @SpringBootApplication
    6. @EnableDiscoveryClient
    7. public class ProviderApplication {
    8. public static void main(String[] args) {
    9. SpringApplication.run(ProviderApplication.class, args);
    10. }
    11. }
  5. In the com.aliware.edas package, create EchoController, 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.

    1. package com.aliware.edas;
    2. import org.springframework.web.bind.annotation.PathVariable;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. import org.springframework.web.bind.annotation.RestController;
    6. @RestController
    7. public class EchoController {
    8. @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
    9. public String echo(@PathVariable String string) {
    10. return string;
    11. }
    12. }
  6. In src\main\resources, create a file named application.properties and add the following configuration to application.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 the application.properties file.

    1. spring.application.name=service-provider
    2. server.port=18081
    3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

Verify the result

  1. Run the main function of ProviderApplication in nacos-service-provider to start the application.

  2. 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 see service-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

  1. Create a Maven project named nacos-service-consumer.

  2. Add dependencies to the pom.xml file.

    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>2.1.4.RELEASE</version>
    5. <relativePath/>
    6. </parent>
    7. <dependencies>
    8. <dependency>
    9. <groupId>com.alibaba.cloud</groupId>
    10. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    11. <version>2.1.0.RELEASE</version>
    12. </dependency>
    13. <dependency>
    14. <groupId>org.springframework.boot</groupId>
    15. <artifactId>spring-boot-starter-web</artifactId>
    16. </dependency>
    17. <dependency>
    18. <groupId>org.springframework.cloud</groupId>
    19. <artifactId>spring-cloud-starter-openfeign</artifactId>
    20. </dependency>
    21. </dependencies>
    22. <dependencyManagement>
    23. <dependencies>
    24. <dependency>
    25. <groupId>org.springframework.cloud</groupId>
    26. <artifactId>spring-cloud-dependencies</artifactId>
    27. <version>Greenwich.SR1</version>
    28. <type>pom</type>
    29. <scope>import</scope>
    30. </dependency>
    31. </dependencies>
    32. </dependencyManagement>
    33. <build>
    34. <plugins>
    35. <plugin>
    36. <groupId>org.springframework.boot</groupId>
    37. <artifactId>spring-boot-maven-plugin</artifactId>
    38. </plugin>
    39. </plugins>
    40. </build>
  3. In src\main\java, create a package named com.aliware.edas.

  4. In the com.aliware.edas package, set RestTemplate and FeignClient.

    1. Create an interface class named EchoService in ‘com.aliware.edas’, add the @FeignClient annotation, and configure the corresponding HTTP URL and HTTP method.

      1. package com.aliware.edas;
      2. import org.springframework.cloud.openfeign.FeignClient;
      3. import org.springframework.web.bind.annotation.PathVariable;
      4. import org.springframework.web.bind.annotation.RequestMapping;
      5. import org.springframework.web.bind.annotation.RequestMethod;
      6. @FeignClient(name = "service-provider")
      7. public interface EchoService {
      8. @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
      9. String echo(@PathVariable("str") String str);
      10. }
    2. Create a startup class named ConsumerApplication in the com.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.
      1. package com.aliware.edas;
      2. import org.springframework.boot.SpringApplication;
      3. import org.springframework.boot.autoconfigure.SpringBootApplication;
      4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
      5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
      6. import org.springframework.cloud.openfeign.EnableFeignClients;
      7. import org.springframework.context.annotation.Bean;
      8. import org.springframework.web.client.RestTemplate;
      9. @SpringBootApplication
      10. @EnableDiscoveryClient
      11. @EnableFeignClients
      12. public class ConsumerApplication {
      13. @LoadBalanced
      14. @Bean
      15. public RestTemplate restTemplate() {
      16. return new RestTemplate();
      17. }
      18. public static void main(String[] args) {
      19. SpringApplication.run(ConsumerApplication.class, args);
      20. }
      21. }
  5. Create a class named TestController in the com.aliware.edas package to demonstrate and verify the service discovery feature.

    1. package com.aliware.edas;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RequestMethod;
    6. import org.springframework.web.bind.annotation.RestController;
    7. import org.springframework.web.client.RestTemplate;
    8. @RestController
    9. public class TestController {
    10. @Autowired
    11. private RestTemplate restTemplate;
    12. @Autowired
    13. private EchoService echoService;
    14. @RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
    15. public String rest(@PathVariable String str) {
    16. return restTemplate.getForObject("http://service-provider/echo/" + str,
    17. String.class);
    18. }
    19. @RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
    20. public String feign(@PathVariable String str) {
    21. return echoService.echo(str);
    22. }
    23. }
  6. In src\main\resources, create a file named application.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 the application.properties file.

    1. spring.application.name=service-consumer
    2. server.port=18082
    3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

Verify the result

  1. Run the main function of ConsumerApplication in nacos-service-consumer to start the application.

  2. 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 see service-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 and curl http://127.0.0.1:18082/echo-feign/feign-rest.

     Service call

  • For Windows: In the address bar of your browser, enter http://127.0.0.1:18082/echo-rest/rest-rest and http://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.

SC Runtime Environment

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

  1. 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.

  2. 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.