本文以包含服务提供者和服务消费者的Spring Cloud微服务应用为例,让您快速体验如何在MSE上构建ZooKeeper、Eureka和Nacos等服务注册中心,实现应用的服务注册与发现,以及消费者对提供者的调用。
在MSE上创建服务注册中心
本文以在MSE构建Nacos为例。
- 进入MSE实例创建页面。
- 未开通MSE集群托管
- 登录MSE产品页。
- 在MSE产品页单击超值资源包。
- 选择您需要创建的MSE实例,例如ZooKeeper、Eureka和Nacos。
进入MSE实例创建及购买页面。
- 已开通MSE集群托管
- 登录MSE管理控制台。
- 在左侧导航栏选择。
- 在实例列表页面单击创建实例。
- 设置付费模式。
在MSE购买页面选择付费模式。
MSE有预付费(包年包月)和按量付费(按小时)两种模式,如果您的服务注册中心使用时间在一个月以上,建议采用更加优惠的预付费(包年包月)模式。
- 设置实例地域和可用区。
选择您MSE实例所在地域。
说明 目前MSE现已开放了华东1(杭州)、华东2(上海)、华北2(北京)、华南1(深圳)、华北3(张家口)、华南5(呼和浩特)、西南1(成都)、中国(香港)、新加坡、美国(弗吉尼亚)、俄罗斯(莫斯科)、美国(硅谷)地域。
- 配置引擎基本信息。

配置基本参数如下:
参数 |
描述 |
引擎类型 |
选择Nacos。
|
引擎版本 |
MSE Nacos支持两种引擎版本,1.1.3和1.2.1。推荐使用1.2.1。
说明 目前仅1.2.1版本支持配置中心,1.1.3不包含配置中心功能。
|
引擎规格 |
MSE实例规格有四种规格,1核2G、2核4G、4核8G和8核16G。
请您根据实际情况选择合适的组件规格,关于组件的评估方法,请参见微服务注册配置中心实例能力评估。
|
集群节点数 |
选择集群内的节点数,即一个集群需要多少台上述规格的节点组成。
注意 强烈建议Nacos集群规模最小3个节点,否则无法保障高可用。
|
- 单击立即购买。
创建完成后,如果所创建的Nacos状态为
运行中,表示Nacos创建完成。
其中访问列对应的mse.XX.nacos.mse.aliyuncs.com为Nacos访问地址。
创建服务提供者
在本地创建服务提供者应用工程,添加依赖,开启服务注册与发现功能,并将注册中心指定为Nacos Server。
- 创建Maven工程,命名为nacos-service-provider。
- 在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版本的生命周期已结束,不推荐使用这个版本开发应用。
- 在src\main\java工程路径下创建名为com.aliware.edas的Package。
- 在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
注解表明此应用需开启服务注册与发现功能。
- 在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;
}
}
- 在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.XX.nacos.mse.aliyuncs.com为在MSE上传创建的Nacos的外网访问地址,如下图所示。
注意 如果您使用的服务注册中心为MSE的Zookeeper或者Eureka,那么您需要将本步骤的注册中心代码换成Zookeeper或者Eureka相应的代码,具体详情请参见
MSE集群托管使用说明。
- 验证结果。
- 执行nacos-service-provider中ProviderApplication的main函数,启动应用。
- 登录MSE注册中心控制台。
- 在左侧导航树中单击实例列表,并在实例列表页单击已创建的MSE实例。
- 设置MSE引擎访问白名单。
如果没有填写任何IP地址及掩码,表示允许所有地址均可访问该实例。本文以无白名单为例。
- 在实例详情页面的左侧导航树,单击服务管理。
如果在服务管理列表中存在所Provider服务表示,该服务注册成功。

创建服务消费者
本内容除介绍服务注册的功能,还将介绍Nacos服务发现与RestTemplate和FeignClient两个客户端如何配合使用。
- 创建Maven工程,命名为nacos-service-consumer。
- 在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>
- 在src\main\java工程路径下创建名为com.aliware.edas的Package。
- 在com.aliware.edas中配置RestTemplate和FeignClient。
- 在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);
}
- 在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);
}
}
- 在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);
}
}
- 在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.XX.nacos.mse.aliyuncs.com
为在MSE上传创建的Nacos的外网访地址,如下图所示。
注意 如果您使用的服务注册中心为MSE的Zookeeper或者Eureka,那么您需要将本步骤的注册中心代码换成Zookeeper或者Eureka相应的代码,具体详情请参见
MSE使用说明。
- 验证结果。
- 执行nacos-service-consumer中ConsumerApplication的main函数,启动应用。
- 登录MSE注册中心控制台。
- 在左侧导航树中单击实例列表,并在实例列表页单击已创建的MSE实例。
- 设置MSE引擎访问白名单。
如果没有填写任何IP地址及掩码,表示允许所有地址均可访问该实例。本文以无白名单为例。
- 在实例详情页面的左侧导航树单击服务管理。
如果在服务管理列表中存在所Consumer服务表示,该服务注册成功。

本地测试
在本地测试消费者对提供者的服务调用结果。
本示例以Windows系统为例。
在文档使用中是否遇到以下问题
更多建议
匿名提交