Spring Cloud 微服务注册发现

本地开发应用

前提条件

着手开发前,请确认已完成以下工作:

  • 下载 Maven 并设置环境变量。
  • 下载最新版本 Nacos Server
  • 按照以下步骤启动 Nacos Server。
    1. 解压 Nacos Server 压缩包。
    2. 进入 nacos/bin 目录,启动 Nacos Server。

tip 提示

  • Linux/Unix/Mac 系统:sh startup.sh -m standalone
  • Windows 系统:双击运行 startup.cmd

创建服务提供者

在本地创建服务提供者应用工程,添加依赖,开启服务注册与发现功能,并将注册中心指定为 Nacos Server。

  1. 创建名为 nacos-service-provider 的 Maven 工程。

  2. 在 pom.xml 文件中添加依赖,此处以 Spring Boot 2.1.4.RELEASE 和 Spring Cloud Greenwich.SR1 为例:

    ```xml

    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>
  1. * 使用版本为 Spring Cloud Greenwich,则对应 Spring Cloud Alibaba 版本为 2.1.1.RELEASE
  2. * 使用版本为 Spring Cloud Finchley 版本,则对应 Spring Cloud Alibaba 版本为 2.0.1.RELEASE
  3. * 使用版本为 Spring Cloud Edgware 版本,则对应 Spring Cloud Alibaba 版本为 1.5.1.RELEASE
  4. > tip 提示
  5. Spring Cloud Edgware 版本的生命周期已结束,不建议使用该版本开发应用。
  6. >
  7. 3. `src/main/java` 下创建 `package: io.terminus.erda.trial.demo.hellospringcloud`
  8. `package: io.terminus.erda.trial.demo.hellospringcloud` 中创建服务提供者的启动类 `ProviderApplication`,并添加如下代码:
  9. ```java
  10. package io.terminus.erda.trial.demo.hellospringcloud;
  11. import org.springframework.boot.SpringApplication;
  12. import org.springframework.boot.autoconfigure.SpringBootApplication;
  13. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  14. /**
  15. * @author erda
  16. */
  17. @SpringBootApplication
  18. @EnableDiscoveryClient
  19. public class ProviderApplication {
  20. public static void main(String[] args) {
  21. SpringApplication.run(ProviderApplication.class, args);
  22. }
  23. }

tip 提示 @EnableDiscoveryClient 注解表明此应用需开启服务注册与发现功能。

package: io.terminus.erda.trial.demo.hellospringcloud 中创建 EchoController,指定 URL mapping 为 {/echo/{String}},指定 HTTP 方法为 GET,方法参数从 URL 路径中获得,回显收到的参数。

  1. package io.terminus.erda.trial.demo.hellospringcloud;
  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. /**
  7. * @author erda
  8. */
  9. @RestController
  10. public class EchoController {
  11. @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
  12. public String echo(@PathVariable String string) {
  13. return string;
  14. }
  15. }
  1. src/main/resources 路径下创建文件 application.yml,并添加如下配置,指定 Nacos Server 的地址。

    1. spring:
    2. application.name: service-provider
    3. cloud:
    4. nacos:
    5. discovery:
    6. server-addr: ${NACOS_ADDRESS:127.0.0.1:8848}
    7. namespace: ${NACOS_TENANT_ID:}

    tip 提示 127.0.0.1 为 Nacos Server 的地址。若 Nacos Server 部署在另外一台机器,则需修改为对应的 IP 地址。

  2. 验证结果。

  3. 执行 nacos-service-providerProviderApplicationmain 函数,启动应用。

    1. 登录本地启动的 Nacos Server 控制台 http://127.0.0.1:8848/nacos(本地 Nacos 控制台的默认用户名和密码同为 nacos)。

    2. 在左侧导航栏选择 服务管理 > 服务列表,列表中已包含 service-provider,且可在详情中查看具体信息。

