Micronaut微服务 实战入门

da130d26e1cbe2c1.jpeg

Your story may not have a happy beginning, but that doesn’t make you who you are, it is restof your story, who you choose to be.

你或许没有一个幸福的开始,但是这并不能够代表你的所有,接下来你的生活取决于你的选择。——《功夫熊猫2》

基本概述

92bf8877f200b467.png

既然决定去做一件事情,就好比把自己铺成一张稿纸,不论是信笔涂鸦还是书写酣畅,就尽情去享受和体验就好。每一进步一点,天道酬勤,谁都会是大赢家。尽管是在这个尴尬的二十几岁,正是应该丰富自己和提升自己的时候,去经历一些非议,成长才会更快。就像是缓存清零,重新启动,一切还是原来模样……在基础入门篇时候,简单概述了Micronaut配置操作。接下来,针对于这一方面做更近一步的学习。比如,在开发工具方面的配置,在Micronaut相关命令和API的运用,创建完Demo工程的项目结构解析以及项目构建等。

开发IDE配置

Micronaut开发工具有IntelliJ IDEA ,Eclipse,Visual Studio Code三种。

IntelliJ IDEA配置

[1].增加Micronaut插件IntelliJ IDEA配置
5684cfa51596fa7d.png

[2].对于Maven 和Gradle工具的配置:

f30f526def0533f6.png

创建项目工程

  • Micronaut CLI Project Creation Commands:
Command Description Options Example
create-app Creates a basic Micronaut application -p Command Flags mn create-app my-project –features mongo-reactive,security-jwt –build maven
create-cli-app Creates a command-line Micronaut application. -p Command Flags mn create-cli-app my-project –features http-client,jdbc-hikari –build maven –lang kotlin –test kotest
create-function-app Creates a Micronaut serverless function, using AWS by default. -p Command Flags mn create-function-app my-lambda-function –lang groovy –test spock
create-messaging-app Creates a Micronaut application that only communicates via a messaging protocol. Uses Kafka by default but can be switched to RabbitMQ with –features rabbitmq. -p Command Flags mn create-function-app my-lambda-function –lang groovy –test spock
create-grpc-app Creates a Micronaut application that uses gRPC. -p Command Flags mn create-grpc-app my-grpc-app –lang groovy –test spock

[⚠️注意事项]:
Micronaut创建工程支持的模板如下:
create-app:创建Micronaut基础应用程序
create-cli-app:创建Micronaut命令行应用程序
create-function-app:创建Micronaut函数应用程序,默认使用AWS
create-messaging-app:创建Micronaut消息队列应用程序,默认使用Kafka
create-grpc-app:创建Micronaut分布式GRPC应用程序

  • Create Command Flags:
Flag Description Example
-l, –lang Language to use for the project (one of java, groovy, kotlin - default is java) –lang groovy
-t, –test Test framework to use for the project (one of junit, spock - default is junit) –test spock
-b,–build Build tool (one of gradle, gradle_kotlin, maven - default is gradle for the languages java and groovy; default is gradle_kotlin for language kotlin) –build maven
-f,–features Features to use for the project, comma-separated –features security-jwt,mongo-gorm
-i,–inplace If present, generates the project in the current directory (project name is optional if this flag is set) –inplace

[⚠️注意事项]:
Micronaut创建工程支持的参数如下:
–lang:支持java, groovy, kotlin语言
–test:支持junit, spock测试框架
–build:支持gradle, gradle_kotlin, maven构建工具
–features:支持众多第三方框架
–inplace:支持替换参数

在本地工程目录:/Users/Projects/GitlabCloud/pandora-cloud-platform中:

