序文
Ballerinaは、完全にオープンソースのコンパイル時の強く型付けされた言語です。ビジョンは、クラウドネイティブ時代のプログラマーが必要なソフトウェアを簡単に作成できるようにすることです。
オープンソースアドレス:https://github.com/ballerina-platform/ballerina -lang
例
対応するプラットフォームのパッケージをダウンロードしてインストールします
https://ballerina.io/downloads/
ここで使用するインストールはubuntu環境で、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 Main
- バレリーナディレクトリを作成し、バレリーナディレクトリに入ります。この手順は必須ではありませんが、ファイルを整理するために、これを行うことをお勧めします
lan@lan-machine:/disk/note$ mkdir ballerina && cd ballerina
lan@lan-machine:/disk/note/ballerina$
- hello-world.balファイルを作成し、hello-worldprintに対応するコードを記述します
lan@lan-machine:/disk/note/ballerina$ touch hello-world.bal
import ballerina/io;
public function main() {
io:println("Hello, World!");
}
- 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 Service
2番目のhello-worldの例は、ポート9090でリッスンしているhttpサーバーを起動することです。
- 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);
}
}
}
- 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!
パスの構成はこんな感じ、
- http:Listener(9090)は、localhost:9090であるリスニングポート9090を構成します。
- servicehelloが最初のパスを構成します
- リソース関数sayHelloは2番目のパスを構成します
完全な構成の後はhttp:// localhost:9090 / hello / sayHelloです
Hello World Paraller
3番目のhello-worldの例は、非同期タスクの実行です。
- 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");
}
}
- 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
印刷の順序が毎回同じではないことがわかります
HelloWorldクライアント
最後のhello-worldの例は、httpクライアントリクエストです。
- 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());
}
}
- 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
こんにちは世界
例はリクエストでhttp://www.mocky.io/v2/5ae082123200006b00510c3d/
あり、結果を出力します
例としては、より公式なWebサイトがあります。
4つのコーヒー豆のリリースで始まります!
パブリックアカウントをフォロー-> [Four CoffeeBeans]最新のコンテンツを入手