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. [SOJ #721]第三送分题(2019-11-14考试)/[CF675E]Trains and Statistic

    题目大意 在一条直线上有\(n\)个点.在第\(i\)个点可以花费\(1\)的代价到达\((i,a_i]\)中任意一点,用\(S[i][j]\)表示从点\(i\)到点\(j\)的最少花费,求\(\su ...

  2. 二十三、并发编程之深入解析Condition源码

    二十三.并发编程之深入解析Condition源码   一.Condition简介 1.Object的wait和notify/notifyAll方法与Condition区别 任何一个java对象都继承于 ...

  3. TinyXPath 对于xpath标准的支持测试

    xpath是一种基于xml的查询标准,一般的xml解析工具都具有,有的因为卓越的xpath性能而出名,其匹配查询算法牛逼而又高效,和正则有的一拼.虽然我现在大部分从事前端工作了,但是对于原理性的东西还 ...

  4. C# 判断域名或ip+端口号 是否能正常连接?

    private static ManualResetEvent TimeoutObject = new ManualResetEvent(false); /// <summary> /// ...

  5. MYSQL中的时间类型

    时间上总共有五中表示方法:它们分别是 time.date.datetime.timestamp和year. time :  “hh:mm:ss”格式表示的时间值,格式显示TIME值,但允许使用字符串或 ...

  6. C/C++ 函数参数传递:传值,传指针,传引用

    前面我们介绍了函数的调用约定,明白了函数调用者与被调用者之间传递参数的顺序与如何进行栈恢复的. 实际上,函数调用者如何将参数传递给被调用者也是有讲究的. 总的来说,函数参数传递分为3种情况:传值,传指 ...

  7. 下载文件时-修改文件名字 Redis在Windows中安装方法 SVN安装和使用(简单版) WinForm-SQL查询避免UI卡死 Asp.Net MVC Https设置

    下载文件时-修改文件名字   1后台代码 /// <summary> /// 文件下载2 /// </summary> /// <param name="Fil ...

  8. Mac 提示错误”xcrun: error“

    错误现象: Mac 安装python模块时出现异常错误 xcrun: error: invalid active developer path (/Library/Developer/CommandL ...

  9. Linux定时任务运行thinkPHP某个方法

    先上实力: 1.查看正在执行的crontab,用命令crontab  -l ,这样就可以看到哪些任务一直在执行了.2.crontab -e  自动打开文件 编辑定时任务程序 在打开的页面中点击“i”键 ...

  10. nginx 页面加载不全的问题

    在nginx的server中添加: proxy_buffer_size 2m; proxy_buffers 8 1m; proxy_busy_buffers_size 2m; 这是由于页面内容过长,默 ...