本文介绍如何使用Alibaba Cloud Toolkit部署应用至SAE,以及对应用进行监控。

前提条件

步骤一:在SAE创建Demo应用

SAE支持代码包和镜像方式部署应用。具体操作,请参见将Demo应用部署到SAE

本文以JAR包方式为例,在SAE分别创建Provider和Consumer应用。具体操作,请参见在SAE控制台使用JAR文件部署微服务应用

步骤二:创建服务提供者

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

  1. 创建命名为nacos-service-provider的Maven工程。
  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.edasPackage
  4. 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);
            }
        }             
  5. 在Packagecom.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=127.0.0.1:8848               

    其中127.0.0.1为Nacos Server的IP地址。如果您的Nacos Server部署在其他设备,则需要修改成对应的IP地址。

    说明 如果您的服务注册中心为自建服务注册中心,请将127.0.0.1替换为您的自建服务中心地址。
  7. 验证结果。
    1. 执行nacos-service-providerProviderApplicationmain函数,启动应用。
    2. 登录本地启动的Nacos Server控制台http://127.0.0.1:8848/nacos(本地Nacos控制台的默认用户名和密码同为nacos)。
    3. 在左侧导航栏中选择服务管理 > 服务列表
      可以看到服务列表中已经包含了service-provider,且在详情中可以查询该服务的详情。

步骤三:创建服务消费者

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

  1. 创建命名为nacos-service-consumer的Maven工程。
  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.edasPackage
  4. com.aliware.edas中配置RestTemplate和FeignClient。
    1. 在Packagecom.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=127.0.0.1:8848

    其中127.0.0.1为Nacos Server的IP地址。如果您的Nacos Server部署在其他设备,则需要修改成对应的IP地址。

    说明 如果您的服务注册中心为自建服务注册中心,请将127.0.0.1:8848替换为您的自建服务中心地址。
  7. 验证结果。
    1. 执行nacos-service-consumerConsumerApplicationmain函数,启动应用。
    2. 登录本地启动的Nacos Server控制台http://127.0.0.1:8848/nacos(本地Nacos控制台的默认用户名和密码同为nacos)。
    3. 在左侧导航栏中选择服务管理 > 服务列表
      可以看到服务列表中已经包含了service-consumer,且在详情中可以查询该服务的详情。

步骤四:本地测试

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

  • Linux/Unix/macOS系统:运行以下命令。
    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-resthttp://127.0.0.1:18082/echo-feign/feign-rest

本示例以Windows系统为例。

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

表示本地开发的微服务Provider和Consumer调用正常。

步骤五:部署应用至SAE

应用程序完成开发后,您需要在Cloud Toolkit中配置部署任务信息,将您的业务代码发布至步骤一:在SAE创建Demo应用所创建的应用。

  1. 配置Cloud Toolkit账户。
    1. 单击Cloud Toolkit图标Alibaba Cloud Toolkit图标,在下拉列表中单击 Preference…,在设置页面左侧导航栏选择 Alibaba Cloud Toolkit > Accounts
    2. Accounts界面中设置Access Key IDAccess Key Secret,并单击OK
      说明

      Access Key IDAccess Key Secret获取方法:

      Accounts界面中单击Get existing AK/SK,进入并登录阿里云登录页面,系统自动跳转至安全信息管理页面,获取Access Key IDAccess Key Secret

  2. 配置部署任务。
    1. 在IntelliJ IDEA上单击Cloud Toolkit 图标Alibaba Cloud Toolkit图标,并在下拉列表中选择Deploy to SAE
    2. Deploy to SAE运行配置页面,配置应用部署参数。配置完成后单击Apply保存设置。
      说明 如果您使用自建服务注册中心,您还需要在Advanced页签中配置启动命令-Dnacos.use.endpoint.parsing.rule=false-Dnacos.use.cloud.namespace.parsing=false
      • Provider 应用配置

        配置应用部署的区域、命名空间和步骤一:在SAE创建Demo应用中创建的应用。

        在ACT上配置Provider
      • Consumer应用配置

        配置应用部署的区域、命名空间和创建的应用。

        Consumer应用配置
  3. 部署应用。
    单击Run,运行Provider应用后,然后运行Consumer应用。运行时
  4. 结果验证。
    1. 为Consumer应用绑定SLB。
      具体操作,请参见为应用绑定SLB为Consumer绑定SLB
    2. 访问Consumer。
      1. 对Consumer发起HTTP请求。

        curl http://47.111.XX.XX/echo-feign/feign-rest

      2. 对Consumer发起HTTP请求,Consumer调用Provider。

        curl http://47.111.XX.XX/echo-rest/rest-rest

      调用请求
    3. 在应用监控大盘查看调用数据。

      在Consumer应用的应用监控中查看应用调用信息。

      应用总览应用详情接口调用详情