原文地址 https://www.infoq.com/news/2018/10/oracle-introduces-helidon
Oracle have introduced a new open-source framework, Project Helidon, a collection of Java libraries designed for creating microservices-based applications that joins Payara Micro, Thorntail (formerly WildFly Swarm), OpenLiberty, and TomEE in the MicroProfile family.
Originally named J4C (Java for Cloud), Helidon was designed to be simple and fast, and is comprised of two versions: Helidon SE and Helidon MP. Helidon SE features three core APIs to create a microservice -- a web server, configuration, and security -- for building microservices-based applications. An application server is not required. Helidon MP supports the MicroProfile 1.1 specification for building microservices-based applications.
Web Server
Inspired by NodeJS and other Java frameworks, Helidon's web server is an asynchronous and reactive API that runs on top of Netty. The [WebServer](https://helidon.io/docs/latest/#/webserver/01_introduction)
interface includes support for configuration, routing, error handling, and building metrics and health endpoints.
The following code example demonstrates how to start a simple Helidon web server to display the text, "_It works!_" on a random available port:
`// starts the server on a random available port 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(); }`
Configuration
The configuration component, [Config](https://helidon.io/docs/latest/#/config/01_introduction)
, loads and processes configuration properties in key/value format. By default, configuration properties will be read from a defined application.properties
or application.yaml
file placed in the /src/main/resources
directory.
The following code example demonstrates how to use Config
and builds upon the previous example by reading an applications.yaml
file to specify a port on which to start the web server.
`// application.yaml server: port: 8080 host: 0.0.0.0 // starts the server on a port defined in 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
The [Security](https://helidon.io/docs/latest/#/security/01_introduction)
class provides support for authentication, authorization, and audit. A number of security providers for use in Helidon applications have been implemented. There are three ways security may be built-in to a Helidon application: from a builder; by configuration; or a hybrid of the first two.
The following code example demonstrates how to build an instance of Security
, use Config
to obtain user authentication (with encrypted password), and display the server time.
`// 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");`
More comprehensive security examples may be found on GitHub.
Helidon Architecture
The relationship between Helidon SE and Helidon MP is shown in the architecture diagram.
The chart below shows where Helidon SE and Helidon MP fit into the categories of microservices frameworks.
Getting Started
Helidon provides quick start examples to demonstrate the differences between Helidon SE and Helidon MP.
The following Maven and Java commands will generate and package the Helidon SE example to create a REST service using Helidon's web server.
`$ 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`
The following Maven and Java commands will generate and package the Helidon MP example to create a REST service using MicroProfile's JAX-RS API.
`$ 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`
Once the server is running, the following commands can be executed:
The entire Helidon project may be found on GitHub.
Dmitry Kornilov, senior software development manager at Oracle, spoke to infoQ about this new project.
InfoQ: What inspired Oracle to develop this new microservices framework?
Dmitry Kornilov: The work on Helidon started some time ago. When microservices architecture started to become very popular for creating cloud services the development experience also needed to change. Java EE is a stable technology but it has a lot of legacy code. Instead of building microservices on top of Java EE, we realized that we needed a new framework which is designed to build microservices from the scratch. That's how Helidon was born.
InfoQ: What makes Helidon unique over the other MicroProfile implementations such as OpenLiberty, Thorntail, Payara Micro and TomEE?
Kornilov: Helidon is not just a MicroProfile implementation. It comes in two flavors: Helidon SE and Helidon MP.
Helidon SE forms the core of Helidon. It's a lightweight set of libraries that could be used separately from each other, but when used together provide the foundation a developer needs to create a microservice: configuration, security, and a web server. It brings a more modern and reactive approach that developers like. We try to make it very clear: no injection'magic'is used, which makes a Helidon SE application easy to debug. There's no special jar format, no special classloaders. Your application is just a vanilla Java SE application. That also means it's compatible with all IDEs with no special plugins required.
Helidon MP is our MicroProfile implementation and it's built on Helidon SE -- it is not derived from an application server. So there's no deployment model, no Java EE packaging, no extra stuff you don't need.
InfoQ: Why was the MicroProfile 1.1 specification implemented instead of a more recent version?
Kornilov: Helidon development started some time ago and we decided to stick to the MicroProfile version which was latest at that time. We are continuously improving Helidon and support for new MicroProfile versions is coming soon.
InfoQ: What's on the horizon for Helidon especially in terms of support for Jakarta EE and more recent versions of the MicroProfile specification?
Kornilov: I already mentioned that we are working on more recent MicroProfile versions support. When new Jakarta EE specifications become visible we will participate in their development and support them in Helidon. Also, we plan to add Oracle Cloud Integration features to Helidon, HTTP client support, a project starter web app, and constantly improving our examples and documentation.
Resources
- Helidon Takes Flight by Dmitry Kornilov (September 7, 2018)
- Oracle Releases New Java Microservices Framework by John Waters (September 10, 2018)
- Microservices From Dev to Deploy, Part 1: Getting Started with Helidon by Todd Sharp (October 3, 2018)