云原生Java框架--Micronaut
开始使用Micronaut
添加一个Micronaut控制器
package micronaut.idg.controller;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
@Controller("/simple")
public class SimpleController {
@Get(produces = MediaType.TEXT_PLAIN)
public String index() {
return "A Simple Endpoint";
}
}
package micronaut.idg.controller;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import java.util.Map;
import java.util.HashMap;
@Controller("/simple")
public class SimpleController {
@Get(produces = MediaType.APPLICATION_JSON)
public Map index() {
Map msg = new HashMap();
msg.put("message", "A simple message");
return msg;
}
}
添加Micronaut服务层
// Simple.java
package micronaut.idg.service;
public interface Simple {
public String getMessage();
}
// SimpleService.java
package micronaut.idg.service;
import jakarta.inject.Singleton;
@Singleton
public class SimpleService implements Simple {
public String getMessage(){
return "A simple service message";
}
}
@Controller("/simple")
public class SimpleController {
@Inject
private final Simple simpleService;
public SimpleController(@Named("simpleService") Simple simple) { //(1)
thi.simpleService = simple;
}
@Get(produces = MediaType.APPLICATION_JSON)
public Map index() {
Map msg = new HashMap();
msg.put("message", simpleService.getMessage());
return msg;
}
}
使用Micronaut的反应式NIO
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.4.11</version>
</dependency>
import reactor.core.publisher.Mono;
//...
@Get
public Mono<map> index() {
Map msg = new HashMap();
msg.put("message", simpleService.getMessage());
return Mono.just(msg);
}
}
使用Micronaut的CLI创建新的组件
mn create-controller MyController
| Rendered controller to src/main/java/micronaut/idg/MyControllerController.java
| Rendered test to src/test/java/micronaut/idg/MyControllerControllerTest.java
使用Micronaut进行云原生开发
./mvnw deploy \
-Dpackaging=docker \
-Djib.to.image=gcr.io/my-org/my-project:latest
gcloud run deploy \
--image=gcr.io/my-org/my-project:latest \
--platform managed \
--allow-unauthenticated
tracing:
jaeger:
enabled: true
sampler:
probability: 1
小结
译者介绍
粉丝福利
扫码关注视频号,私信“666”
领取技术期刊
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/
关注网络尖刀微信公众号
随时掌握互联网精彩
随时掌握互联网精彩
赞助链接
排名
热点
搜索指数
- 1 和人民在一起 7921797
- 2 江西“最强钉子户”:后悔没答应拆迁 7903965
- 3 柯洁被判负 7808031
- 4 今天明天 都是小年 7789067
- 5 成都巨型刘亦菲成了网红打卡点 7695722
- 6 肖战一出场就有郭靖的感觉 7589240
- 7 #网红潘宏是否涉嫌虐狗# 7405414
- 8 嘴角起泡其实是病毒感染 7331393
- 9 卖猪商户切肉时发现猪被打针 7256443
- 10 第一批见岳父的男生开始坐立不安 7141128