All Products
Search
Document Center

Deploy Spring Cloud Applications to EDAS

Last Updated: Mar 14, 2019

This document describes how to use ANS to connect your Spring Cloud applications to EDAS and how to use the EDAS service registry to implement service discovery.

Note: If you are familiar with Spring Cloud, when reading this document, you will find that there is no difference between the use of ANS and the use of Eureka or Consul for service registration and discovery.For migration from Eureka or Consul to ANS, no code needs to be modified.

EDAS currently supports only Spring Cloud Finchley (0.2.1. RELEASE) and Spring Cloud Edgware (0.1.1. RELEASE). The version of Spring Cloud Alibaba matched to Spring Cloud Fincheley is 0.2.1 RELEASE. The version of Spring Cloud Alibaba matched to Spring Cloud Edgware is 0.1.1 RELEASE.

Why is ANS used?

Application Naming Service (ANS) is a service discovery component provided by EDAS, which is a commercial version of open source Nacos.

Spring Cloud Alibaba ANS implements the standard APIs and specifications of Spring Cloud Registry and can completely replace the service discovery function provided by Spring Cloud Eureka or Spring Cloud Consul.

In addition, ANS offers the following advantages over Spring Cloud Eureka and Spring Cloud Consul:

  • ANS is a shared component that saves you the cost of deploying, operating, or maintaining Spring Cloud Eureka or Spring Cloud Consul.
  • ANS provides link encryption for both service registration and discovery calls, protecting your service from being detected by others.
  • ANS is fully integrated with other EDAS components to provide you with a complete set of microservice solutions.

Local development

This document uses a service provider and a service consumer as an example to show you how to develop service providers and consumers locally, and how to deploy them to EDAS, complete service registration in the EDAS service registry, and implement a service consumer’s call of a service provider.

Local development mainly describes key information involved in development. For more information about Spring Cloud, download service-provider and service-consumer.

Preparation

  • Download, start, and configure the lightweight configuration center.

    To facilitate local development, EDAS provides a lightweight configuration center that has the basic features of the EDAS service registry.You can deploy the applications developed based on the lightweight configuration center to EDAS without making any modifications to code or configurations.

    For more information about how to download, start, and configure the lightweight configuration center, see Configure a lightweight configuration center.The latest version is recommended.

  • Download Maven and set environment variables.

