micronaut 学习 二 创建一个简单的服务
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 学习 二 创建一个简单的服务的更多相关文章
- pipelinewise 学习二 创建一个简单的pipeline
pipelinewise 提供了方便的创建简单pipeline的命令,可以简化pipeline 的创建,同时也可以帮我们学习 生成demo pipeline pipelinewise init --n ...
- 服务注册中心之ZooKeeper系列(二) 实现一个简单微服务之间调用的例子
上一篇文章简单介绍了ZooKeeper,讲了分布式中,每个微服务都会部署到多台服务器上,那服务之间的调用是怎么样的呢?如图: 1.集群A中的服务调用者如何发现集群B中的服务提供者呢? 2.集群A中的服 ...
- ROS学习笔记11-写一个简单的服务和客户端(C++版本)
本文主要来源于:http://wiki.ros.org/ROS/Tutorials/WritingServiceClient%28c%2B%2B%29 写一个服务节点.在创建消息和服务中,我们创建了一 ...
- python创建一个简单的服务
python -m http.server 8000 --bind 0.0.0.0 8000为端口 0.0.0.0允许远程访问
- (转)微服务_创建一个简单的Eureka注册中心
原文地址:https://www.cnblogs.com/lplshermie/p/9105329.html 微服务和分布式已经成了一种极其普遍的技术,为了跟上时代的步伐,最近开始着手学习Spring ...
- 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(二)(代码篇)
这篇是上一篇的延续: 用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一) 源代码在github上可以下载,地址:https://github.com/guoxia ...
- [WCF学习笔记] 我的WCF之旅(1):创建一个简单的WCF程序
近日学习WCF,找了很多资料,终于找到了Artech这个不错的系列.希望能从中有所收获. 本文用于记录在学习和实践WCF过程中遇到的各种基础问题以及解决方法,以供日后回顾翻阅.可能这些问题都很基础,可 ...
- Python框架学习之用Flask创建一个简单项目
在前面一篇讲了如何创建一个虚拟环境,今天这一篇就来说说如何创建一个简单的Flask项目.关于Flask的具体介绍就不详细叙述了,我们只要知道它非常简洁.灵活和扩展性强就够了.它不像Django那样集成 ...
- BitAdminCore框架应用篇:(二)创建一个简单的增删改查模块
NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookie ...
随机推荐
- Django 模板语言 变量名称
Django 模板语言 变量名称 模板语言中已变量形式显示 # view 文件内 def func(request): return render(request,"index.html&q ...
- requests获取响应时间(elapsed)与超时(timeout)、小数四舍五入
前言 requests发请求时,接口的响应时间,也是我们需要关注的一个点,如果响应时间太长,也是不合理的.如果服务端没及时响应,也不能一直等着,可以设置一个timeout超时的时间 elapsed官方 ...
- 揭秘丨7分钟看懂华为云鲲鹏Redis背后的自研技术【华为云技术分享】
2019年5月,华为云发布全球首个基于自研ARM架构的分布式缓存鲲鹏Redis,搭载华为LibOS+华为编译器+安全容器引擎三项黑科技,在保证Redis强劲高性能外,还降低客户30%的使用成本,真正实 ...
- OpenGL学习 (一) - 简单窗口绘制
一.OpenGL 简介 OpenGL 本质: OpenGL(Open Graphics Library),通常可以认为是API,其包含了一系列可以操作图形.图像的函数.但深究下来,它是由Khronos ...
- springboot初体验-不知道怎么创建spring-boot项目?
https://spring.io/projects/spring-boot/ 在以上地址找到 Quick start Bootstrap your application with Spring I ...
- 修改dedecms 一些配置cfg_softname,cfg_soft_enname,cfg_soft_devteam
1.找到include 中的common.inc.php 直接修改就可以了.
- C#读写修改设置调整UVC摄像头画面-全景
有时,我们需要在C#代码中对摄像头的全景进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...
- C++之拷贝控制 (Copy Control)
只有2种成员 值成员: 指针成员: 依实现可分为raw pointer / shared_ptr; 现在,仅考虑第③种:资源对象共享 角度来考虑拷贝控制 类的两种语义:值语义.似指针 编译器提供的de ...
- pandas-22 数据去重处理
pandas-22 数据去重处理 数据去重可以使用duplicated()和drop_duplicates()两个方法. DataFrame.duplicated(subset = None,keep ...
- python3常用的内置函数
数学相关 abs(a) : 求取绝对值.abs(-1) max(list) : 求取list最大值.max([1,2,3]) min(list) : 求取list最小值.min([1,2,3]) su ...