클라우드 네이티브 프로그래밍 언어 발레리나 : Hello-World

머리말

Ballerina는 완전한 오픈 소스 컴파일 타임의 강력한 형식 언어입니다. 비전은 클라우드 네이티브 시대의 프로그래머가 원하는 소프트웨어를 쉽게 작성할 수 있도록하는 것입니다.
오픈 소스 주소 : https://github.com/ballerina-platform/ballerina -lang

설치할 해당 플랫폼의 패키지를 다운로드하십시오.

https://ballerina.io/downloads/

다음은 우분투 환경에서의 설치입니다. deb 패키지를 다운로드 한 후 설치하십시오.

lan@lan-machine:~$ sudo dpkg -i ballerina-linux-installer-x64-1.1.0.deb 
[sudo] password for lan: 
Selecting previously unselected package ballerina-1.1.0.
(Reading database ... 187196 files and directories currently installed.)
Preparing to unpack ballerina-linux-installer-x64-1.1.0.deb ...
Unpacking ballerina-1.1.0 (1.1.0) ...
Setting up ballerina-1.1.0 (1.1.0) ...
lan@lan-machine:~$ ballerina version
jBallerina 1.1.0
Language specification 2019R3
Ballerina tool 0.8.0

설치된 발레리나 버전이 1.1.0임을 알 수 있습니다.

Hello World 메인

  1. 발레리나 디렉토리를 생성하고 발레리나 디렉토리를 입력합니다.이 단계는 필요하지 않지만 파일 구성의 경우이 작업을 수행하는 것이 좋습니다.
lan@lan-machine:/disk/note$ mkdir ballerina && cd ballerina
lan@lan-machine:/disk/note/ballerina$
  1. hello-world.bal 파일을 만들고 hello-world 인쇄에 해당하는 코드를 작성합니다.
    lan@lan-machine:/disk/note/ballerina$ touch hello-world.bal
import ballerina/io;
public function main() {
    io:println("Hello, World!");
}
  1. Hello-world 실행
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world.bal 
Compiling source
        hello-world.bal

Generating executables
Running executables

Hello, World!  

보시다시피 Hello World가 성공적으로 입력되었으며 가장 간단한 예제가 완료되었습니다.

Hello World 서비스

두 번째 hello-world 예제는 포트 9090에서 수신하는 http 서버를 시작하는 것입니다.

  1. hello-world-service.bal 파일을 만들고 해당 코드를 작성합니다.

lan@lan-machine:/disk/note/ballerina$ touch hello-world-service.bal

import ballerina/http;
import ballerina/log;
service hello on new http:Listener(9090) {

    resource function sayHello(http:Caller caller, http:Request req) {

        var result = caller->respond("Hello, World!");

        if (result is error) {
            log:printError("Error sending response", result);
        }
    }
}
  1. hello-world-service.bal 실행
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-service.bal
Compiling source
        hello-world-service.bal

Generating executables
Running executables

[ballerina/http] started HTTP/WS listener 0.0.0.0:9090

lan@lan-machine:~$ curl http://localhost:9090/hello/sayHello
Hello, World!

경로의 구성은 다음과 같습니다.

  1. http : Listener (9090)는 localhost : 9090 인 수신 대기 포트 9090을 구성합니다.
  2. 서비스 hello는 첫 번째 경로를 구성합니다.
  3. 리소스 함수 sayHello는 두 번째 경로를 구성합니다.

완성 된 구성은 http : // localhost : 9090 / hello / sayHello입니다.

Hello World Paraller

세 번째 hello-world 예제는 비동기 작업의 실행입니다.

  1. hello-world-paraller.bal 파일을 만들고 해당 코드를 작성합니다.
    lan@lan-machine:/disk/note/ballerina$ touch hello-world-paraller.bal
import ballerina/io;
public function main() {
    worker w1 {
        io:println("Hello, World! #m");
    }

    worker w2 {
        io:println("Hello, World! #n");
    }
    worker w3 {
        io:println("Hello, World! #k");
    }
}
  1. hello-world-paraller.bal 파일 실행
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-paraller.bal
Compiling source
        hello-world-paraller.bal

Generating executables
Running executables

Hello, World! #m
Hello, World! #n
Hello, World! #k
lan@lan-machine:/disk/note/ballerina$ 
lan@lan-machine:/disk/note/ballerina$ 
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-paraller.bal
Compiling source
        hello-world-paraller.bal

Generating executables
Running executables

Hello, World! #n
Hello, World! #m
Hello, World! #k

매번 인쇄 순서가 동일하지 않음을 알 수 있습니다.

Hello World 클라이언트

마지막 hello-world 예제는 http 클라이언트 요청입니다.

  1. hello-world-client.bar 파일을 만들고 해당 코드를 작성합니다.
    lan@lan-machine:/disk/note/ballerina$ touch hello-world-client.bal
import ballerina/http;
import ballerina/io;
public function main() {
    http:Client clientEP = new ("http://www.mocky.io");

    var resp = clientEP->get("/v2/5ae082123200006b00510c3d/");

    if (resp is http:Response) {
        var payload = resp.getTextPayload();
        if (payload is string) {

            io:println(payload);
        } else {

            io:println(payload.detail());
        }
    } else {

        io:println(resp.detail());
    }
}
  1. hello-world-client.bal 파일 실행
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-client.bal
Compiling source
        hello-world-client.bal

Generating executables
Running executables

Hello World

예는 요청 http://www.mocky.io/v2/5ae082123200006b00510c3d/및 결과 인쇄입니다.

더 많은 공식 웹 사이트를 찾을 수 있습니다.

원산지 네 개의 커피 콩 출시!
공개 계정 팔로우-> [Four Coffee Beans] 최신 콘텐츠 받기

추천

출처blog.csdn.net/lypgcs/article/details/104280493