Reactor-guice 是一個基於 Google Guice 和 Reactor-netty 的 反應式微服務框架
設計為大家喜歡的通過註解來配置路由,性能基於反應式框架的特點,非常的高效。
對於 web 開發常見的 Json, Protobuf, 表單,文件上傳,websocket ,都是輕鬆支持
0.0.3 註冊註解 @GET @POST @PUT @DELETE @Products @PATH
0.0.5 靜態文件目錄后默認輸出 index.html
0.0.8 可以 POST 數組
0.10 支持輸出 Protobuf
0.11 增加自動掃描 Controlle 類和 Service 類
0.12.1 靜態文件的讀取塞入到非同步中去處理
0.12.2 優化 Jar 內讀取文件的變數
0.12.4 增加 Https (0.12.3)
0.12.5 修復 websocket 長鏈接 bug
<dependency>
<groupId>com.doopp</groupId>
<artifactId>reactor-guice</artifactId>
<version>0.12.5</version>
</dependency>
gradle
compile 'com.doopp:reactor-guice:0.12.5'
public static void main(String[] args) throws IOException {
// 載入配置
Properties properties = new Properties();
properties.load(new FileInputStream(args[0]));
String host = properties.getProperty("server.host");
int port = Integer.valueOf(properties.getProperty("server.port"));
// 啟動服務
ReactorGuiceServer.create()
.bind(host, port)
.createInjector(
// 方便使用 @Names 來獲取配置
binder -> Names.bindProperties(binder, properties),
// 資料庫
new MyBatisModule() {
@Override
protected void initialize() {
install(JdbcHelper.MySQL);
bindDataSourceProviderType(HikariDataSourceProvider.class);
bindTransactionFactoryType(JdbcTransactionFactory.class);
addMapperClasses("com.doopp.gauss.app.dao");
// addInterceptorClass(PageInterceptor.class);
}
},
// Redis
new RedisModule(),
// 自定義的配置
new ApplicationModule()
)
// 配置 Json 處理類
.setHttpMessageConverter(new MyGsonHttpMessageConverter())
// 設定自動掃描 Controller 和 Service 的包名,可以配置多個
.basePackages("com.doopp.gauss.app", ...)
// 配置多個靜態資源
.addResource("/static/", "/static-public/")
.addResource("/", "/public/")
// https
.setHttps(new File(jksFilePath), jksPassword, jksSecret)
// 目前僅支持通過 URI 來過濾,可以多次 addFilter
.addFilter("/", AppFilter.class)
// 錯誤信息輸出
.printError(true)
.launch();
}
@Controller
@Path("/api/admin")
public class ExampleController {
@GET
@Path("/json")
@Produces({MediaType.APPLICATION_JSON})
public Mono<Map<String, String>> json() {
return Mono
.just(new HashMap<String, String>())
.map(m -> {
m.put("hi", "five girl");
return m;
});
}
@GET
@Path("/jpeg")
@Produces({"image/jpeg"})
public Mono<ByteBuf> jpeg() {
return HttpClient.create()
.get()
.uri("https://static.cnbetacdn.com/article/2019/0402/6398390c491f650.jpg")
.responseContent()
.aggregate()
.map(ByteBuf::retain);
}
}
@Path("/kreactor/ws")
@Singleton
public class WsTestHandle extends AbstractWebSocketServerHandle {
@Override
public void connected(Channel channel) {
System.out.println(channel.id());
super.connected(channel);
}
@Override
public void onTextMessage(TextWebSocketFrame frame, Channel channel) {
System.out.println(frame.text());
super.onTextMessage(frame, channel);
}
}
ReactorGuiceServer.create()
.bind(host, port)
.setApiGatewayDispatcher(new MyApiGatewayDispatcher())
.addFilter("/", TestFilter.class)
.launch();
ReactorGuiceServer.create()
.bind(host, port)
.createInjector(module1, module2, ...)
.setHttpMessageConverter(new JacksonHttpMessageConverter())
.setApiGatewayDispatcher(new MyApiGatewayDispatcher())
.handlePackages("com.doopp.reactor.guice.test")
.addFilter("/", TestFilter.class)
.launch();
// Server
@POST
@Path("/test/post-bean")
public Mono<User> testPostBean(@BeanParam User user, @FileParam(value="image", path = "C:\\Users\\koocyton\\Desktop") File[] file) {
return Mono.just(user);
}
// Client Test
@Test
public void testFileUpload() {
String hhe = HttpClient.create()
.post()
.uri("http://127.0.0.1:8083/kreactor/test/post-bean")
.sendForm((req, form) -> form.multipart(true)
.attr("id", "123123121312312")
.attr("account", "account")
.attr("password", "password")
.attr("name", "name")
.file("image", new File("C:\\Users\\koocyton\\Pictures\\cloud.jpg"))
.file("image", new File("C:\\Users\\koocyton\\Pictures\\st.jpg"))
)
.responseSingle((res, content) -> content)
.map(byteBuf -> byteBuf.toString(CharsetUtil.UTF_8))
.block();
System.out.println(hhe);
}
// Server
@POST
@Path("/test/proto-post-bean")
public Mono<Hello> testPostBean(@BeanParam Hello hello) {
return Mono.just(hello);
}
// Test Client
@Test
public void testPostProtobufBean() {
Hello.Builder builder = Hello.newBuilder();
builder.setId(123);
builder.setName("wuyi");
builder.setEmail("wuyi@doopp.com");
ByteBuf buf = Unpooled.wrappedBuffer(builder.build().toByteArray()).retain();
String hhe = HttpClient.create()
.headers(headers -> {
headers.add(HttpHeaderNames.CONTENT_TYPE, "application/x-protobuf");
})
.post()
.uri("http://127.0.0.1:8083/kreactor/test/proto-post-bean")
.send(Flux.just(buf.retain()))
.responseSingle((res, content) -> content)
.map(byteBuf -> byteBuf.toString(CharsetUtil.UTF_8))
.block();
System.out.println(hhe);
}
[admin
]