graalvm字节码到位码

GraalVM & Micronauts — frenzy way to MicroServices, Serverless: Part1

GraalVM和Micronauts-疯狂的微服务方式,无服务器:第1部分

Early adopters for Cloud-Native ( microservices, serverless ) are now moving to its next wave called v2.x. leveraging the maturity, learnings, and identified shortfalls to design next-level stuff.

Cloud-Native的早期采用者(微服务,无服务器)现在正在向下一波v2.x迈进。 利用成熟度,学习经验和发现的不足来设计下一级产品。

Let’s recap few purposes of going cloud-native that we will relate here:

让我们来回顾一下我们将在这里涉及到云原生的一些目的:

  • Scalability

    可扩展性
  • Cost — Memory vs Runtime

    成本—内存与运行时间
  • Start-up & execution time ( cold start in Serverless )

    启动和执行时间(在Serverless中冷启动)
  • Throughput

    通量
  • Enable polyglot

    启用多语言

The current era of microservices is dominated by Java + other JVM based languages by large. Beside JVM’s super-rich ecosystem of libraries, frameworks, community support etc. few observation like thick memory footprint, cold startup & extended execution time, less polyglot support factored engineers for moving away from JVM platforms towards non-JVM stuff like nodeJs, Go-Lang, Python for cloud-native ecosystem.

微服务的当前时代主要由Java +其他基于JVM的语言所主导。 除了JVM的库,框架,社区支持等超级丰富的生态系统外,很少观察到诸如内存占用量大,冷启动和执行时间延长等问题,较少的多语言支持使工程师从JVM平台转向非JVM之类的东西,例如nodeJs,Go- Lang,适用于云原生生态系统的Python。

GraalVM — An Oracle Labs initiative in collaboration with open source community supported by (VMWare, Red Hat, Microsoft, Twitter, Amazon and more.). To quote from their website :

GraalVM —与开放源码社区合作的Oracle Labs计划,得到了(VMWare,Red Hat,Microsoft,Twitter,Amazon等)的支持。 要引用他们的网站:

It is a new universal virtual machine that supports a polyglot runtime environment and the ability to compile Java applications down to native machine code. GraalVM removes the isolation between programming languages and enables interoperability in a shared runtime.

它是一种新的通用虚拟机,它支持多语言运行时环境,并且能够将Java应用程序编译为本地机器代码。 GraalVM消除了编程语言之间的隔离,并在共享的运行时中实现了互操作性。

Image for post
Oracle Labs White Paper Oracle Labs白皮书
Image for post
Pratik Prakash Pratik Prakash

为什么社区对GraalVM充满热情? (Why the community is enthusiastic about GraalVM?)

“GraalVM — Byte code to Bit Code” 😀😀

“ GraalVM-字节码到位码”😀😀

  1. GraalVM — Accelerating Application Performance :

    GraalVM —加速应用程序性能:

    GraalVM a modern JVM build on Oracle Java SE can improve the performance of any application written in Java or JVM without any single line change in existing code. It reduces the response time of end-user that in turn free up CPU, memory consumption and can be better utilized for more capacity and load handling.

    GraalVM是基于Oracle Java SE构建的现代JVM,可以提高用Java或JVM编写的任何应用程序的性能,而无需对现有代码进行任何单行更改。 它减少了最终用户的响应时间,从而减少了CPU,内存的消耗,并且可以更好地用于更大的容量和负载处理。

  2. GraalVM — Highly Optimized Compiler

    GraalVM —高度优化的编译器

    The GraalVM compiler is highly optimized to enhance the pre-written application or the new one with its 62 separate compiler optimization algorithms (called “Phases”), of which 27 are patented. Many JVM languages (e.g., Java, Scala, Kotlin) will see around a 30%–40% speedup using GraalVM when compared with Java SE-8.

    GraalVM编译器经过高度优化,以增强其预先编写的应用程序或新应用程序,它具有62种独立的编译器优化算法(称为“阶段”),其中27种已获得专利。 与Java SE-8相比,使用GraalVM可以使许多JVM语言(例如Java,Scala,Kotlin)的速度提高30%至40%。

Image for post
Oracle White Paper Oracle白皮书

3. GraalVM — “Native imaging”GraalVM “Native imaging” allows ahead of time compilation of Java code to a standalone executable called “native image”. It includes the application classes, classes from its dependencies, runtime library, classes from JDK and statically linked native code from JDK by using AoT (ahead-of-time compiler) for a specific operating system and architecture directly. It does not run on the Java VM, but includes necessary components like memory management and thread scheduling from a different virtual machine called “Substrate VM”.

3. GraalVM-“本机映像” GraalVM“本机映像”允许提前将Java代码编译为称为“本机映像”的独立可执行文件。 它包括直接针对特定操作系统和体系结构的应用程序类,来自其依赖项的类,运行时库,来自JDK的类以及来自JDK的静态链接本机代码(通过使用AoT(提前编译器))。 它不是在Java VM上运行,而是包括必要的组件,例如内存管理和来自称为“底物VM”的其他虚拟机的线程调度。