创建服务消费者

  1. 创建名为 nacos-service-consumer 的 Maven 工程。

  2. 在 pom.xml 中添加依赖。

    ```xml

    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>
  1. 3. `src/main/java` 下创建 `package: io.terminus.erda.trial.demo.hellospringcloud`
  2. 4. `package: io.terminus.erda.trial.demo.hellospringcloud` 中配置 `RestTemplate` `FeignClient`
  3. `package: io.terminus.erda.trial.demo.hellospringcloud` 中创建一个接口类 `EchoService `,添加 `@FeignClient` 注解,并配置对应的 HTTP URL 地址及 HTTP 方法。
  4. ```java
  5. package io.terminus.erda.trial.demo.hellospringcloud;
  6. import org.springframework.cloud.openfeign.FeignClient;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. /**
  11. * @author erda
  12. */
  13. @FeignClient(name = "service-provider")
  14. public interface EchoService {
  15. @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
  16. String echo(@PathVariable("str") String str);
  17. }

package: io.terminus.erda.trial.demo.hellospringcloud 中创建启动类 ConsumerApplication 并添加相关配置。

  • 使用 @EnableDiscoveryClient 注解启用服务注册与发现。
  • 使用 @EnableFeignClients 注解激活 FeignClient
  • 添加 @LoadBalanced 注解将 RestTemplate 与服务发现集成。

    1. package io.terminus.erda.trial.demo.hellospringcloud;
    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. /**
    10. * @author erda
    11. */
    12. @SpringBootApplication
    13. @EnableDiscoveryClient
    14. @EnableFeignClients
    15. public class ConsumerApplication {
    16. @LoadBalanced
    17. @Bean
    18. public RestTemplate restTemplate() {
    19. return new RestTemplate();
    20. }
    21. public static void main(String[] args) {
    22. SpringApplication.run(ConsumerApplication.class, args);
    23. }
    24. }
  1. package: io.terminus.erda.trial.demo.hellospringcloud 中创建类 TestController 以演示和验证服务发现功能。

    ```java package io.terminus.erda.trial.demo.hellospringcloud;

    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;

    /**

    • @author edas */ @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) {

      1. return restTemplate.getForObject("http://service-provider/echo/" + str,
      2. String.class);

      }

      @RequestMapping(value = “/echo-feign/{str}”, method = RequestMethod.GET) public String feign(@PathVariable String str) {

      1. return echoService.echo(str);

      }

    }

  2. src/main/resources 路径下创建文件 application.yml,并添加如下配置,指定 Nacos Server 的地址。

    1. spring:
    2. application.name: service-consumer
    3. cloud:
    4. nacos:
    5. discovery:
    6. server-addr: ${NACOS_ADDRESS:127.0.0.1:8848}
    7. namespace: ${NACOS_TENANT_ID:}

    tip 127.0.0.1:8848 为 Nacos Server 的地址。若 Nacos Server 部署在另外一台机器,则需修改为对应的地址。

  3. 验证结果。

    1. 执行 nacos-service-consumerConsumerApplicationmain 函数,启动应用。
    2. 登录本地启动的 Nacos Server 控制台 http://127.0.0.1:8848/nacos(本地 Nacos 控制台的默认用户名和密码同为 nacos)。
    3. 在左侧导航栏选择 服务管理 > 服务列表,列表中已包含 service-consumer,且可在详情中查看具体信息。

本地测试

在本地测试消费者对提供者的服务调用结果。

部署应用至 Erda

部署应用至 Erda 时,需注意如下配置中的环境变量将被自动注入。

  1. spring:
  2. application.name: service-provider
  3. cloud:
  4. nacos:
  5. discovery:
  6. server-addr: ${NACOS_ADDRESS:127.0.0.1:8848}
  7. namespace: ${NACOS_TENANT_ID:}
  1. spring:
  2. application.name: service-consumer
  3. cloud:
  4. nacos:
  5. discovery:
  6. server-addr: ${NACOS_ADDRESS:127.0.0.1:8848}
  7. namespace: ${NACOS_TENANT_ID:}

修改 dice.yml,添加注册中心 v2.0 的依赖。

Spring Cloud 微服务注册发现 - 图1

完成服务部署后,Erda 将自动注入两个环境变量:

  • NACOS_ADDRESS
  • NACOS_TENANT_ID

点击注册中心,即可查看注册完成的服务。

Spring Cloud 微服务注册发现 - 图2