Cloud native programming language ballerina:hello-world

Preface

Ballerina is a fully open source compile-time strongly typed language. The vision is to allow programmers in the cloud-native era to easily write the software they want.
Open source address: https://github.com/ballerina-platform/ballerina-lang

Example

Download the package of the corresponding platform to install

https://ballerina.io/downloads/

Here is the installation in the ubuntu environment, after downloading the deb package, install it

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

You can see that the version of ballerina installed is 1.1.0

Hello World Main

  1. Create the ballerina directory and enter the ballerina directory, this step is not necessary, but for file organization, it is recommended to do this
lan@lan-machine:/disk/note$ mkdir ballerina && cd ballerina
lan@lan-machine:/disk/note/ballerina$
  1. Create a hello-world.bal file and write the corresponding code for hello-world print
    lan@lan-machine:/disk/note/ballerina$ touch hello-world.bal
import ballerina/io;
public function main() {
    io:println("Hello, World!");
}
  1. Run hello-world
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world.bal 
Compiling source
        hello-world.bal

Generating executables
Running executables

Hello, World!  

As you can see, Hello World has been successfully entered, and the simplest example is completed.

Hello World Service

The second hello-world example is to start an http server listening on port 9090

  1. Create the hello-world-service.bal file and write the corresponding code

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. Run 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!

The composition of the path is like this,

  1. http:Listener(9090) constitutes the listening port 9090, which is localhost:9090
  2. service hello constitutes the first path
  3. resource function sayHello constitutes the second path

After the complete composition is http://localhost:9090/hello/sayHello

Hello World Paraller

The third hello-world example is the execution of asynchronous tasks

  1. Create the hello-world-paraller.bal file and write the corresponding code
    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. Run the hello-world-paraller.bal file
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

You can see that the order of printing is not the same each time

Hello World Client

The last hello-world example is an http client request

  1. Create the hello-world-client.bar file and write the corresponding code
    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. Run the hello-world-client.bal file
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-client.bal
Compiling source
        hello-world-client.bal

Generating executables
Running executables

Hello World

Example is a request http://www.mocky.io/v2/5ae082123200006b00510c3d/and print the result

The example can be more official website found

Originating in four coffee beans release!
Follow the public account -> [Four Coffee Beans] Get the latest content

Guess you like

Origin blog.csdn.net/lypgcs/article/details/104280493