edit-icon download-icon

Service registration and discovery

Last Updated: Dec 06, 2018

Currently, EDAS fully supports Spring Cloud applications and they can be deployed directly in EDAS.

To provide Alibaba’s middleware for you through cloud services, EDAS has replaced some components of Spring Cloud for higher security and ease of use.

This document describes how to enable the service registration and discovery function of EDAS for your applications.

Download the Demo source code sc-vip-server and sc-vip-client.

Enable service registration and discovery

A simple example is used below to describe how to enable service registration and discovery.

Create a service provider

The service provider in this example provides a simple echo service and registers itself with the service discovery center.

  1. Create a Spring Cloud project named sc-vip-server.

  2. Introduce necessary dependencies in pom.xml:

    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>1.5.8.RELEASE</version>
    5. <relativePath/>
    6. </parent>
    7. <dependencies>
    8. <dependency>
    9. <groupId>org.springframework.cloud</groupId>
    10. <artifactId>spring-cloud-starter-vipclient</artifactId>
    11. <version>1.3</version>
    12. </dependency>
    13. <dependency>
    14. <groupId>org.springframework.cloud</groupId>
    15. <artifactId>spring-cloud-starter-pandora</artifactId>
    16. <version>1.3</version>
    17. </dependency>
    18. </dependencies>
    19. <dependencyManagement>
    20. <dependencies>
    21. <dependency>
    22. <groupId>org.springframework.cloud</groupId>
    23. <artifactId>spring-cloud-dependencies</artifactId>
    24. <version>Dalston.SR4</version>
    25. <type>pom</type>
    26. <scope>import</scope>
    27. </dependency>
    28. </dependencies>
    29. </dependencyManagement>

    If you do not want to configure the parent of the project as spring-boot-starter-parent, you can add dependencyManagement and set scope=import as follows to manage dependencies.

    1. <dependencyManagement>
    2. <dependencies>
    3. <dependency>
    4. <groupId>org.springframework.boot</groupId>
    5. <artifactId>spring-boot-dependencies</artifactId>
    6. <version>1.5.8.RELEASE</version>
    7. <type>pom</type>
    8. <scope>import</scope>
    9. </dependency>
    10. </dependencies>
    11. </dependencyManagement>
  3. Add the code of the service provider, in which the annotation of @EnableDiscoveryClient 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. PandoraBootstrap.run(args);
    6. SpringApplication.run(ServerApplication.class, args);
    7. PandoraBootstrap.markStartupAndWait();
    8. }
    9. }
  4. Create an EchoController to provide simple echo service.

    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. }
  5. Configure the application name and the listener port number in the application.properties file in resources.

    1. spring.application.name=service-provider
    2. server.port=18081

Create a service consumer

A service consumer is created in this example. The service consumer calls the service provider through RestTemplate, AsyncRestTemplate, and FeignClient.

  1. Create a Spring Cloud project named sc-vip-client.

  2. Introduce necessary dependencies in pom.xml:

    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>1.5.8.RELEASE</version>
    5. <relativePath/>
    6. </parent>
    7. <dependencies>
    8. <dependency>
    9. <groupId>org.springframework.cloud</groupId>
    10. <artifactId>spring-cloud-starter-vipclient</artifactId>
    11. <version>1.3</version>
    12. </dependency>
    13. <dependency>
    14. <groupId>org.springframework.cloud</groupId>
    15. <artifactId>spring-cloud-starter-pandora</artifactId>
    16. <version>1.3</version>
    17. </dependency>
    18. <dependency>
    19. <groupId>org.springframework.cloud</groupId>
    20. <artifactId>spring-cloud-starter-feign</artifactId>
    21. </dependency>
    22. </dependencies>
    23. <dependencyManagement>
    24. <dependencies>
    25. <dependency>
    26. <groupId>org.springframework.cloud</groupId>
    27. <artifactId>spring-cloud-dependencies</artifactId>
    28. <version>Dalston.SR4</version>
    29. <type>pom</type>
    30. <scope>import</scope>
    31. </dependency>
    32. </dependencies>
    33. </dependencyManagement>

    To demonstrate the use of FeignClient, an additional dependency of spring-cloud-starter-feign is added to the pom.xml file compared to that of the service provider.

  3. Different from the server, the clients support other functions in addition to service enabling and registration. In addition to service enabling and registration, two more configurations must be added to use the three clients of RestTemplate, AsyncRestTemplate, and FeignClient:

    • Add the annotation of @LoadBalanced to combine RestTemplate, AsyncRestTemplate, and service discovery.
    • Activate FeignClients by using the annotation of @EnableFeignClients.

      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. PandoraBootstrap.run(args);
      17. SpringApplication.run(ConsumerApplication.class, args);
      18. PandoraBootstrap.markStartupAndWait();
      19. }
      20. }
  4. Complete the configuration of FeignClient of EchoService before using it. Configure the service name and the HTTP request corresponding to the method. The service name is service-provider configured in the sc-vip-server project. The code is as follows:

    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. }
  5. Create a Controller for call test.

    /echo-rest/ verifies the call of the service provider through RestTemplate.

    /echo-async-rest/ verifies the call of the service provider through AsyncRestTemplate.

    /echo-feign/ verifies the call of the service provider through FeignClient.

    1. @RestController
    2. public class Controller {
    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. }
  6. Configure the application name and the listener port number.

    1. spring.application.name=service-consumer
    2. server.port=18082