GraalVM Native image supports JVM-based languages, e.g. Java, Scala, Kotlin etc.GraalVM native binary executables have incredibly fast load and execution time(since program initialisation can be done at build time and the application is already compiled) and much smaller footprint (since the JVM and JIT compiler don’t have to be distributed).

GraalVM本机映像支持基于JVM的语言,例如Java,Scala,Kotlin等。GraalVM本机二进制可执行文件具有难以置信的快速加载和执行时间(因为程序初始化可以在构建时完成,并且应用程序已经编译)并且占用空间小得多(因为不需要分发JVM和JIT编译器)。

This is especially important during a cold start (e.g. serverless on demand start-up) as there is really no time to waste waiting for Spring Boot, JRE to configure itself. In this case, the system would already face a considerable backlog of events to process.In case of GraalVM, it needs to know in advance which bytecode is going to be executed. Features like runtime dependency injection (on which Spring Boot relies heavily) will not work out of the box. Instead, a framework like Micronaut (which uses compile time dependency injection, proxy objects etc.) can be leveraged. In addition, the time it takes to create a native image is quite considerable. A typical compilation to a native image might take several minutes, so this step is one you would like to do e.g. during a nightly build.

这在冷启动(例如,无服务器按需启动)期间尤其重要,因为实际上没有时间浪费时间等待Spring Boot,JRE进行自身配置。 在这种情况下,系统将已经面临大量待处理事件。对于GraalVM,它需要事先知道将要执行哪个字节码。 运行时依赖项注入(Spring Boot高度依赖)之类的功能将无法立即使用。 相反, 可以利用 Micronaut 这样 的框架 (使用编译时依赖项注入,代理对象等)。 此外,创建本地映像所花费的时间也相当可观。 对本机映像进行典型的编译可能需要几分钟,因此此步骤是您希望执行的步骤,例如在每晚构建期间。

export GRAALVM_HOME=
export PATH=$GRAALVM_HOME/bin:$PATH
# gu is the Graal Updater, it is used for installing additional packages,in this case the package for building native images
gu install native-image
# mn- Micronauts framework to leverage Native-Image
native-image --no-server -cp target/demo-mn-0.1.jar

One can build a native image directly with Maven using the mvn packagecommand without running the native-image plugin.

可以使用 mvn package 命令 直接使用Maven构建本机映像, 而无需运行本 native-image 插件。

<plugin>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>${graalvm.version}</version>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<skip>false</skip>
<imageName>example</imageName>
<buildArgs>
--no-fallback
</buildArgs>
</configuration>
</plugin>

4. Substrate VM — a subproject within Graal (VM) “Native-Image” code does not run on Java VM, but includes necessary components like memory management and thread scheduling from a different virtual machine, called “Substrate VM”. It is a native runtime component (like the deoptimizer, garbage collector, thread scheduling etc.). The resulting program has faster startup time and lowers runtime memory overhead compared to a Java VM.

4. Substrate VM-Graal(VM) “本机映像”代码中的子项目不在Java VM上运行,但包含必要的组件,例如内存管理和来自不同虚拟机(称为“ Substrate VM”)的线程调度。 它是本机运行时组件(如反优化器,垃圾收集器,线程调度等)。 与Java VM相比,生成的程序具有更快的启动时间并降低了运行时内存开销。

Image for post
Oracle White Paper Oracle白皮书

5. GraalVM’s — ( Ahead of Time Compiler) AoT vs OpenJDK’s JITThis just-in-time compilation understands the workload over time and optimize the final code depending on its nature. That’s the reason why over time the JVM is considered more performant than native executables doing the same.Features like reflection and proxies, heavily used by dependency injection centered frameworks such as Spring, pose a serious challenge for AoT compilation.

5. GraalVM的-(提前于时间编译器)AoT与OpenJDK的JIT即时编译可以理解随时间推移的工作负载,并根据其性质优化最终代码。 这就是为什么随着时间的流逝,JVM被认为比本机可执行文件性能更高的原因,诸如反射和代理之类的功能在以依赖注入为中心的框架(如Spring)中大量使用,这对AoT编译提出了严峻的挑战。

Hotspot JVM JIT is like a Diesel engine vs Petrol engine (AoT)

热点JVM JIT就像柴油引擎与汽油引擎(AoT)

Those relationships require to be manually declared in configuration to allow the compiler know about them — you guessed it — ahead of time.Recently, two new JVM frameworks based onGraalVM with this approach in mind: Micronaut and Quarkus.

这些关系需要在配置中手动声明,以使编译器提前知道它们(您猜对了)。最近,牢记此方法的两个基于GraalVM的新JVM框架: Micronaut Quarkus

Image for post

6. GraalVM — MicroService & Serverless — for CloudNaitve EcoSystemMicroservices and serverless architectures are being implemented or are part of the roadmap in most modern solution stacks. Given that Java is still the dominant language for business applications, the need for reducing the startup time for Java is becoming more important. Serverless architectures are one such area that needs faster startup time along with applications hosted on the container platform.Several projects have accepted GraalVM Native Image as a platform for their applications like Quarkus, Micronaut. Spring Framework recently announced the upcoming support for native images.

