全部產品
Search
文件中心

Serverless App Engine:使用Alibaba Cloud Toolkit自動化部署微服務至SAE

更新時間:Feb 21, 2025

本文介紹如何使用Alibaba Cloud Toolkit部署應用至SAE,以及對應用進行監控。

前提條件

步驟一:在SAE建立Demo應用

SAE支援程式碼封裝和鏡像方式部署應用。具體操作,請參見將Java應用部署到SAE 1.0

本文以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表徵圖,在下拉式清單中單擊 Preferensce…,在設定頁面左側導覽列選擇 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。

      具體操作,請參見為應用綁定CLB為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. 在應用監控大盤查看調用資料。