Local development and commissioning

Start the Lightweight Configuration Center

The Lightweight Configuration Center must be started for local development and commissioning, which includes a lightweight version of the server of EDAS service registration and discovery. See Lightweight Configuration Center for details.

Start the application

The application can be locally started in two ways.

Start in IDE

To start the application in IDE, configure the startup parameter -Dvipserver.server.port=8080 in VM options and start the application directly using the main method.

If your Lightweight Configuration Center and the application are deployed on different computers, hosts binding is required. See Lightweight Configuration Center for details.

Start by using FatJar

  1. Add FatJar packaging plugins.

    To package pandora-boot project into FatJar by using Maven, the following plugins should be added in pom.xml. To avoid conflicts with other packaging plugins, do not add other FatJar plugins in build plugin.

    1. <build>
    2. <plugin>
    3. <groupId>com.taobao.pandora</groupId>
    4. <artifactId>pandora-boot-maven-plugin</artifactId>
    5. <version>2.1.9.1</version>
    6. <executions>
    7. <execution>
    8. <phase>package</phase>
    9. <goals>
    10. <goal>repackage</goal>
    11. </goals>
    12. </execution>
    13. </executions>
    14. </plugin>
    15. </build>
  2. After adding the plugins, run the maven command mvn clean package under the home directory of the project to create a package. The created FatJar file is located under the target directory.

  3. Start the application using Java command.

    1. java -Dvipserver.server.port=8080 -Dpandora.location=/Users/{$username}/.m2/repository/com/taobao/pandora/taobao-hsf.sar/dev-SNAPSHOT/taobao-hsf.sar-dev-SNAPSHOT.jar -jar sc-vip-server-0.0.1-SNAPSHOT.jar

NOTE: The path specified by -Dpandora.location must be a full path placed before sc-vip-server-0.0.1-SNAPSHOT.jar.

Demonstration

Start the service and call the service provider through the clients. Each call is successful.

Calling result

FAQ

  1. Failed to enable service discovery for AsyncRestTemplate.

    AsyncRestTemplate is not enabled with service discovery until recently and versions after Dalston are required. See the pull request for details.

  2. FatJar packaging plugin conflict

    To avoid conflicts with other packaging plugins, do not add other FatJar plugins in build plugin.

  3. Can taobao-hsf.sar be included during packaging?

    Yes, but this is not recommended.

    Modify the pandora-boot-maven-plugin plugin and set excludeSar as false to automatically include taobao-hsf.sar during packaging.

    1. <plugin>
    2. <groupId>com.taobao.pandora</groupId>
    3. <artifactId>pandora-boot-maven-plugin</artifactId>
    4. <version>2.1.9.1</version>
    5. <configuration>
    6. <excludeSar>false</excludeSar>
    7. </configuration>
    8. <executions>
    9. <execution>
    10. <phase>package</phase>
    11. <goals>
    12. <goal>repackage</goal>
    13. </goals>
    14. </execution>
    15. </executions>
    16. </plugin>

    In this way, the package can be started without a configured Pandora address.

    1. java -jar -Dvipserver.server.port=8080 sc-vip-server-0.0.1-SNAPSHOT.jar

    Restore the configuration to excluding the SAR package before deploying an application in EDAS.

Thank you! We've received your feedback.