Enterprise Distributed Application Service (EDAS) provides the general availability (GA) version of the Nacos registry. Applications developed in Nacos can use this shared registry of EDAS without code modification. 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.

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 managed in Microservice Engine (MSE). After you deploy your application to EDAS, you can use the application management, microservice governance, and cloud native Platform as a Service (PaaS) features of EDAS regardless of the registry type.

  • For more information about how to deploy an application to EDAS, see Overview and Overview.

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.

Preparations

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:
    1. Decompress the downloaded Nacos Server package.
    2. Go to the nacos/bin directory to start Nacos Server.
      • On Linux, UNIX, or macOS, run the sudo sh startup.sh -m standalone command.
      • On 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 then specify Nacos Server as the registry.

  1. Create a Maven project that is named nacos-service-provider.
  2. 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.
  3. In src\main\java, create a package that is named com.aliware.edas.
  4. 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);
            }
        }             
  5. 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;
            }
        }              
  6. In src\main\resources, 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.

  7. Verify the result.
    1. Use the main function of ProviderApplication in nacos-service-provider to start the application.
    2. 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.
    3. 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.

  1. Create a Maven project that is named nacos-service-consumer.
  2. 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>        
  3. In src\main\java, create a package that is named com.aliware.edas.
  4. In the com.aliware.edas package, configure RestTemplate and FeignClient.
    1. 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);
      }                   
    2. 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);
          }
      }
  5. 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);
            }
    
        }           
  6. In src\main\resources, 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.

  7. Verify the result.
    1. Run the main function of ConsumerApplication in nacos-service-consumer to start the application.
    2. 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.
    3. 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.

  • On Linux, UNIX, or macOS, run the following commands:
    curl http://127.0.0.1:18082/echo-rest/rest-rest
    curl http://127.0.0.1:18082/echo-feign/feign-rest
  • On Windows, enter http://127.0.0.1:18082/echo-rest/rest-rest and http://127.0.0.1:18082/echo-feign/feign-rest in the browser.

What to do next

After applications are developed, they can be deployed to EDAS. For more information, see Overview and Overview.

Configuration items for reference

Configuration itemKeyDefault valueDescription
Server addressspring.cloud.nacos.discovery.server-addrNoneThe IP address and the port on which Nacos Server listens.
Service namespring.cloud.nacos.discovery.service${spring.application.name}The name of the current service.
Network interface controller (NIC) namespring.cloud.nacos.discovery.network-interfaceNoneIf 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 addressspring.cloud.nacos.discovery.ipNoneThis configuration item has the highest priority.
Registered portspring.cloud.nacos.discovery.port-1No configurations are required by default. The system automatically detects the port.
Namespacespring.cloud.nacos.discovery.namespaceNoneNamespaces 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.
Metadataspring.cloud.nacos.discovery.metadataNoneThis item must be configured in the Map format. You can specify metadata information that is related to the service based on your needs.
Clusterspring.cloud.nacos.discovery.cluster-nameDEFAULTThe name of the Nacos cluster.
Endpointspring.cloud.nacos.discovery.endpointUTF-8The 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 integrationribbon.nacos.enabledtrueChange the value only when you need.

For more information about Spring Cloud Alibaba Nacos Discovery, see the open source version of Nacos Discovery.