This topic describes how to use Alibaba Cloud Toolkit to deploy an application to Serverless App Engine (SAE) and monitor the application.
Prerequisites
SAE is activated. For more information, see Activate SAE.
Maven is downloaded and environment variables are configured.
Java Development Kit (JDK) 1.8 or later is downloaded and installed.
IntelliJ IDEA 2018.3 or later is downloaded and installed.
NoteThe official server of the JetBrains plug-ins is deployed outside China. If you cannot download IntelliJ IDEA due to slow network responses, obtain the offline installation package for IntelliJ IDEA and install it. For more information, see Contact us.
Alibaba Cloud Toolkit is installed and configured in IntelliJ IDEA.
Step 1: Create demo applications in SAE
SAE allows you to deploy applications by using code packages and images. For more information, see Deploy a Java application to SAE 1.0.
In this example, JAR packages are used to create a provider application and a consumer application in SAE. For more information, see Deploy a microservices application by using a JAR package in the SAE console.
Step 2: Create a provider
Create a provider application project in an on-premises environment, add dependencies, enable service registration and discovery, and then specify the Nacos server as the service registry.
Create a Maven project named
nacos-service-provider
.Add dependencies to the
pom.xml
file.The following sample code provides an example on how to add the Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1 dependencies:
<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>
In this example, Spring Cloud Greenwich is used. The version of Spring Cloud Alibaba for Spring Cloud Greenwich is 2.1.1.RELEASE.
The version of Spring Cloud Alibaba for Spring Cloud Finchley is 2.0.1.RELEASE.
The version of Spring Cloud Alibaba for Spring Cloud Edgware is 1.5.1.RELEASE.
NoteSpring Cloud Edgware is discontinued. We recommend that you do not use Spring Cloud Edgware to develop applications.
In
src\main\java
, create a package named com.aliware.edas.In the com.aliware.edas package, create a startup class named
ProviderApplication
for the provider, and add the following sample code:The
@EnableDiscoveryClient
annotation is used to enable the service registration and discovery feature for the provider application.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); } }
In the
com.aliware.edas
package, createEchoController
, and specify/echo/{String}
as the URL mapping and GET as the HTTP method. Obtain the method parameter from the URL path, and echo the received parameter.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; } }
In
src\main\resources
, create a file named application.properties and add the following configuration to application.properties to specify the IP address of Nacos Server.spring.application.name=service-provider server.port=18081 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
In the preceding configuration,
127.0.0.1
indicates the IP address of your Nacos Server. If your Nacos Server is deployed on another server, you must change the value to the IP address of the server.NoteIf your service registry is a self-managed service registry, replace
127.0.0.1
with the address of the self-managed service registry.Verify the result.
Execute the
main
function ofProviderApplication
in thenacos-service-provider
project to start the provider application.Log on to the on-premises Nacos Server console at
http://127.0.0.1:8848/nacos
. The default username and password that can be used to log on to the Nacos Server console are nacos.In the left-side navigation pane, choose .
You can view service-provider in the service list. You can also query the details of the service.
Step 3: Create a consumer
This section describes the service registration feature, and how Nacos Server works with RestTemplate and FeignClient.
Create a Maven project named
nacos-service-consumer
.Add dependencies to the
pom.xml
file.<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>
In
src\main\java
, create a package named com.aliware.edas.In the
com.aliware.edas
package, configure RestTemplate and FeignClient.In the
com.aliware.edas
package, create an interface class namedEchoService
, add the@FeignClient
annotation, and then specify the HTTP URL and HTTP method.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); }
In the
com.aliware.edas
package, create a startup class namedConsumerApplication
and add the related configurations.Add the
@EnableDiscoveryClient
annotation to enable service registration and discovery.Add the
@EnableFeignClients
annotation to enable FeignClient.Add the
@LoadBalanced
annotation to integrate RestTemplate with service discovery.
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); } }
In the
com.aliware.edas
package, create a class namedTestController
to test and validate the service discovery feature.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); } }
In
src\main\resources
, create a file namedapplication.properties
and add the following configuration toapplication.properties
to specify the IP address of Nacos Server.spring.application.name=service-consumer server.port=18082 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
In the preceding configuration,
127.0.0.1
indicates the IP address of your Nacos Server. If your Nacos Server is deployed on another server, you must change the value to the IP address of the server.NoteIf your service registry is a self-managed service registry, replace
127.0.0.1:8848
with the address of the self-managed service registry.Verify the result.
Execute the
main
function ofConsumerApplication
in thenacos-service-consumer
project to start the consumer application.Log on to the on-premises Nacos Server console at
http://127.0.0.1:8848/nacos
. The default username and password that can be used to log on to the Nacos Server console are nacos.In the left-side navigation pane, choose .
You can view service-consumer in the service list. You can also query the details of the service.
Step 4: Test the services in an on-premises environment
Use the consumer to call the provider in an on-premises environment.
For Linux, UNIX, or macOS, run the following commands:
curl http://127.0.0.1:18082/echo-rest/rest-rest curl http://127.0.0.1:18082/echo-feign/feign-rest
On Windows, enter http://127.0.0.1:18082/echo-rest/rest-rest and then http://127.0.0.1:18082/echo-feign/feign-rest in the address bar of a browser and the press Enter key.
In this example, services are tested on Windows.
The preceding figure shows that the microservices provider is called by the microservices consumer in an on-premises environment.
Step 5: Deploy the applications to SAE
After you develop an application, you must configure a deployment task in Alibaba Cloud Toolkit to publish your business code to the application that you created in Step 1: Create demo applications in SAE.
Configure an account in Alibaba Cloud Toolkit.
Click the Alibaba Cloud Toolkit icon and select Preference… from the drop-down list. In the left-side navigation pane of the Settings page, choose .
On the Accounts page, configure the Access Key ID and Access Key Secret parameters, and click OK.
NoteTo obtain the Access Key ID and Access Key Secret, perform the following steps:
On the Accounts page, click Get existing AK/SK to go to the Alibaba Cloud logon page and then log on to Alibaba Cloud. On the Security Management page, obtain the Access Key ID and Access Key Secret.
Configure deployment tasks.
On IntelliJ IDEA, click the Cloud Toolkit icon and select Deploy to SAE from the drop-down list.
On the Deploy to SAE page, configure the parameters. After you configure the settings, click Apply to save the settings.
NoteIf you use a self-managed service registry, you must configure the following startup commands on the Advanced tab:
-Dnacos.use.endpoint.parsing.rule=false
and-Dnacos.use.cloud.namespace.parsing=false
.Configure the settings for the provider application.
In the Application section, specify the region, namespace, and name of the application that you created in Step 1: Create demo applications in SAE.
Configure the settings for the consumer application.
In the Application section, specify the region, namespace, and name of the application that you created in SAE.
Deploy the applications.
Click Run to run the provider application, and then run the consumer application.
Verify the result.
Bind a Server Load Balancer (SLB) instance to the consumer application.
For more information, see Bind an SLB instance to an application.
Access the consumer.
Initiate an HTTP request to the consumer.
curl http://47.111.XX.XX/echo-feign/feign-rest
Initiate an HTTP request to the consumer. The consumer calls the provider.
curl http://47.111.XX.XX/echo-rest/rest-rest
View the call data on the related Application Monitoring dashboards.