micronaut 提供的cli 很方便,我们可以快速创建具有所需特性的应用,以下是一个简单的web server app
创建命令
mn create-app hello-world
效果
mn create-app hello-world
| Generating Java project...
| Application created at /Users/dalong/mylearning/micronaut-project/hello-world
启动服务
./gradlew run
添加简单代码
src/main/java/hello/world/HelloController.java
package hello.world;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
@Controller("/hello")
public class HelloController {
@Get(produces = MediaType.TEXT_PLAIN)
public String index() {
return "Hello World";
}
}
访问效果
curl http://localhost:8080/hello
Hello World%
集成测试
- 测试controller
使用httpclient
package hello.world;
import io.micronaut.context.annotation.Property;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.runtime.server.EmbeddedServer;
import io.micronaut.test.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import javax.inject.Inject;
import static org.junit.jupiter.api.Assertions.assertEquals;
@MicronautTest
class HelloControllerSpec {
@Inject
EmbeddedServer server;
@Inject
@Client("/")
HttpClient client;
@Test
void testHelloWorldResponse() {
String response = client.toBlocking()
.retrieve(HttpRequest.GET("/hello"));
assertEquals("Hello World", response); //)
}
}
- 测试service
package hello.world;
import io.micronaut.test.annotation.MicronautTest;
import javax.inject.Inject;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@MicronautTest
public class HelloClientSpec {
@Inject
HelloClient client;
@Test
public void testHelloWorldResponse(){
assertEquals("Hello World", client.hello().blockingGet());
}
}
打包应用
./gradlew assemble
效果
部署打包软件
java -jar build/libs/hello-world-0.1-all.jar
效果:
hello-world java -jar build/libs/hello-world-0.1-all.jar
16:20:24.013 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 2009ms. Server Running: http://localhost:8080
docker 运行
默认micronaut 生成的项目包含了一个dockerfile ,我添加了一个docker-compose 文件
dockerfile
FROM adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim
COPY build/libs/hello-world-*-all.jar hello-world.jar
EXPOSE 8080
CMD java -Dcom.sun.management.jmxremote -noverify ${JAVA_OPTS} -jar hello-world.jar
version: "3"
services:
app:
image: dalongrong/micronaut-hello-world
build: ./
ports:
- "8080:8080"
运行&&效果
docker-compose up -d
效果:
Creating network "hello-world_default" with the default driver
Building app
Step 1/4 : FROM adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim
jdk-11.0.1.13-alpine-slim: Pulling from adoptopenjdk/openjdk11-openj9
4fe2ade4980c: Pull complete
1ba2a07c78f2: Pull complete
944724f145c9: Pull complete
1cb512dae36f: Pull complete
Digest: sha256:60718fa9eb6b6bc4ab6fe7f3a9db31b8725fb63ebdda833a43f541c07792ff5c
Status: Downloaded newer image for adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim
---> f3f4b8ddca6f
Step 2/4 : COPY build/libs/hello-world-*-all.jar hello-world.jar
---> b794f9b8e0d4
Step 3/4 : EXPOSE 8080
---> Running in d3e19ae06642
Removing intermediate container d3e19ae06642
---> 2f4abdc6ddfd
Step 4/4 : CMD java -Dcom.sun.management.jmxremote -noverify ${JAVA_OPTS} -jar hello-world.jar
---> Running in 5958c2868e24
Removing intermediate container 5958c2868e24
---> 44c4289cb2b6
Successfully built 44c4289cb2b6
Successfully tagged dalongrong/micronaut-hello-world:latest
WARNING: Image for service app was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating hello-world_app_1 ... done
访问:
curl http://localhost:8080/hello
Hello World%
参考资料
https://docs.micronaut.io/snapshot/guide/index.html
https://github.com/rongfengliang/micronaut-hello-world