LoginSignup
6
5

More than 5 years have passed since last update.

MicronautでHello World

Last updated at Posted at 2019-02-06

Micronaut?

Micronaut

  • JavaVM上でのフルスタックフレームワークらしい
  • モダンな、MicroserviceやServerlessなアプリケーションを簡単に作れるらしい
  • Grailsを作った人たちが作っているらしい
  • 言語は、Java、Groovy、Kotlinが選べるらしい
  • リアクティブなクライアント、サーバーもサポートしているらしい

Micronaut 1.0への道 - JVMベースのフルスタックフレームワーク

最近、ちらほらと名前を聞くようになってきた気がするので、ちょっと試してみましょう。

環境

今回の利用環境は、こちらです。

$ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

CLIのインストール

アプリケーションの作成は、mnコマンド(CLI)で始めるもののようです。

ダウンロードページからダウンロードするか、SDKMANでインストールします。

今回は、SDKMANを使ってインストールしましょう。

Build/Install the CLI

$ sdk install micronaut

今回は、Micronaut 1.0.4がインストールされました。

$ mn -V
| Micronaut Version: 1.0.4
| JVM Version: 1.8.0_191

アプリケーションのひな型作成

では、mnコマンドを使ってアプリケーションのひな型を作成してみます。

Creating a Server Application

mn create-appで作成されるプロジェクトはGradleが使われるようですが、個人的にApache Mavenが良いので、Mavenを使うように--build mavenを指定します。

$ mn create-app hello-world --build maven

これで、hello-worldディレクトリが作成されます。

$ cd hello-world

生成されたファイルを、リストアップしてみましょう。

$ tree
.
├── Dockerfile
├── micronaut-cli.yml
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── hello
    │   │       └── world
    │   │           └── Application.java
    │   └── resources
    │       ├── application.yml
    │       └── logback.xml
    └── test
        └── java
            └── hello
                └── world

10 directories, 8 files

Maven Wrapperを使ったプロジェクトができています。

Dockerfileまで作られるんですねぇ。

生成されたpom.xmlを見てみると、mainクラスやmaven-shade-pluginexec-maven-pluginの設定がしてあり、そのまま使えそうです。

mainメソッドを持ったクラスは、こちら。

src/main/java/hello/world/Application.java

package hello.world;

import io.micronaut.runtime.Micronaut;

public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class);
    }
}

ビルド、パッケージング。

$ ./mvnw package

JARファイルが作成されたので、起動してみます。

$ java -jar target/hello-world-0.1.jar 
12:56:59.043 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 1108ms. Server Running: http://localhost:8080

起動が1秒!

curlでアクセスしてみます。

$ curl localhost:8080
{"_links":{"self":{"href":"/","templated":false}},"message":"Page Not Found"}

Not Foundですが、レスポンスが返りました。

では、ドキュメントに習い、Controllerを作成してみます。

src/main/java/hello/world/HelloController.java

package hello.world;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;

@Controller("/hello") 
public class HelloController {
    @Get(produces = MediaType.TEXT_PLAIN) 
    public String index() {
        return "Hello World"; 
    }
}

ビルドして、起動。

$ ./mvnw package
$ java -jar target/hello-world-0.1.jar

確認。

$ curl localhost:8080/hello
Hello World

動きましたねー。

IDEで開発する場合

IntelliJ IDEAで開発する場合は、Annotation Processorsを有効にする必要があるようです。

Setting up an IDE

雰囲気、こんな感じで。

GroovyやKotlinで開発する場合

mvn create-app時に、--featuresで調整するようです。

$ mn create-app hello-world-groovy --build maven --features=groovy

$ mn create-app hello-world-kotlin --build maven --features=kotlin

その他の生成されたファイルについて

パッと、貼っておきます。

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>hello.world</groupId>
  <artifactId>hello-world</artifactId>
  <version>0.1</version>
  <properties>
    <micronaut.version>1.0.4</micronaut.version>
    <jdk.version>1.8</jdk.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <exec.mainClass>hello.world.Application</exec.mainClass>
  </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-client</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-inject-java</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-all</artifactId>
      <version>1.3</version>
      <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>-noverify</argument>
            <argument>-XX:TieredStopAtLevel=1</argument>
            <argument>-classpath</argument>
            <classpath/>
            <argument>${exec.mainClass}</argument>
          </arguments>
        </configuration>
      </plugin>
    </plugins>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
          <configuration>
            <source>${jdk.version}</source>
            <target>${jdk.version}</target>
            <encoding>UTF-8</encoding>
            <compilerArgs>
              <arg>-parameters</arg>
            </compilerArgs>
            <annotationProcessorPaths>
                  <path>
                    <groupId>io.micronaut</groupId>
                    <artifactId>micronaut-inject-java</artifactId>
                    <version>${micronaut.version}</version>
                  </path>
                  <path>
                    <groupId>io.micronaut</groupId>
                    <artifactId>micronaut-validation</artifactId>
                    <version>${micronaut.version}</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>${micronaut.version}</version>
                  </path>
                  <path>
                    <groupId>io.micronaut</groupId>
                    <artifactId>micronaut-validation</artifactId>
                    <version>${micronaut.version}</version>
                  </path>
                </annotationProcessorPaths>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Dockerfile

FROM openjdk:8u171-alpine3.7
RUN apk --no-cache add curl
COPY target/hello-world*.jar hello-world.jar
CMD java ${JAVA_OPTS} -jar hello-world.jar

micronaut-cli.yml

profile: service
defaultPackage: hello.world
---
testFramework: junit
sourceLanguage: java

src/main/resources/application.yml

micronaut:
    application:
        name: hello-world

src/main/resources/logback.xml

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>
6
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5