6.用于CloudNaitve生态系统的GraalVM —微服务和无服务器-微服务和无服务器架构正在实施,或已成为大多数现代解决方案堆栈中路线图的一部分。 鉴于Java仍然是业务应用程序的主要语言,减少Java启动时间的需求变得越来越重要。 无服务器架构就是这样一个领域,它需要更快的启动时间以及容器平台上托管的应用程序。一些项目已经接受了GraalVM Native Image作为其应用程序的平台,例如QuarkusMicronaut 。 Spring Framework最近宣布即将支持本机映像

Image for post

7. GraalVM — Execution and Start-up time“Loading Time Affects Your Bottom Line”GraalVM allows you to compile a program ahead of time into a native executable that runs with Substrate VM. Substrate VM is written in Java and compiled into the native executable. The resulting program has faster startup time and less runtime memory overhead compared to a Java VM.

7. GraalVM-执行和启动时间 “加载时间影响您的底线” GraalVM允许您提前将程序编译为与Substrate VM一起运行的本机可执行文件。 基板VM用Java编写,然后编译为本地可执行文件。 与Java VM相比,生成的程序具有更快的启动时间和更少的运行时内存开销。

We measured the startup and memory footprint of a basic microservice build in Micronaut.

我们测量了Micronaut中基本微服务构建的启动和内存占用。

My GitHub Repo for Demo Apps build with Micronauts for benchmarking :https://github.com/scanpratik/micronauts-GraalVM--JIThttps://github.com/scanpratik/micronauts-nativeImage-GraalVMhttps://github.com/scanpratik/graalVM-SpringBoot2.X

我的演示应用程序的GitHub Repo使用Micronauts构建以进行基准测试: https : //github.com/scanpratik/micronauts-GraalVM--JIT https://github.com/scanpratik/micronauts-nativeImage-GraalVM https://github.com /scanpratik/graalVM-SpringBoot2.X

Image for post
Ref: https://www.graalvm.org
参考: https : //www.graalvm.org
Image for post
Ref: https://www.graalvm.org
参考: https : //www.graalvm.org

8. GraalVM Polyglot Runtime“Truffle Language Implementation Framework”A new language implementation framework called Truffle makes it possible to implement language interpreters that are both simple and high performance. So GraalVM is not only a JIT compiler and ahead-of-time native compiler for Java, but it can also be a JIT compiler for JavaScript, Ruby, R, and Python. Polyglot embeddings can also be compiled ahead-of-time.

8. GraalVM Polyglot运行时 “松露语言实现框架”一种称为Truffle的新语言实现框架使实现简单而高性能的语言解释器成为可能。 因此GraalVM不仅是Java的JIT编译器和提前的本机编译器,而且还可以是JavaScript,Ruby,R和Python的JIT编译器。 多语言嵌入也可以提前编译。

Image for post
Oracle White Paper of GraalVM GraalVM的Oracle白皮书

Your pom.xml file as for using Truffle plugin :

关于使用Truffle插件的pom.xml文件:

<dependency>
<groupId>org.graalvm.truffle</groupId>
<artifactId>truffle-api</artifactId>
<version>20.1.0</version> <!-- or any later version -->
</dependency>
<dependency>
<groupId>org.graalvm.truffle</groupId>
<artifactId>truffle-dsl-processor</artifactId>
<version>20.1.0</version>
<scope>provided</scope>
</dependency>$ gu install native-image
$ gu install ruby
$ gu install python
$ gu install R

Cloud-Native Microservices, Serverless — Design Decision :Sometimes good performance means good throughput, sometimes it means low memory usage, and sometimes it means a fast cold-start startup time.

云原生的微服务,无服务器-设计决策:有时良好的性能意味着良好的吞吐量,有时意味着较低的内存使用,有时意味着快速的冷启动启动时间。

GraalVM vs Hotspot VM ( OpenJDK) — A Trade-offs while selecting Native Image:Benefits of “Native-image” aren’t for free — they come with a cost.Obviously the native executable can only run on a single platform. Another limitation is caused by missing metadata during runtime. Since by default native image doesn’t retain information about classes and methods, hence one’s ability to perform reflection is limited. As there are many Java frameworks that rely on reflective access, getting them run on Native Image may require additional configuration.

GraalVM与Hotspot VM(OpenJDK)—选择本机映像时需要权衡: “本机映像”的好处不是免费的,而是要付出代价的。显然,本机可执行文件只能在单个平台上运行。 另一个限制是由于运行时缺少元数据引起的。 由于默认情况下本机图像不保留有关类和方法的信息,因此限制了执行反射的能力。 由于有许多Java框架都依赖反射访问,因此要使其在Native Image上运行可能需要其他配置。

Let’s see in next section, Part2 — how next wave of Microservices and Serverless are getting build based on modern frameworks like Micronaut build on GraalVM.

让我们在下一部分,第二部分中看到-下一波Microservices和Serverless如何基于基于GraalVM的Micronaut等现代框架进行构建。

翻译自: https://medium.com/swlh/graalvm-byte-code-to-bit-code-39463aa209b4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值