开发者社区 > 云原生 > 微服务 > 正文

如何使用Alibaba Cloud Toolkit自动化部署微服务至SAE?

如何使用Alibaba Cloud Toolkit自动化部署微服务至SAE?

展开
收起
小天使爱美 2020-03-27 13:02:45 1321 0
1 条回答
写回答
取消 提交回答
  • 本以介绍如何使用Alibaba Cloud Toolkit部署应用至SAE,以及对应用进行监控。

    前提条件 开通 SAE 服务。 下载 Maven 并设置环境变量。 下载并安装 JDK 1.8或更高版本。 下载并安装 IntelliJ IDEA (2018.3或更高版本)。 说明 由于JetBrains插件官方服务器设立在海外,如果因访问缓慢导致无法下载安装,请加入文末交流群,从Cloud Toolkit产品运营部获取离线安装包。 IntelliJ IDEA中已安装Alibaba Cloud Toolkit插件,具体请参见安装Cloud Toolkit。 步骤一:在SAE创建Demo应用 SAE支持JAR包、WAR包和镜像三种方式部署应用,具体请参见应用部署概述。

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

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

    创建命名为nacos-service-provider的Maven工程。 在pom.xml文件中添加依赖。 以Spring Boot 2.1.4.RELEASE和Spring Cloud Greenwich.SR1为例,依赖如下:

    org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE

    com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 2.1.1.RELEASE org.springframework.boot spring-boot-starter-web

    org.springframework.cloud spring-cloud-dependencies Greenwich.SR1 pom import

    org.springframework.boot spring-boot-maven-plugin
    示例中使用的版本为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);
        }
    }             
    

    在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; } }
    在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-service-provider中ProviderApplication的main函数,启动应用。 登录本地启动的Nacos Server控制台http://127.0.0.1:8848/nacos(本地Nacos控制台的默认用户名和密码同为nacos)。 在左侧导航栏中选择服务管理 > 服务列表 。 可以看到服务列表中已经包含了service-provider,且在详情中可以查询该服务的详情。 步骤四:创建服务消费者 本内容除介绍服务注册的功能,还将介绍Nacos服务发现与RestTemplate和FeignClient两个客户端如何配合使用。

    创建命名为nacos-service-consumer的Maven工程。 在pom.xml中添加依赖。 org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE

    com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 2.1.1.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-openfeign

    org.springframework.cloud spring-cloud-dependencies Greenwich.SR1 pom import

    org.springframework.boot spring-boot-maven-plugin
    在src\main\java下创建名为com.aliware.edas的Package。 在 com.aliware.edas中配置RestTemplate和FeignClient。 在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); }
    在 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=127.0.0.1:8848 其中127.0.0.1为Nacos Server的IP地址。如果您的Nacos Server部署在其他设备,则需要修改成对应的IP地址。

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

    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调用成功 表示本地开发的微服务 Provider 和 Consumer 调用正常。

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

    配置 Cloud Toolkit 账户。 单击 Cloud Toolkit 图标Alibaba Cloud Toolkit图标,在下拉列表中单击 Preference…,在设置页面左侧导航栏选择 Alibaba Cloud Toolkit > Accounts 。 在Accounts界面中设置Access Key ID和Access Key Secret,并单击OK。 说明 Access Key ID和Access Key Secret获取方法:

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

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

    在ACT上配置Provider Consumer应用配置 配置应用部署的区域、命名空间和步骤2中创建的应用。

    Consumer应用配置 部署应用。 单击Run,运行Provider应用后,然后运行Consumer应用。运行时 结果验证。 为Consumer应用绑定SLB。 具体操作参见为应用绑定SLB。为Consumer绑定SLB为Consumer绑定SLB成功示意图 访问Consumer。 对Consumer发起HTTP请求。 curl http://47.111.58.18/echo-feign/feign-rest

    对Consumer发起HTTP请求,Consumer调用Provider。 curl http://47.111.58.18/echo-rest/rest-rest

    调用请求 在应用监控大盘查看调用数据。 在Consumer应用的应用监控中查看应用调用信息。

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

    2020-03-27 13:11:08
    赞同 展开评论 打赏

为微服务建设降本增效,为微服务落地保驾护航。

相关电子书

更多
SAE 引领应用步入 Serverless 全托管新时代 立即下载
Serverless时代下大规模微服务应用运维的最佳实践 立即下载
云效携手SAE助力掌讯科技持续降本提效 立即下载