如何在MSE上为Spring Cloud应用构建服务注册中心
以包含服务提供者和服务消费者的Spring Cloud微服务应用为例,本文介绍如何在MSE上快速构建ZooKeeper、Eureka和Nacos等服务注册中心,实现应用的服务注册与发现,以及消费者对提供者的调用。
前提条件
如果通过公网的方式访问MSE,确保服务提供者和服务消费者都已具备开启访问公网的能力。
在MSE上创建服务注册中心
本文以在MSE构建Nacos为例。
如果您需要的注册中心为ZooKeeper,具体操作请参见购买并构建ZooKeeper引擎。
- 进入MSE实例创建页面。
- 根据MSE实例所在地域,选择实例的地域。说明 关于MSE当前支持的地域,请参见开服地域。
- 配置引擎基本信息。
配置项 描述 产品版本 根据需要选择开发版或者专业版。
开发版用于开发自测或产品体验场景,不可用于生产环境。关于不同版本类型的差异,请参见实例及版本选型。
引擎类型 选择Nacos。 引擎版本 Nacos引擎版本。 实例名称 自定义实例名称。 引擎规格 MSE实例规格有多种规格: - 专业版支持1核2G、2核4G、4核8G、8核16G和16核32G。
- 开发版支持1核2G和2核4G。
请根据实际情况选择合适的组件规格。关于组件的评估方法,请参见实例能力评估。
集群节点数 选择集群内的节点数,即一个集群需要多少台上述规格的节点组成。 重要 开发版只支持1个节点。为保障高可用,专业版至少配置3个节点。资源组 选择资源组。 资源组可以实现企业内部多用户、多项目的资源分级管理。
单击立即购买。
创建完成后,访问MSE管理控制台,选择 ,在实例列表的运行状态列查看所创建的Nacos状态,若状态为运行中,表示Nacos创建完成。
在访问方式列,查看实例的Nacos访问地址,格式为mse.XX.nacos.mse.aliyuncs.com。
创建服务提供者
在本地创建服务提供者应用工程,然后添加依赖,并开启服务注册与发现功能,并将注册中心指定为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上传创建的Nacos的外网访问地址,格式为mse.XX.nacos.mse.aliyuncs.com。
重要如果您使用的服务注册中心为MSE的Zookeeper或者Eureka,那么您需要将本步骤的注册中心代码换成Zookeeper或者Eureka相应的代码,具体详情请参见MSE集群托管使用说明。
验证结果。
执行nacos-service-provider中ProviderApplication的main函数,启动应用。
登录MSE注册中心控制台。
在左侧导航栏,选择 ,并在实例列表页单击已创建的MSE实例。
设置MSE引擎访问白名单。
如果没有填写任何IP地址及掩码,表示允许所有地址均可访问该实例。本文以无白名单为例。
在左侧导航栏,选择服务管理 > 服务列表。
创建服务消费者
本内容除介绍服务注册的功能,还将介绍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上传创建的Nacos的外网访问地址,格式为
mse.XX.nacos.mse.aliyuncs.com
。重要如果您使用的服务注册中心为MSE的Zookeeper或者Eureka,那么您需要将本步骤的注册中心代码换成Zookeeper或者Eureka相应的代码,具体详情请参见MSE使用说明。
验证结果。
执行nacos-service-consumer中ConsumerApplication的main函数,启动应用。
登录MSE注册中心控制台。
在左侧导航栏,选择 ,并在实例列表页单击已创建的MSE实例。
设置MSE引擎访问白名单。
如果没有填写任何IP地址及掩码,表示允许所有地址均可访问该实例。本文以无白名单为例。
在左侧导航栏,选择服务管理 > 服务列表。
本地测试
在本地测试消费者对提供者的服务调用结果。
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。
本示例以Windows系统为例。
常见问题
本地开发的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版本的生命周期已结束,不推荐使用此版本开发应用。