Micro Services Reactor-Guice framework 0.12.2 release, the gateway mode support Websocket

Reactor-guice Micro Reactor is a service based on Google Guice framework and the Reactor-netty

Routing and auto-configuration through annotations dependency injection, or use him as your gateway service, because only the use of reflection during the boot process to complete the configuration, so his performance is based Guice and Reactor.

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

1. Introducing a reactor-guice

maven

<dependency>
    <groupId>com.doopp</groupId>
    <artifactId>reactor-guice</artifactId>
    <version>0.12.2</version>
</dependency>

gradle

compile 'com.doopp:reactor-guice:0.12.2'

2. Create the application

static void main public (String [] args) throws IOException { 
        // Loading arranged 
        the Properties Properties new new = the Properties (); 
        Properties.load (new new the FileInputStream (args [0])); 

        String = Properties.getProperty Host ( "Server. Host "); 
        int Port = Integer.valueOf (Properties.getProperty (" server.port ")); 
        // start the service 
        ReactorGuiceServer.create () 
                .bind (Host, Port) 
                .createInjector ( 
                        // easy to use to get @Names configuration 
                        Binder -> Names.bindProperties (Binder, Properties), 
                        // database 
                        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(),
                        // 自定义的配置
                        ApplicationModule new new () 
                )
                // 配置 Json 处理类
                .setHttpMessageConverter (new new MyGsonHttpMessageConverter ()) 
                // set automatically scans Controller and Service package name, you can configure multiple 
                .basePackages ( "com.doopp.gauss.app", ...) 
                // currently only support filtered through URI, multiple times addFilter 
                .addFilter ( "/", AppFilter.class) 
                // error information output 
                .printError (to true) 
                .launch (); 
    }

3. Create 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 mode

ReactorGuiceServer.create()
        .bind(host, port)
        .setApiGatewayDispatcher(new MyApiGatewayDispatcher())
        .addFilter("/", TestFilter.class)
        .launch();

Mixed 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();

Forms and file uploads

// 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

@GET
@Path("/test/protobuf")
@Produces("application/x-protobuf")
public Mono<byte[]> testProtobuf() {
    Hello.Builder builder = Hello.newBuilder();
    builder.setId(123);
    builder.setName("wuyi");
    builder.setEmail("[email protected]");
    return Mono.just(builder.build().toByteArray());
}

Guess you like

Origin www.oschina.net/news/107494/react-guice-0-12-2-released