micronaut 學習 二 建立一個簡單的服務

micronaut 提供的cli 很方便,咱們能夠快速建立具備所需特性的應用,如下是一個簡單的web server apphtml

建立命令

mn create-app hello-world

效果java

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.javagit

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 

效果github

 

 

部署打包軟件

java -jar build/libs/hello-world-0.1-all.jar 
 

效果:web

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 文件
dockerfiledocker

 
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"

運行&&效果api

docker-compose up -d
 

效果:app

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

curl http://localhost:8080/hello 
Hello World% 

參考資料

https://docs.micronaut.io/snapshot/guide/index.html
https://github.com/rongfengliang/micronaut-hello-worldide

相關文章
相關標籤/搜索