首页 文章 精选 留言 我的

精选列表

搜索[连接],共10000篇文章
优秀的个人博客,低调大师

反应式微框架 Reactor-Guice 0.12.5, 修复 websocket 连接退出

Reactor-guice 是一个基于 Google Guice 和 Reactor-netty 的 反应式微服务框架 设计为大家喜欢的通过注解来配置路由,性能基于反应式框架的特点,非常的高效。 对于 web 开发常见的 Json, Protobuf, 表单,文件上传,websocket ,都是轻松支持 0.0.3 注册注解 @GET @POST @PUT @DELETE @Products @PATH 支持 Websocket 可作为静态文件服 支持对 URI 做 Filter 处理 0.0.5 静态文件目录后默认输出 index.html 支持自选模板引擎,自带Freemark 和 Thymeleaf 处理类 支持自定义Json引擎,自带 Gson 和jackson 的处理类 添加 ModelMap, 注入到 Controlle 的方法,用户模板的变量代入 可以上传文件了 0.0.8 可以 POST 数组 通过返回 Mono.just("redirect:/...") 实现重定向 支持 API 网关模式 修复头信息错误的 BUG 可选返回跨域头信息 0.10 支持输出 Protobuf BeanParm 可以支持 Form ,Json 或 Protobuf ,将上传的数据自动组织成对象 上传的文件接受类型 byte[] , UploadFile, File 上传文件可以自动保存到指定的目录 0.11 增加自动扫描 Controlle 类和 Service 类 通过扫描完成路由配置和依赖注入,不同手动做额外配置 0.12.1 静态文件的读取塞入到异步中去处理 这个版本是一个稳定的版本 0.12.2 优化 Jar 内读取文件的变量 简化 websocket 的接口 网关模式增加 websocket 0.12.4 增加 Https (0.12.3) 静态文件目录可配置多个 修复上传文件多次后,溢出的情况,补充 FileUpload.release() 修复非 Form POST ,错误的做了 Bytebuf.release() 0.12.5 修复 websocket 长链接 bug 1. 引入 reactor-guice maven <dependency> <groupId>com.doopp</groupId> <artifactId>reactor-guice</artifactId> <version>0.12.5</version> </dependency> gradle compile 'com.doopp:reactor-guice:0.12.5' 2. 创建应用 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(); } 3. 创建 Controller Controller Example @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); } } WebSocket @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); } } Api Gateway 模式 ReactorGuiceServer.create() .bind(host, port) .setApiGatewayDispatcher(new MyApiGatewayDispatcher()) .addFilter("/", TestFilter.class) .launch(); 混合的 Api Gateway Model 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); } Protobuf // 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); }

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册