美文网首页
Micronaut微服务 | 实战入门

Micronaut微服务 | 实战入门

作者: 朝槿木兮 | 来源:发表于2021-03-12 17:37 被阅读0次
    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:
    ddb51adf817b50ac.png

    [⚠️注意事项]:
    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:
    49615c6c98acfbc1.png

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

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

    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

    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:项目对象模型

    <?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镜像的配置文件

    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 应用的配置文件
    profile: service
    defaultPackage: pandora.cloud.generator
    ---
    testFramework: junit
    sourceLanguage: java
    

    项目启动

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


    e400cd0f0ef1d7f6.png 3d59552697bf8857.png

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

    相关文章

      网友评论

          本文标题:Micronaut微服务 | 实战入门

          本文链接:https://www.haomeiwen.com/subject/lkbzqltx.html