项目作者: StonyShi

项目描述 :
reactor-netty-jersey
高级语言: Java
项目地址: git://github.com/StonyShi/reactor-netty-jersey.git
创建时间: 2018-12-12T10:14:30Z
项目社区:https://github.com/StonyShi/reactor-netty-jersey

开源协议:

下载


reactor-netty-jersey

1. reactor-netty jersey2.x的扩展

2. 扩展get方法支持get?p=1 的方式访问

3. 支持静态资源不加配置前缀(如不加: /res

4. 增加对原有路由全桥接,支持静态文件访问

代码示例:

  1. <dependency>
  2. <groupId>com.github.stonyshi</groupId>
  3. <artifactId>reactor-netty-jersey</artifactId>
  4. <version>1.0</version>
  5. </dependency>
  1. final Path resource = Paths.get(MainTest.class.getResource("/public").toURI());
  2. HttpServer.create(opts -> opts.port(8084))
  3. .startAndAwait(JerseyBasedHandler.builder()
  4. .property(ServerProperties.TRACING, "ON_DEMAND")
  5. .property(ServerProperties.TRACING_THRESHOLD, "SUMMARY")
  6. .register(JacksonProvider.class)
  7. .packages("com.stony.controllers")
  8. .appName("my-app-web")
  9. .withRouter(routes -> {
  10. routes.get("/v10/get", (req, resp) -> resp.sendString(Mono.just("asdfasdf")))
  11. .get("/v10/get2", (req, resp) -> {
  12. logger.info("json enter");
  13. return resp.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
  14. .sendString(Mono.just(JSON.toJSONString(new UserTest("che", 10, "blue"))));
  15. })
  16. .directory("/res", resource)
  17. .get("/v9/get", (req, resp) -> {
  18. System.out.println(req.params());
  19. System.out.println(req.path());
  20. System.out.println(req.uri());
  21. System.out.println("id = " + req.param("id"));
  22. System.out.println("name = " + req.param("name"));
  23. return resp
  24. .header(HttpHeaderNames.CONTENT_TYPE, "text/plain;charset=utf-8")
  25. .chunkedTransfer(false)
  26. .sendString(Mono.just(req.params().toString()));
  27. });
  28. })
  29. .build()
  30. );
  31. public class UserTest {
  32. String name;
  33. int id;
  34. @JsonProperty("first_name")
  35. private String firstName;
  36. @JsonCreator
  37. public UserTest(@JsonProperty("name") String name, @JsonProperty("id") int id, @JsonProperty("first_name") String firstName) {
  38. this.name = name;
  39. this.id = id;
  40. this.firstName = firstName;
  41. }
  42. }
  43. @Path("/api")
  44. public class ApiController {
  45. @POST()
  46. @Path("/post")
  47. @Consumes(MediaType.APPLICATION_JSON)
  48. @Produces(MediaType.APPLICATION_JSON)
  49. public UserTest post(UserTest body) {
  50. System.out.println("body = " + body);
  51. return new UserTest("bai", 220, "li");
  52. }
  53. @GET()
  54. @Path("/json")
  55. @Consumes(MediaType.APPLICATION_JSON)
  56. @Produces(MediaType.APPLICATION_JSON)
  57. public UserTest json() {
  58. logger.info("json enter");
  59. return new UserTest("bai", 200, "li");
  60. }
  61. }
  62. curl -X POST \
  63. http://localhost:8084/api/post \
  64. -H 'cache-control: no-cache' \
  65. -H 'content-type: application/json' \
  66. -H 'postman-token: f202fc59-94b9-f0e3-307d-35cb0fe885d6' \
  67. -d '{"first_name":"li","name":"bai","id":200}'
  68. {
  69. "name": "bai",
  70. "id": 200,
  71. "first_name": "li"
  72. }

压测示例(没装wrk可以使用ab压测):

  1. wrk -H 'Connection: keep-alive' -t12 -c400 -d30s http://localhost:8084/v10/get
  2. Running 30s test @ http://localhost:8084/v10/get
  3. 12 threads and 400 connections
  4. Thread Stats Avg Stdev Max +/- Stdev
  5. Latency 18.51ms 88.31ms 1.65s 98.10%
  6. Req/Sec 4.38k 1.83k 16.25k 60.54%
  7. 1535312 requests in 30.10s, 150.81MB read
  8. Requests/sec: 51013.47
  9. Transfer/sec: 5.01MB
  10. wrk -H 'Connection: keep-alive' -t20 -c400 -d30s http://localhost:8084/api/json
  11. Running 30s test @ http://localhost:8084/api/json
  12. 20 threads and 400 connections
  13. Thread Stats Avg Stdev Max +/- Stdev
  14. Latency 54.04ms 9.31ms 142.88ms 71.88%
  15. Req/Sec 370.86 53.38 676.00 71.84%
  16. 221881 requests in 30.08s, 107.92MB read
  17. Socket errors: connect 0, read 221881, write 0, timeout 0
  18. Requests/sec: 7376.17
  19. Transfer/sec: 3.59MB