本文介绍如何使用Alibaba Cloud Toolkit部署应用至SAE,以及对应用进行监控。
前提条件
- 开通SAE服务。
- 下载Maven并设置环境变量。
- 下载并安装JDK 1.8或更高版本。
- 下载并安装IntelliJ IDEA (2018.3或更高版本)。说明 由于JetBrains插件官方服务器设立在海外,如果因访问缓慢导致无法下载安装,请联系SAE工程师获取离线安装包。更多信息,请参见联系我们。
- IntelliJ IDEA中已安装Alibaba Cloud Toolkit插件。具体操作,请参见通过IntelliJ IDEA插件部署应用。
步骤一:在SAE创建Demo应用
SAE支持代码包和镜像方式部署应用。具体操作,请参见将Demo应用部署到SAE。
本文以JAR包方式为例,在SAE分别创建Provider和Consumer应用。具体操作,请参见在SAE控制台使用JAR文件部署微服务应用。
步骤二:创建服务提供者
在本地创建服务提供者应用工程,添加依赖,开启服务注册与发现功能,并将注册中心指定为Nacos Server。
- 创建命名为
nacos-service-provider
的Maven工程。 - 在
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
,并添加如下代码。其中
@EnableDiscoveryClient
注解表明此应用需开启服务注册与发现功能。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); } }
- 在Package
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=127.0.0.1:8848
其中
127.0.0.1
为Nacos Server的IP地址。如果您的Nacos Server部署在其他设备,则需要修改成对应的IP地址。说明 如果您的服务注册中心为自建服务注册中心,请将127.0.0.1
替换为您的自建服务中心地址。 - 验证结果。
步骤四:创建服务消费者
本步骤介绍服务注册的功能,以及Nacos服务发现与RestTemplate和FeignClient两个客户端如何配合使用。
- 创建命名为
nacos-service-consumer
的Maven工程。 - 在
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
中创建类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=127.0.0.1:8848
其中
127.0.0.1
为Nacos Server的IP地址。如果您的Nacos Server部署在其他设备,则需要修改成对应的IP地址。说明 如果您的服务注册中心为自建服务注册中心,请将127.0.0.1:8848
替换为您的自建服务中心地址。 - 验证结果。
步骤五:本地测试
在本地测试消费者对提供者的服务调用结果。
- 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系统为例。

表示本地开发的微服务Provider和Consumer调用正常。
步骤六:部署应用至SAE
应用程序完成开发后,您需要在Cloud Toolkit中配置部署任务信息,将您的业务代码发布至步骤一:在SAE创建Demo应用所创建的应用。
- 配置Cloud Toolkit账户。
- 配置部署任务。
- 部署应用。单击Run,运行Provider应用后,然后运行Consumer应用。
- 结果验证。