近日,Oracle 推出 了一個新的開源框架 Helidon ,該項目是一個用於建立基於微服務的應用程序的Java庫集合。和 Payara Micro 、 Thorntail (以前的 WildFly Swarm )、 OpenLiberty 、TomEE 等項目同樣,該項目也加入了MicroProfile家族。java
Helidon最初被命名爲J4C(Java for Cloud),其設計以簡單、快速爲目標,它包括兩個版本:Helidon SE 和 Helidon MP 。Helidon SE提供了建立微服務的三個核心API:Web服務器、配置和安全,用於構建基於微服務的應用程序,不須要應用服務器。Helidon MP支持用於構建基於微服務的應用程序的MicroProfile 1.1規範。git
受NodeJS和其餘Java框架的啓發,Helidon的Web服務器是一個異步、反應性API,運行在 Netty 之上。 WebServer 接口包括對配置、路由、錯誤處理以及構建 度量和健康 端點的支持。github
下面的示例代碼演示瞭如何啓動一個簡單的Helidon Web服務器,在一個隨機可用的端口上顯示「 It works! 」: web
// 在一個隨機可用的端口上啓動服務器 public void startWebServerUsingRandomPort() throws Exception { WebServer webServer = WebServer .create(Routing.builder() .any((req,res) -> res.send("It works!" + "\n")) .build()) .start() .toCompletableFuture() .get(10,TimeUnit.SECONDS); System.out.println("Server started at: http://localhost:" + webServer.port() + "\n"); webServer.shutdown().toCompletableFuture(); }
配置組件 Config 加載和處理鍵/值格式的配置屬性。在默認狀況下,配置屬性將從定義好的 application.properties 或 application.yaml 文件中讀取,它們位於 /src/main/resources 目錄下。apache
下面的示例代碼基於前面的例子構建,它演示瞭如何使用 Config ,經過讀取 applications.yaml 文件得到指定的端口啓動Web服務器。 安全
// application.yaml server: port: 8080 host: 0.0.0.0 // 在application.yaml預約義的端口上啓動服務器 public void startWebServerUsingDefinedPort() throws Exception { Config config = Config.create(); ServerConfiguration serverConfig = ServerConfiguration.fromConfig(config.get("server")); WebServer webServer = WebServer .create(serverConfig,Routing.builder() .any((req,res) -> res.send("It works!" + "\n")) .build()) .start() .toCompletableFuture() .get(10,TimeUnit.SECONDS); System.out.println("Server started at: http://localhost:" + webServer.port() + "\n"); webServer.shutdown().toCompletableFuture(); }
類 Security 爲身份驗證、受權和審計提供支持。已經有許多用於Helidon應用程序的 安全提供程序 實現。有三種方法能夠將安全性內置到Helidon應用程序中:從構建器、經過配置或者是前二者的結合。性能優化
下面的示例代碼演示瞭如何構建 Security 實例、使用 Config 獲取用戶身份驗證(使用加密密碼)並顯示服務器時間。服務器
// application.yaml http-basic-auth: users: login: "mpredli" password: "${CLEAR=somePassword}" roles: ["user","admin"] Config config = Config.create(); Security security = Security.builder() .config(config) .addProvider(...) .build(); String user = config.get("http-basic-auth.users.login").asString(); String password = config.get("http-basic-auth.users.password").asString(); System.out.println("\n"); System.out.println("INFO: user = " + user); System.out.println("INFO: password = " + password); SecurityTime time = SecurityTime.builder().build(); time = security.getServerTime(); System.out.println("INFO: server time = " + time.toString()); System.out.println("\n");
GitHub架構
提供了更詳盡的安全示例。併發
下面的架構圖顯示了Helidon SE和Helidon MP的關係。
下圖說明了Helidon SE和Helidon MP所屬的微服務框架類別。
Helidon提供了 快速入門示例 來演示Helidon SE和Helidon MP之間的區別。
下面的Maven和Java命令將生成並打包Helidon SE示例,使用Helidon的Web服務器建立一個REST服務。
$ mvn archetype:generate -DinteractiveMode=false \ -DarchetypeGroupId=io.helidon.archetypes \ -DarchetypeArtifactId=helidon-quickstart-se \ -DarchetypeVersion=0.10.1 \ -DgroupId=io.helidon.examples \ -DartifactId=quickstart-se \ -Dpackage=io.helidon.examples.quickstart.se $ cd quickstart-se $ mvn package $ java -jar target/quickstart-se.jar
下面的Maven和Java命令將生成並打包Helidon MP示例,使用MicroProfile的JAX-RS API建立一個REST服務。
$ mvn archetype:generate -DinteractiveMode=false \ -DarchetypeGroupId=io.helidon.archetypes \ -DarchetypeArtifactId=helidon-quickstart-mp \ -DarchetypeVersion=0.10.1 \ -DgroupId=io.helidon.examples \ -DartifactId=quickstart-mp \ -Dpackage=io.helidon.examples.quickstart.mp $ cd quickstart-mp $ mvn package $ java -jar target/quickstart-mp.jar
一旦服務器開始運行,就能夠執行下面的命令:
在 GitHub 上能夠找到整個Helidon項目。
Oracle的高級軟件開發經理 Dmitry Kornilov 向infoQ介紹了這個新項目。
在此我向你們推薦一個架構學習交流君羊。交流學習君羊:821169538 裏面會分享一些資深架構師錄製的視頻錄像:有Spring,MyBatis,Netty源碼分析,高併發、高性能、分佈式、微服務架構的原理,JVM性能優化、分佈式架構等這些成爲架構師必備的知識體系。還能領取免費的學習資源,目前受益良多。
Dmitry Kornilov:有關Helidon的工做已經開始一段時間了。當建立雲服務的微服務體系結構開始變得很是流行時,開發體驗也須要改變。Java EE是一種穩定的技術,可是它有不少遺留代碼。咱們沒有在Java EE上構建微服務,咱們意識到,咱們須要一個從頭開始設計的構建微服務的新框架。Helidon就是這樣出現的。
InfoQ:與OpenLiberty、Thorntail、Payara Micro和TomEE等其餘MicroProfile實現相比,Helidon有什麼獨特之處?
Kornilov:Helidon不單單是一個MicroProfile實現。它有兩個版本:Helidon SE和Helidon MP。
Helidon SE構成了Helidon的核心。它是一組輕量級的庫,其中的庫能夠單獨使用,但若是一塊兒使用,就能夠知足開發人員建立微服務的基本需求:配置、安全和Web服務器。它帶來了一種開發人員喜歡的更現代的反應性方法。咱們老是盡力明確:不使用注入「魔法」,使Helidon SE應用程序易於調試。沒有特殊的jar格式,沒有特殊的類加載器。你的應用程序只是一個普通的Java SE應用程序。這也意味着,它與全部IDE兼容,不須要特殊的插件。
Helidon MP是咱們的MicroProfile實現,它以Helidon SE爲基礎構建——它不是派生自某個應用服務器。所以,沒有部署模型,沒有Java EE打包,沒有你不須要的額外的東西。
Kornilov:Helidon的開發在一段時間以前就開始了,咱們決定堅持使用當時最新的MicroProfile版本。咱們正在不斷地改進Helidon,對新的MicroProfile版本的支持很快就會到來。
InfoQ:接下來,尤爲是在Jakarta EE支持和MicroProfile規範較新版本的支持方面,Helidon將開展哪些工做?
Kornilov:我已經提到過,咱們正致力於對MicroProfile較新版本的支持。當新的Jakarta EE 規範出現時,咱們將參與它們的開發並在Helidon中支持它們。此外,咱們計劃向Helidon添加Oracle Cloud集成特性、HTTP客戶端支持、項目啓動器Web應用,並不斷改進咱們的示例和文檔。
出處:https://mp.toutiao.com/profile_v3/graphic/articles