Search in sources :

Example 1 with DefaultHttpServerMetrics

use of org.apache.servicecomb.foundation.vertx.metrics.DefaultHttpServerMetrics in project java-chassis by ServiceComb.

the class RestServerVerticle method start.

@Override
public void start(Promise<Void> startPromise) throws Exception {
    try {
        super.start();
        // 如果本地未配置地址,则表示不必监听,只需要作为客户端使用即可
        if (endpointObject == null) {
            LOGGER.warn("rest listen address is not configured, will not start.");
            startPromise.complete();
            return;
        }
        Router mainRouter = Router.router(vertx);
        mountAccessLogHandler(mainRouter);
        mountCorsHandler(mainRouter);
        initDispatcher(mainRouter);
        mountGlobalRestFailureHandler(mainRouter);
        HttpServer httpServer = createHttpServer();
        httpServer.requestHandler(mainRouter);
        httpServer.connectionHandler(connection -> {
            DefaultHttpServerMetrics serverMetrics = (DefaultHttpServerMetrics) ((ConnectionBase) connection).metrics();
            DefaultServerEndpointMetric endpointMetric = serverMetrics.getEndpointMetric();
            long connectedCount = endpointMetric.getCurrentConnectionCount();
            int connectionLimit = DynamicPropertyFactory.getInstance().getIntProperty("servicecomb.rest.server.connection-limit", Integer.MAX_VALUE).get();
            if (connectedCount > connectionLimit) {
                connection.close();
                endpointMetric.onRejectByConnectionLimit();
            }
        });
        List<HttpServerExceptionHandler> httpServerExceptionHandlers = SPIServiceUtils.getAllService(HttpServerExceptionHandler.class);
        httpServer.exceptionHandler(e -> {
            if (e instanceof ClosedChannelException) {
                // This is quite normal in between browser and ege, so do not print out.
                LOGGER.debug("Unexpected error in server.{}", ExceptionUtils.getExceptionMessageWithoutTrace(e));
            } else {
                LOGGER.error("Unexpected error in server.{}", ExceptionUtils.getExceptionMessageWithoutTrace(e));
            }
            httpServerExceptionHandlers.forEach(httpServerExceptionHandler -> {
                httpServerExceptionHandler.handle(e);
            });
        });
        startListen(httpServer, startPromise);
    } catch (Throwable e) {
        // vert.x got some states that not print error and execute call back in VertexUtils.blockDeploy, we add a log our self.
        LOGGER.error("", e);
        throw e;
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) DefaultHttpServerMetrics(org.apache.servicecomb.foundation.vertx.metrics.DefaultHttpServerMetrics) Endpoint(org.apache.servicecomb.core.Endpoint) DefaultServerEndpointMetric(org.apache.servicecomb.foundation.vertx.metrics.metric.DefaultServerEndpointMetric)

Aggregations

HttpServer (io.vertx.core.http.HttpServer)1 Router (io.vertx.ext.web.Router)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Endpoint (org.apache.servicecomb.core.Endpoint)1 DefaultHttpServerMetrics (org.apache.servicecomb.foundation.vertx.metrics.DefaultHttpServerMetrics)1 DefaultServerEndpointMetric (org.apache.servicecomb.foundation.vertx.metrics.metric.DefaultServerEndpointMetric)1