首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Oracle推出轻量级Java微服务框架Helidon

近日,Oracle推出了一个新的开源框架Helidon,该项目是一个用于创建基于微服务的应用程序的Java库集合。和Payara MicroThorntail(之前的WildFly Swarm)、OpenLibertyTomEE等项目一样,该项目也加入了MicroProfile家族。

Helidon最初被命名为J4C(Java for Cloud),其设计以简单、快速为目标,它包括两个版本:Helidon SEHelidon MP。Helidon SE提供了创建微服务的三个核心API:Web服务器、配置和安全,用于构建基于微服务的应用程序,不需要应用服务器。Helidon MP支持用于构建基于微服务的应用程序的MicroProfile 1.1规范。

Web服务器

受NodeJS和其他Java框架的启发,Helidon的Web服务器是一个异步、反应性API,运行在Netty之上。WebServer接口包括对配置、路由、错误处理以及构建度量和健康端点的支持。

下面的示例代码演示了如何启动一个简单的Helidon Web服务器,在一个随机可用的端口上显示“It works!”: ?

代码语言:javascript
复制
// 在一个随机可用的端口上启动服务器
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.propertiesapplication.yaml文件中读取,它们位于/src/main/resources目录下。

下面的示例代码基于前面的例子构建,它演示了如何使用Config,通过读取applications.yaml文件获得指定的端口启动Web服务器。 ?

代码语言:javascript
复制
// 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获取用户身份验证(使用加密密码)并显示服务器时间。

代码语言:javascript
复制
// 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的架构

下面的架构图显示了Helidon SE和Helidon MP的关系。

下图说明了Helidon SE和Helidon MP所属的微服务框架类别。

入门指南

Helidon提供了快速入门示例来演示Helidon SE和Helidon MP之间的区别。

下面的Maven和Java命令将生成并打包Helidon SE示例,使用Helidon的Web服务器创建一个REST服务。

代码语言:javascript
复制
$ 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服务。

代码语言:javascript
复制
$ 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介绍了这个新项目。

InfoQ:是什么给了甲骨文开发这个新微服务框架的启发?

InfoQ:与OpenLiberty、Thorntail、Payara Micro和TomEE等其他MicroProfile实现相比,Helidon有什么独特之处?

InfoQ:为什么实现的是MicroProfile 1.1规范,而不是一个更新的版本?

InfoQ:接下来,尤其是在Jakarta EE支持和MicroProfile规范较新版本的支持方面,Helidon将开展哪些工作?

相关资源

查看英文原文:Oracle Introduces Helidon - A Lightweight Java Microservices Framework

  • 发表于:
  • 本文为 InfoQ 中文站特供稿件
  • 首发地址http://www.infoq.com/cn/news/2018/10/oracle-introduces-helidon
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券
http://www.vxiaotou.com