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

micronaut 学习 二 创建一个简单的服务的更多相关文章

  1. pipelinewise 学习二 创建一个简单的pipeline

    pipelinewise 提供了方便的创建简单pipeline的命令,可以简化pipeline 的创建,同时也可以帮我们学习 生成demo pipeline pipelinewise init --n ...

  2. 服务注册中心之ZooKeeper系列(二) 实现一个简单微服务之间调用的例子

    上一篇文章简单介绍了ZooKeeper,讲了分布式中,每个微服务都会部署到多台服务器上,那服务之间的调用是怎么样的呢?如图: 1.集群A中的服务调用者如何发现集群B中的服务提供者呢? 2.集群A中的服 ...

  3. ROS学习笔记11-写一个简单的服务和客户端(C++版本)

    本文主要来源于:http://wiki.ros.org/ROS/Tutorials/WritingServiceClient%28c%2B%2B%29 写一个服务节点.在创建消息和服务中,我们创建了一 ...

  4. python创建一个简单的服务

    python -m http.server 8000 --bind 0.0.0.0 8000为端口 0.0.0.0允许远程访问

  5. (转)微服务_创建一个简单的Eureka注册中心

    原文地址:https://www.cnblogs.com/lplshermie/p/9105329.html 微服务和分布式已经成了一种极其普遍的技术,为了跟上时代的步伐,最近开始着手学习Spring ...

  6. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(二)(代码篇)

    这篇是上一篇的延续: 用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一) 源代码在github上可以下载,地址:https://github.com/guoxia ...

  7. [WCF学习笔记] 我的WCF之旅(1):创建一个简单的WCF程序

    近日学习WCF,找了很多资料,终于找到了Artech这个不错的系列.希望能从中有所收获. 本文用于记录在学习和实践WCF过程中遇到的各种基础问题以及解决方法,以供日后回顾翻阅.可能这些问题都很基础,可 ...

  8. Python框架学习之用Flask创建一个简单项目

    在前面一篇讲了如何创建一个虚拟环境,今天这一篇就来说说如何创建一个简单的Flask项目.关于Flask的具体介绍就不详细叙述了,我们只要知道它非常简洁.灵活和扩展性强就够了.它不像Django那样集成 ...

  9. BitAdminCore框架应用篇:(二)创建一个简单的增删改查模块

    NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookie ...

随机推荐

  1. FileChannel详解

    经过前两篇文章的学习,相信对Channel有了一定的整体性认识.接下来通过学习本篇文章,更进一步认识Channel,学习FileChannel的细节 用途 特点 api 原理 一.用途 传统IO中的F ...

  2. Bagging 和RF的区别

    跑训练无聊看了看别人的面经,发现自己一时半会答不上来,整理一下. 一.Bagging介绍 先看一个Bagging的一个概念图(图来自https://www.cnblogs.com/nickchen12 ...

  3. 树莓派4B安装Raspbian系统及配置

    2019/11/11, 树莓派4B, Raspbian Buster 摘要:给树莓派4B安装系统及基础配置 树莓派实验室参考文档 准备工具 树莓派4B硬件 SD卡格式化工具 SD Formatter ...

  4. javascript Class.method vs Class.prototype.method(类方法和对象方法)

    在stackoverflow上看到一个这样的提问,以下代码有什么区别? Class.method = function () { /* code */ } Class.prototype.method ...

  5. Spring Security OAuth2.0 - AuthorizationServer和ResourceServer分离

    <Spring Security实现OAuth2.0授权服务 - 基础版>和<Spring Security实现OAuth2.0授权服务 - 进阶版>两篇文章中介绍如何搭建OA ...

  6. Myeclipse中JSP镶嵌的html报错

    Window > perferences > General > Editors > File Associations > 在File types 中选择 *.jsp ...

  7. 使用node+vue实现简单的WebSocket聊天功能

    最近学习了一下websocket的即时通信,感觉非常的强大,这里我用node启动了一个服务进行websocket链接,然后再vue的view里面进行了链接,进行通信,废话不多说,直接上代码吧, 首先, ...

  8. Eureka设计原理

    1. Eureka设计原理 1.1. 前言 目前我越来越关注技术原理层面的东西,开始考虑中间件设计背后,要考虑哪些因素,为什么要这样设计,有什么优化的地方,这次来讨论Eureka 1.2. 设计问题 ...

  9. Java 之 自定义异常

    1.为什么需要自定义异常类 Java中不同的异常类,分别表示着某一种具体的异常情况,那么在开发中总是有些异常情况是没有定义好的,此时我们根据自己业务的异常情况来定义异常类.  一些异常都是 Java ...

  10. 转 根据CPU核心数确定线程池并发线程数

    转自: https://www.cnblogs.com/dennyzhangdd/p/6909771.html?utm_source=itdadao&utm_medium=referral 目 ...