spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.51k stars 3.31k forks source link

How to monitor client disconnection #3490

Open javaMikes opened 1 month ago

javaMikes commented 1 month ago

spring cloud gateway version: 2.2.0

I wanto monitor client disconnection and record log, but i can't find a valid way to do this.

I will be gradeful if find the solution!

ctlove0523 commented 1 week ago

you can write your owner WebServerFactoryCustomizer like this

@Component
public class Customizer implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
    @Override
    public void customize(NettyReactiveWebServerFactory factory) {
        factory.addServerCustomizers(new NettyServerCustomizer() {
            @Override
            public HttpServer apply(HttpServer httpServer) {
                return httpServer.childObserve(new ConnectionObserver() {
                    @Override
                    public void onStateChange(Connection connection, State newState) {
                        if (newState == State.DISCONNECTING) {
                            System.out.println("disconnecting");
                        }
                        if (newState == State.CONNECTED) {
                            System.out.println("connected");
                        }
                    }
                });
            }
        });
    }
}