Create a service provider

  1. Create a Spring Cloud project named service-provider.

    The following takes the Spring Boot 2.0.6 RELEASE and Spring Cloud Finchley.SR1as an example. Add the following content to the pom.xml file.

    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>2.0.6.RELEASE</version>
    5. <relativePath/>
    6. </parent>
    7. <dependencies>
    8. <dependency>
    9. <groupId>org.springframework.cloud</groupId>
    10. <artifactId>spring-cloud-starter-alicloud-ans</artifactId>
    11. <version>0.2.1.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>Finchley.SR1</version>
    24. <type>pom</type>
    25. <scope>import</scope>
    26. </dependency>
    27. </dependencies>
    28. </dependencyManagement>

    If you need to use Spring Boot 1.x, use Spring Boot 1.5.x, Spring Cloud Edgware, and Spring Cloud Alibaba 0.1.0. RELEASE.

    Note: Spring Boot 1.x will expire in August 2019, so we recommend that you use a later version to develop applications.

  2. Develop the startup class of the service provider, in which the @EnableDiscoveryClient annotation indicates that the service registration and discovery features must be enabled for the application.

    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. public class ServerApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(ServerApplication.class, args);
    6. }
    7. }
  3. Create a simple controller, set URL mapping to {/echo/{String}}, set the HTTP method to GET, retrieve method parameters from the URL path, and set the logic to echo the received parameters.

    1. @RestController
    2. public class EchoController {
    3. @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
    4. public String echo(@PathVariable String string) {
    5. return string;
    6. }
    7. }
  4. Add the following configuration to the application.properties file and specify the EDAS lightweight configuration center as the registry.

    where, 127.0.0.1 is the address of the lightweight configuration center, which must be changed to the corresponding IP address if your lightweight configuration center is deployed on another instance.The lightweight configuration center does not support port modification, so port 8080 must be used.

    1. spring.application.name=service-provider
    2. server.port=18081
    3. spring.cloud.alicloud.ans.server-list=127.0.0.1
    4. spring.cloud.alicloud.ans.server-port=8080

    Note: The two parameters spring.cloud.alicloud.ans.server-list=127.0.0.1 and spring.cloud.alicloud.ans.server-port=8080 are used only in the local development environment where the lightweight configuration center serves the service registry. When an application is deployed to EDAS, the two parameters can be either removed or retained without affecting service registration and usage.

  5. Execute the main function in service-provider to enable the service.

  6. Log on to the console of the lightweight configuration center (http://127.0.0.1:8080). In the left-side navigation pane, click Services to view the list of service providers.The list already contains the service-provider, and you can query the group and address of the service.

Create a service consumer

This topic describes the service discovery feature and how ANS service discovery is combined with the RestTemplate, AsyncRestTemplate, and FeignClient clients.

  1. Create a Spring Cloud project named service-consumer.Introduce the necessary dependencies to the pom.xml file:

    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>2.0.6.RELEASE</version>
    5. <relativePath/>
    6. </parent>
    7. <dependencies>
    8. <dependency>
    9. <groupId>org.springframework.cloud</groupId>
    10. <artifactId>spring-cloud-starter-alicloud-ans</artifactId>
    11. <version>0.2.1.RELEASE</version>
    12. </dependency>
    13. <dependency>
    14. <groupId>org.springframework.cloud</groupId>
    15. <artifactId>spring-cloud-starter-openfeign</artifactId>
    16. </dependency>
    17. <dependency>
    18. <groupId>org.springframework.boot</groupId>
    19. <artifactId>spring-boot-starter-web</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>Finchley.SR1</version>
    28. <type>pom</type>
    29. <scope>import</scope>
    30. </dependency>
    31. </dependencies>
    32. </dependencyManagement>
  2. Configure RestTemplate, AsyncRestTemplate, and FeignClient.

    1. FeignClient is the client that completes HTTP-to-RPC conversion to facilitate calls.Before using this client, complete the following configurations:

      1. Add the @EnableFeignClient annotation.

      2. Configure the corresponding HTTP URL and HTTP method.

        1. @FeignClient(name = "service-provider")
        2. public interface EchoService{
        3. @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        4. String echo(@PathVariable("str") String str);
        5. }
    2. In the startup class:

      1. Enable service registration and discovery by using the @EnableDiscoveryClient annotation.

      2. Activate FeignClient by using the @EnableFeignClients annotation.

      3. Add the annotation of @LoadBalanced to combine RestTemplate, AsyncRestTemplate, and service discovery.

        1. @SpringBootApplication
        2. @EnableDiscoveryClient
        3. @EnableFeignClients
        4. public class ConsumerApplication {
        5. @LoadBalanced
        6. @Bean
        7. public RestTemplate restTemplate() {
        8. return new RestTemplate();
        9. }
        10. @LoadBalanced
        11. @Bean
        12. public AsyncRestTemplate asyncRestTemplate(){
        13. return new AsyncRestTemplate();
        14. }
        15. public static void main(String[] args) {
        16. SpringApplication.run(ConsumerApplication.class, args);
        17. }
        18. }
  3. Create a controller to demonstrate and verify the service discovery feature.

    1. @RestController
    2. public class TestController {
    3. @Autowired
    4. private RestTemplate restTemplate;
    5. @Autowired
    6. private AsyncRestTemplate asyncRestTemplate;
    7. @Autowired
    8. private EchoService echoService;
    9. @RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
    10. public String rest(@PathVariable String str) {
    11. return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
    12. }
    13. @RequestMapping(value = "/echo-async-rest/{str}", method = RequestMethod.GET)
    14. public String asyncRest(@PathVariable String str) throws Exception{
    15. ListenableFuture<ResponseEntity<String>> future = asyncRestTemplate.
    16. getForEntity("http://service-provider/echo/"+str, String.class);
    17. return future.get().getBody();
    18. }
    19. @RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
    20. public String feign(@PathVariable String str) {
    21. return echoService.echo(str);
    22. }
    23. }
  4. Add the following configuration to the application.properties file and specify the EDAS lightweight configuration center as the registry.

    where, 127.0.0.1 is the address of the lightweight configuration center, which must be changed to the corresponding IP address if your lightweight configuration center is deployed on another instance.The lightweight configuration center does not support port modification, so port 8080 must be used.

    1. spring.application.name=service-consumer
    2. server.port=18081
    3. spring.cloud.alicloud.ans.server-list=127.0.0.1
    4. spring.cloud.alicloud.ans.server-port=8080
  5. Execute the main function in service-consumer to enable the service.

  6. Log on to the console of the lightweight configuration center (http://127.0.0.1:8080). In the left-side navigation pane, click Services to view the list of service providers.Service providers already contain the service-consumer, and you can query the group and address of the service.

Result verification

Call the demo APIs separately to view the result of successful calls.

 Service call

Deploy applications to EDAS

ANS is designed for migrating applications from the development environment to EDAS. It allows you to directly deploy applications to EDAS without modifying any code or configurations.

The middleware’s server address, service port, and authentication during middleware communications will be set to a higher priority in the publishing process.For example, if you have the following configuration, you either retain or do not enter the following settings.

  1. spring.cloud.alicloud.ans.server-list=127.0.0.1
  2. spring.cloud.alicloud.ans.server-port=8080
  1. Add the following configuration to the pom.xml files of service-rovider and service-onsumer. Then, run mvn clean package to package native programs into executable JAR packages.

    • Provider

      1. <build>
      2. <plugins>
      3. <plugin>
      4. <groupId>org.springframework.boot</groupId>
      5. <artifactId>spring-boot-maven-plugin</artifactId>
      6. </plugin>
      7. </plugins>
      8. </build>
    • Consumer

      1. <build>
      2. <plugins>
      3. <plugin>
      4. <groupId>org.springframework.boot</groupId>
      5. <artifactId>spring-boot-maven-plugin</artifactId>
      6. </plugin>
      7. </plugins>
      8. </build>
  2. Deploy applications based on the appropriate documents for the corresponding cluster types.

    if you want to deploy Spring Cloud applications onto ECS Cluster with JAR,you should select Standard Java application runtime environment when creating applicaion.

    SC Runtime Environment

Reference configuration items

Configuration item Key Default value Description
Service name spring.cloud.alicloud.ans.client-domains spring.application.name If this item is not set, the spring.application.name value is used by default.To publish multiple services, separate them by commas (,).
Register or not spring.cloud.alicloud.ans.register-enabled true To discover a service without registration, set this item to false to disable registration.
IP address to be registered spring.cloud.alicloud.ans.client-ip N/A To specify a registered IP address for the local instance, set this item to the IP address for the top priority.
NIC with the IP address to be registered spring.cloud.alicloud.ans.client-interface-name N/A Set this item to the name of the NIC with the IP address to be registered.
Port to be registered spring.cloud.alicloud.ans.client-port N/A Set this item to the port to be registered.

FAQ

  • How do I call a successfully registered service?

    A: Because the spring-cloud-starter-alicloud-ans integrates Ribbon by default, you can call the service by using RestTemplate and FeignClient.

  • Why does service registration always fail?

    A: If the account information is correct, but registration fails when you run the demo in this document,the time on your instance may be inaccurate, resulting in authentication failure.In this case, calibrate the time on your instance. We recommend that you enable automatic time synchronization.