简体   繁体   中英

How can I produce a 503 error locally with Tomcat?

I am working on a project developed with Spring Boot using version 8.5.9 of Tomcat.

The application offers RESTfull web services in JAX RS. In the Prod environment, sometimes I get an error 503 Service not available , but I can not reproduce it because I use my local Mock.

Is there a way to reproduce the error locally? Like putting the TomCat on hold in unavailable for a moment?

According to the MDN , HTTP 503 Service Unavailable server error means :

... response code indicates that the server is not ready to handle the request.

The common causes are :

a server that is down for maintenance or that is overloaded.

To reproduce this error response in a natural way, you could so overloaded Tomcat.

To do it change the maximum number of simultaneous requests that can be handled by Tomcat.

If you use an embedded tomcat, set the server.tomcat.max-threads Spring Boot property with a weak value easily reachable such as :

server.tomcat.max-threads = 1

Otherwise, if you use our own Tomcat installation, set maxThreads with a weak value in the Connector element of the server.xml configuration file :

<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
           ...
           maxThreads="1"/>

You can mock it through WireMock using Java.

public static WireMockServer wireMockServer = new WireMockServer(8089);
wireMockServer.start();
    final WireMock wireMock = new WireMock(8089);
    wireMock.register(WireMock.get(WireMock.urlEqualTo("/conf"))
            .willReturn(WireMock.aResponse()
                    .withStatus(503)));

Hit localhost:8089/conf to get 503 Service Unavailable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM