kumuluz / kumuluzee

Lightweight open-source framework for developing microservices using standard Java EE technologies and migrating Java EE to cloud-native architecture.
https://ee.kumuluz.com
MIT License
291 stars 71 forks source link

jax rs gzip support #184

Closed eriskooo closed 3 years ago

eriskooo commented 3 years ago

hello,

please - is there direct gzip support for rest server / client side ? something like @GZIP ?

thank you,

e.

gpor0 commented 3 years ago

Hello @eriskooo

Did you check the gzip config options as provided in documentation https://github.com/kumuluz/kumuluzee/wiki/EeConfig ?

eriskooo commented 3 years ago

Hi, yes, but - I don't see an option for custom GET enable (all I need is to use gzip in one GET with huge load) - same there is no example for client side, is it automatical - or I need to write and register interceptor ?

gzip:
  enabled: false
  min-gzip-size: [integer]
  included-methods: [list]
  included-mime-types: [list]
  excluded-mime-types: [list]
  excluded-agent-patterns: [list]
  excluded-paths: [list]
  included-paths: [list]
gpor0 commented 3 years ago

Following config should work for method GET /store/api/system

gzip:
      enabled: true
      included-methods: [GET]
      included-paths: [/store/api/system]

Keep in mind that client must tell the server that requester understand gzip using Accept-Encoding header. Example Accept-Encoding: gzip, deflate, br

see https://tools.ietf.org/html/rfc7231#section-5.3.4

eriskooo commented 3 years ago

Hi,

kumuluzee: gzip: enabled: true included-methods: [GET] included-paths: [/api/v1/payment-groups/failed-payment-group]

curl -X GET "http://localhost:8080/api/v1/payment-groups/failed-payment-group" -H "Accept-Encoding: compress, gzip" -H "accept: application/json"

with/without last header return always json representation ;(

Thnx,

e.

gpor0 commented 3 years ago

Hi,

i think you have gzip configuration on the wrong path. It should be:

kumuluzee:
    server:
        gzip:
            enabled: true
            included-methods: [GET]
            included-paths: [/api/v1/payment-groups/failed-payment-group]

This is the expected (example) output of curl. See the difference in Content-Length response header.

gregor@Gregors-MacBook-Pro ~ % curl -X GET "http://localhost:8070/store/api/system" -H "Accept: application/json" -v                                     
Note: Unnecessary use of -X or --request, GET is already inferred.
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8070 (#0)
> GET /store/api/system HTTP/1.1
> Host: localhost:8070
> User-Agent: curl/7.64.1
> Accept: application/json
> 
< HTTP/1.1 200 OK
< Date: Mon, 16 Nov 2020 11:34:33 GMT
< X-Powered-By: KumuluzEE/3.11.0
< Content-Type: application/json
< X-Backend: Store
< X-Backend-Version: 1.2.0-RC6
< X-Backend-Env: DEV
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS,HEAD
< Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
< Access-Control-Expose-Headers: X-Total-Count
< X-Processed-Millis: 4
< Vary: Accept-Encoding, User-Agent
< Content-Length: 213
< Server: Jetty(9.4.28.v20200408)
< 
* Connection #0 to host localhost left intact
{"container":null,"appServer":"KumuluzEE 3.11.0","appName":"Store","appVersion":"1.2.0-RC6","commitId":"af360986","buildOn":"Gregors-MacBook-Pro.local","buildTime":"16 Nov 2020 11:30 UTC","environment":"DEV"}* Closing connection 0
gregor@Gregors-MacBook-Pro ~ % 
gregor@Gregors-MacBook-Pro ~ % 
gregor@Gregors-MacBook-Pro ~ % 
gregor@Gregors-MacBook-Pro ~ % 
gregor@Gregors-MacBook-Pro ~ % curl -X GET "http://localhost:8070/store/api/system" -H "Accept-Encoding: compress, gzip" -H "Accept: application/json" -v
Note: Unnecessary use of -X or --request, GET is already inferred.
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8070 (#0)
> GET /store/api/system HTTP/1.1
> Host: localhost:8070
> User-Agent: curl/7.64.1
> Accept-Encoding: compress, gzip
> Accept: application/json
> 
< HTTP/1.1 200 OK
< Date: Mon, 16 Nov 2020 11:34:41 GMT
< X-Powered-By: KumuluzEE/3.11.0
< Content-Type: application/json
< X-Backend: Store
< X-Backend-Version: 1.2.0-RC6
< X-Backend-Env: DEV
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS,HEAD
< Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
< Access-Control-Expose-Headers: X-Total-Count
< X-Processed-Millis: 4
< Vary: Accept-Encoding, User-Agent
< Content-Encoding: gzip
< Content-Length: 184
< Server: Jetty(9.4.28.v20200408)
< 
Warning: Binary output can mess up your terminal. Use "--output -" to tell 
Warning: curl to output it to your terminal anyway, or consider "--output 
Warning: <FILE>" to save to a file.
* Failed writing body (0 != 184)
* Closing connection 0
gregor@Gregors-MacBook-Pro ~ % 

More information about gzip and Jetty: http://www.eclipse.org/jetty/documentation/current/gzip-filter.html

eriskooo commented 3 years ago

excellent ! many thanks for your support, have a nice day !

e.

gpor0 commented 3 years ago

Great.

One more thing!

You need to know that Jettys gzip filter (when enabled) adds Vary Header to each response (whether you request gzip or not). Vary: Accept-Encoding, User-Agent I don't know why, but for some reason Jetty adds User-Agent value to Vary response header causing proxy caches to cache content per requester (client, browser, os, etc.). This may result in huge amount of cache misses or cache size. If you don't use proxy caches you are good to go.