• 周六. 10 月 5th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

First experience of serverless: rapid development and deployment of a Hello World (Java version)

King Wang

1 月 3, 2022

Yesterday, I was attracted by the cool big screen of Alibaba cloud !

I’ve been waiting 85 There are so few post developers ! I’m curious what the hell is 90、95 After playing ? I took a deep look at .

This is one about Serverless Experience activities of ,Serverless In China, it has always been in a state of tepid and tepid , So many developers don’t really understand Serverless In the end is what !

If you have this curiosity , Then you can experience it just like me , Write a simple Hello World, Have a most intuitive feeling of this product full of future sense !

P.S. Participate in activities , Join the nail group and draw once a day , Have a chance to win AirPods、 the height is PG Model ( I like )、Cherry Mechanical keyboard and other rich gifts !

How to participate

-【 official 】 Activity home page :https://developer.aliyun.com/topic/yiqi/hol?utm_content=g_1000180354 -【 official 】 Customs clearance book :https://help.aliyun.com/document_detail/181573.html

Official documents, like other Alibaba projects, need to be improved (lan)….

If you have enough confidence in your own ability to explore , You can try it directly through official documents .

If you want to step on the pit less , You can see what I wrote below Super detailed customs clearance book , To complete a Java Version of Hello World!

First step : Enter the product list , I’m gonna go ahead and create an app

The second step : choice “ Empty application ” label

  • Serverless Application server selection :“ Function calculation FC”
  • Develop language options :Java

The third step : Fill in the application name 、 Application Introduction , Just define yourself

Step four : Click… In the new application card “ Development and deployment ” Button

Step five : newly build helloworld.java

The contents are as follows :

package com.alibaba.serverless.helloworld;
import java.io.UnsupportedEncodingException;
import java.util.UUID;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.FunctionComputeLogger;
import com.aliyun.fc.runtime.FunctionInitializer;
import com.aliyun.fc.runtime.PojoRequestHandler;
import com.aliyuncs.utils.Base64Helper;
/**
* Corresponds to the function name in the deployment route :
* com.alibaba.serverless.helloworld.helloworld::handleRequest
*/
public class helloworld implements FunctionInitializer, PojoRequestHandler<ApiGwRequest, ApiGwResponse> {
/*
* The callback function when the cloud function is initialized .
*/
@Override
public void initialize(Context context) {
FunctionComputeLogger logger = context.getLogger();
logger.debug(String.format("RequestID is %s %n", context.getRequestId()));
}
/*
* Web Cloud function processing entry , Every time the entry is called .
* @param request All the information requested is in request in
*/
@Override
public ApiGwResponse handleRequest(ApiGwRequest request, Context context) {
FunctionComputeLogger logger = context.getLogger();
// Read request object
String requestPath = request.getPath();
String bodyContext = "";
if (requestPath.startsWith("")) {
bodyContext = " Hello , The world !";
}
// Building return objects
ApiGwResponse response = new ApiGwResponse();
response.getHeaders().put("responseHeader", "testValue");
response.getHeaders().put("Content-type", "text/html; charset=utf-8");
boolean isBase = false;
if ( isBase ) {
try {
bodyContext = Base64Helper.encode(bodyContext, "UTF-8");
} catch (UnsupportedEncodingException e) {
isBase = false;
}
}
response.setBody( bodyContext );
response.setBase64Encoded( isBase );
response.setStatusCode(200);
logger.info("Response return :" + bodyContext );
return response;
}
}

Step six : Follow the diagram below , Submission code

Step seven : Deploy to everyday environments

Click the first icon on the left toolbar , Enter the deployment interface , Click on “ Add a route ”.

In the figure 2 Part of the content is : com.alibaba.serverless.helloworld.helloworld::handleRequest, The corresponding is created before helloworld.java Medium handleRequest Law , It means /helloworld The request will be sent to the handleRequest To deal with it .

Be careful , The request method should be set to POST, I’ll tell you why !

Step eight : test /helloworld Interface

Next to the deployment tab is a test tool , Enter the user path /helloworld, Click test to initiate the request , The results are shown in the figure below :

Looking back at the code , Right body Content :

Does this testing tool always feel a little strange ?! Yes , There is no such thing as HTTP Request Method The choice of , This request is used by default POST It’s coming , So in the last step, we configured /helloworld by POST request , Otherwise it won’t work here . Look at the official book of brothers , Did you find this hole ?

How about ? Your first time Serverless Did the trip go well ?

Click here to try out this future development approach

First article :Serverless First experience : Quickly develop and deploy a Hello World(Java edition ), Reprint please indicate the source . Welcome to my official account. : Program the ape DD, Get exclusive learning resources and daily dry goods push .

发表回复