1
2
3
4
5
6
7
8
9
10
shell复制代码MacBook-Pro:pandora-cloud-platform root$ cd /Users/Projects/GitlabCloud/pandora-cloud-platform
MacBook-Pro:pandora-cloud-platform root$ ls
LICENSE pandora-cloud-gateway
README.en.md pandora-cloud-model
README.md pandora-cloud-platform.iml
pandora-cloud-console pandora-cloud-program
pandora-cloud-core pandora-cloud-schedule
pandora-cloud-dependencies pom.xml
pandora-cloud-framework
MacBook-Pro:pandora-cloud-platform root$

输入:mn create-app com.pandora-cloud-monitor –build maven –lang=java

1
2
3
4
shell复制代码MacBook-Pro:pandora-cloud-platform root$ mn create-app com.pandora-cloud-monitor --build maven --lang=java
| Generating Java project...
| Application created at /Users/Projects/GitlabCloud/pandora-cloud-platform/pandora-cloud-monitor
MacBook-Pro:pandora-cloud-platform root$

工程结构

项目工程机构如下:
ca83664c6a831016.png

  • .gitignore:分布式版本控制系统git的配置文件,意思为忽略提交
    在 .gitingore 文件中,遵循相应的语法,即在每一行指定一个忽略规则。 如:.log、/target/、.idea
  • mvnw:全名是maven wrapper的文件
    它的作用是在maven-wrapper.properties文件中记录你要使用的maven版本,当用户执行mvnw clean 命令时,发现当前用户的maven版本和期望的版本不一致,那么就下载期望的版本,然后用期望的版本来执行mvn命令,比如mvn clean命令。
  • mvn文件夹:存放mvnw相关文件
    存放着maven-wrapper.properties和相关jar包以及名为MavenWrapperDownloader的java文件
  • mvn.cmd:执行mvnw命令的cmd入口
    注:mvnw文件适用于Linux(bash),mvnw.cmd适用于Windows 环境。
  • pom.xml:项目对象模型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
xml复制代码<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pandora.cloud.generator</groupId>
<artifactId>pandora-cloud-generator</artifactId>
<version>0.1</version>
<properties>
<micronaut.version>1.2.6</micronaut.version>
<jdk.version>1.8</jdk.version>
<maven.compiler.target>${jdk.version}</maven.compiler.target>
<maven.compiler.source>${jdk.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<exec.mainClass>pandora.cloud.generator.Application</exec.mainClass>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<maven-failsafe-plugin.version>2.22.2</maven-failsafe-plugin.version>
</properties>
<repositories>
<repository>
<id>jcenter.bintray.com</id>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-bom</artifactId>
<version>${micronaut.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-runtime</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${exec.mainClass}</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>-noverify</argument>
<argument>-XX:TieredStopAtLevel=1</argument>
<argument>-Dcom.sun.management.jmxremote</argument>
<argument>${exec.mainClass}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<detail>true</detail>
<includes>
<include>%regex[.*]</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>1.2.6</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<version>1.2.6</version>
</path>
</annotationProcessorPaths>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>1.2.6</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<version>1.2.6</version>
</path>
</annotationProcessorPaths>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和licenses,以及其他所有的项目相关因素,是项目级别的配置文件。

  • src:存放开发代码和资源目录
    6cd941b828d50687.png
  • Dockerfile :构建Docker镜像的配置文件
1
2
3
4
yml复制代码FROM adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim
COPY target/pandora-cloud-generator-*.jar pandora-cloud-generator.jar
EXPOSE 8080
CMD java -Dcom.sun.management.jmxremote -noverify ${JAVA_OPTS} -jar pandora-cloud-generator.jar
  • micronaut-cli.yml:micronaut 应用的配置文件
1
2
3
4
5
yml复制代码profile: service
defaultPackage: pandora.cloud.generator
---
testFramework: junit
sourceLanguage: java

项目启动

Micronaut应用程序的启动方式和Springboot应用启动一样,通过调用Micronaut.run来实现:
e400cd0f0ef1d7f6.png

3d59552697bf8857.png

版权声明:本文为博主原创文章,遵循相关版权协议,如若转载或者分享请附上原文出处链接和链接来源。

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%