
Some people hear their own inner voices with great clearness. And they live by what they hear.Such people become crazy,or they become legends ...... 有些人能清楚地聽到自己内心深處的聲音,并以此行事。這些人要麼變成了瘋子,要麼成為傳奇。——《秋日傳奇》
A modern, JVM-based, full-stack framework for building modular, easily testable microservice and serverless applications. ——by[https://micronaut.io/]
作為一名程式員,在微服務[MicroService]亂入的世界,聽着擁抱雲原生[Cloud Native],馬踏無服務[Serverless]的論斷。或許都會經不住誘惑,會自主在搜尋相關的關鍵詞去探究一下這些新鮮以及陌生的字眼。但是實際上,在經過現實的熏陶之後,我們所擺弄的還是最原始的形态。大多數在進入一家企業之後,不由自主的發現,在Spring的生态影響下,現在大部分程式員都活躍在這個架構裡。假如跳出這個架構之後,我們是否還能對現在的工作得心應手,恐怕是要打上一個大大的問号的?在一次偶然的機會,知道了Micronaut。于是,開啟了對Micronaut的探索之路......
Micronaut is a modern, JVM-based, full stack Java framework designed for building modular, easily testable JVM applications with support for Java, Kotlin, and Groovy.
基于JVM的現代全棧Java架構,旨在通過支援Java,Kotlin和Groovy語言來構模組化塊化,易于測試的微服務應用程式以及無服務應用程式。
Micronaut is developed by the creators of the Grails framework and takes inspiration from lessons learnt over the years building real-world applications from monoliths to microservices using Spring, Spring Boot and Grails.
Micronaut由Grails架構的建立者開發,并借鑒了多年使用Spring,Spring Boot和Grails建構從單塊到微服務的實際應用程式的經驗教訓。
Micronaut aims to provide all the tools necessary to build JVM applications including: Dependency Injection and Inversion of Control (IoC) Aspect Oriented Programming (AOP) Sensible Defaults and Auto-Configuration
Micronaut旨在提供建構JVM應用程式所需的所有工具,包括:
依賴注入和控制反轉(IoC)
面向方面的程式設計(AOP)
合理的預設設定和自動配置
With Micronaut you can build Message-Driven Applications, Command Line Applications, HTTP Servers and more whilst for Microservices in particular Micronaut also provides: Distributed Configuration Service Discovery HTTP Routing Client-Side Load Balancing
使用Micronaut,您可以建構消息驅動的應用程式,指令行應用程式,HTTP伺服器等,而對于Microservices,Micronaut特别提供以下功能:
分布式配置
服務發現
HTTP路由
用戶端負載平衡
At the same time Micronaut aims to avoid the downsides of frameworks like Spring, Spring Boot and Grails by providing: Fast startup time Reduced memory footprint Minimal use of reflection Minimal use of proxies No runtime bytecode generation Easy Unit Testing
同時,Micronaut旨在通過提供以下内容來避免諸如Spring,Spring Boot和Grails之類的架構的弊端:
快速啟動時間
低記憶體占用
最小粒度使用反射
最小粒度使用代理
無運作時位元組碼生成
簡單的單元測試
Historically, frameworks such as Spring and Grails were not designed to run in scenarios such as serverless functions, Android apps, or low memory footprint microservices. In contrast, Micronaut is designed to be suitable for all of these scenarios. This goal is achieved through the use of Java’s annotation processors, which are usable on any JVM language that supports them, as well as an HTTP Server and Client built on Netty. To provide a similar programming model to Spring and Grails, these annotation processors precompile the necessary metadata to perform DI, define AOP proxies and configure your application to run in a low-memory environment. Many APIs in Micronaut are heavily inspired by Spring and Grails. This is by design, and helps bring developers up to speed quickly.
綜上所述,Micronaut設計的初衷是面向無服務,低記憶體占用的微服務等場景而生的。通過使用Java的(可在支援它們的任何JVM語言中使用)以及基于Netty建構的HTTP Server和Client 來實作此目标。提供與Spring和Grails相似的程式設計模型,利用批注處理器會預編譯必要的中繼資料,以執行DI,定義AOP代理并配置您的應用程式以在低記憶體環境中運作。
1.Micronaut SDK安裝
This project implements a project generator for Micronaut as is both a Command Line (CLI) application and an API that can be run as a HTTP server, deployed to a Servlet container or executed on a Serverless platform (Google Cloud Run, Google Cloud Function, AWS Lambda or Azure Function).
目前Micronaut最新的版本為2.3.4。Micronaut建立工程實提供了Micronaut Command Line (CLI) 指令行和 Micronaut API接口2種方式,應用可部署在Servlet container[Web 容器]和Serverless Platform [無服務架構]平台。
SDK安裝方式有以下幾種:
Install with SDKman
Install with Homebrew
Install with MacPorts
Install with Chocolatey
Install through Binary on Windows
Build & Install from Source
在Unix/Linux環境使用SDKman安裝: [1].sdk update更新
[1].sdk install micronaut 安裝SDK
在Mac環境使用Homebrew和MacPorts安裝:
[1].MacPorts安裝:
[2].Homebrew安裝:
MacPorts的理念是盡量減少對系統現有庫的依賴,是以它需要下載下傳許多基礎庫,然後編譯安裝到系統中。這樣做最大的問題在于下載下傳時間長,編譯時間長,最大的好處就是不會破壞系統原有軟體包。
[⚠️注意事項]: 1.Mac OSX系統上有許多優秀的包管理軟體能夠将Linux上常用的指令工具和依賴安裝到系統上,主要有Fink、Macports、Homebrew這3個主流包管理軟體。 2.MacPorts依賴于BSD的軟體包管理工具(port),它的工作方式是從倉庫中下載下傳軟體包以及其依賴庫,然後在本機中編譯安裝。盡量減少對系統現有庫的依賴,是以它需要下載下傳許多基礎庫,然後編譯安裝到系統中。這樣做最大的問題在于下載下傳時間長,編譯時間長,最大的好處就是不會破壞系統原有軟體包。 3.Homebrew與MacPorts的工作方式類似,也是從倉庫中下載下傳軟體包以及其依賴庫,然後在本機中編譯安裝。Homebrew的理念是盡量使用系統中存在的庫,且軟體包都安裝到/usr/local目錄裡,最大的好處就是下載下傳編譯時間短于MacPorts,安裝簡單,且無需root使用者權限來安裝。 3.Fink是一個希望把越來越多Unix上軟體帶到Mac OSX系統上的項目。它通過移植軟體源碼,然後打包成能在Mac OSX系統上執行的二進制進行分發。Fink依賴于Debian的軟體包管理工具(dpkg/dselect/apt-get),是以它将軟體包打包成.deb格式。這樣做最大的好處就是安裝軟體是預編譯好的,是以無需本機編譯。Fink最大的問題在于軟體包的更新疊代比較慢,是以要想使用最新版軟體包,則需要等待Fink社群更新。
在Windows環境下,使用Chocolatey和Binary二進制檔案安裝:
[1].Chocolatey安裝:
[2].Binary二進制安裝:設定環境變量[%MICRONAUT_HOME%]
除此之外還可以下載下傳源碼進行安裝: [1].利用Git克隆源碼:
[2].利用gradlew建構:
[3].配置環境安裝:
[4].配置環境變量:
[4].配置環境變量生效:
[⚠️注意事項]: [1].源碼安裝以上方式支援Linux和Unix以及Macos [2].Windows環境按照對應的配置即可
2.Micronaut SDK使用
一般建立代碼使用mn指令:
[1].主函數mn: Micronaut CLI 提供的主函數名 [2].create-[COMMAND]:Micronaut CLI 提供的應用程式模闆:
create-app NAME
create-cli-app NAME
create-function-app NAME
create-grpc-app NAME
create-messaging-app NAME
[2].--build:建構工具支援:gradle, gradle_kotlin, maven.
例如: