文档

在Spring Cloud应用中接入MSE Nacos作为服务注册中心

更新时间:

通过配置Spring Cloud应用指向MSE Nacos的服务地址,应用启动后能够自动向Nacos注册中心注册服务实例信息,并实现服务发现、配置管理等功能。MSE集成了多种注册中心产品,提供企业级的高可用性、安全性和可扩展能力,降低微服务架构下的运维复杂性,提升系统的稳定性和可靠性。本文介绍如何在Spring Cloud应用中接入MSE Nacos作为服务注册中心,实现服务调用。

前提条件

重要

如果通过公网的方式访问MSE,确保服务提供者和服务消费者都已具备开启访问公网的能力。

创建服务提供者

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

  1. 创建Maven工程,命名为nacos-service-provider

  2. pom.xml中添加如下依赖。

    以Spring Boot 2.1.4.RELEASE和Spring Cloud Greenwich.SR1为例,依赖如下:

    <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,对应Spring Cloud Alibaba版本为2.1.1.RELEASE。

    • 如果使用Spring Cloud Finchley版本,对应Spring Cloud Alibaba版本为2.0.1.RELEASE。

    • 如果使用Spring Cloud Edgware版本,对应Spring Cloud Alibaba版本为1.5.1.RELEASE。

    重要

    Spring Cloud Edgware版本的生命周期已结束,不推荐使用这个版本开发应用。

  3. 在src\main\java工程路径下创建名为com.aliware.edas的Package

  4. 在com.aliware.edas中创建服务提供者的启动类ProviderApplication,并添加如下代码。

    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);
            }
        }             
    说明

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

  5. 在com.aliware.edas中创建EchoController。指定URL mapping为/echo/{String},指定HTTP方法为GET,方法参数从URL路径中获得,回显收到的参数。

    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. 在src\main\resources工程路径下创建文件application.properties,在application.properties中添加如下配置,并指定Nacos Server的访问地址。

    spring.application.name=service-provider
    server.port=18081
    spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848               

    在实例列表页面,可以查看MSE Nacos的外网访问地址,格式为mse.XX.nacos.mse.aliyuncs.com。

    重要

    如果您使用的服务注册中心为MSE的Zookeeper或者Eureka,那么您需要将本步骤的注册中心代码换成Zookeeper或者Eureka相应的代码,具体详情请参见MSE集群托管使用说明

  7. 查看应用是否注册成功。

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

    2. 登录MSE注册配置中心管理控制台,并在顶部菜单栏选择地域。

    3. 在左侧导航栏,选择注册配置中心 > 实例列表。单击目标实例名称。

    4. 在左侧导航栏,选择服务管理 > 服务列表

    5. 在服务列表页面,查看应用是否注册成功。

创建服务消费者

本步骤除介绍服务注册的功能,还将介绍Nacos服务发现与RestTemplate和FeignClient两个客户端如何配合使用。

  1. 创建Maven工程,命名为nacos-service-consumer

  2. pom.xml中添加如下依赖。

    <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. 在src\main\java工程路径下创建名为com.aliware.edas的Package

  4. 在com.aliware.edas中配置RestTemplate和FeignClient。

    1. 在com.aliware.edas中创建一个接口类EchoService,添加@FeignClient注解,并配置对应的HTTP URL地址及HTTP方法。

      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. 在com.aliware.edas中创建启动类ConsumerApplication并添加相关配置。

      • 使用@EnableDiscoveryClient注解启用服务注册与发现。

      • 使用@EnableFeignClients注解激活FeignClient。

      • 使用@LoadBalanced注解将RestTemplate与服务发现集成。

      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. 在com.aliware.edas中创建类TestController,以演示和验证服务发现功能。

    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. 在src\main\resources工程路径下创建文件application.properties,在application.properties中添加如下配置,指定Nacos Server的地址。

    spring.application.name=service-consumer
    server.port=18082
    spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848

    实例列表页面,可以查看MSE Nacos的外网访问地址,格式为mse.XX.nacos.mse.aliyuncs.com

    重要

    如果您使用的服务注册中心为MSE的Zookeeper或者Eureka,那么您需要将本步骤的注册中心代码换成Zookeeper或者Eureka相应的代码,具体详情请参见MSE使用说明

  7. 查看应用是否注册成功。

    1. 执行nacos-service-consumerConsumerApplicationmain函数,启动应用。

    2. 登录MSE注册配置中心管理控制台,并在顶部菜单栏选择地域。

    3. 在左侧导航栏,选择注册配置中心 > 实例列表。单击目标实例名称。

    4. 在左侧导航栏,选择服务管理 > 服务列表

    5. 在服务列表页面,查看应用是否注册成功。

本地测试

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

  • Linux/Unix/Mac系统:使用如下命令进行服务调用。

    curl http://127.0.0.1:18082/echo-rest/rest-rest
    curl http://127.0.0.1:18082/echo-feign/feign-rest
  • Windows系统:在浏览器中输入http://127.0.0.1:18082/echo-rest/rest-rest和http://127.0.0.1:18082/echo-feign/feign-rest。

    如图所示表明服务调用成功。

    Spring Cloud微服务应用是使用MSE调用成功

常见问题

本地开发的Spring Cloud微服务应用,其服务注册中心为MSE上创建的Nacos,应用运行后,在MSE服务管理页面看不到服务信息,如何处理?

您需要对您的应用访问进行白名单配置。具体操作,请参见设置白名单

说明

MSE默认设置为127.0.0.1/32,表示禁止所有地址的访问。

MSE支持哪些Spring Cloud版本?

  • Spring Cloud Greenwich对应的Spring Cloud Alibaba版本为2.1.1.RELEASE。

  • Spring Cloud Finchley对应的Spring Cloud Alibaba版本为 2.0.1.RELEASE。

  • Spring Cloud Edgware对应的 Spring Cloud Alibaba版本为1.5.1.RELEASE。

重要

Spring Cloud Edgware版本的生命周期已结束,不推荐使用此版本开发应用。

  • 本页导读 (1)
文档反馈