Spring Boot部署Heroku遇Error R10启动超时问题求助
Hey there, let's get your Spring Boot app deployed to Heroku smoothly! That R10 timeout error is super common, and it's almost always because your app isn't binding to the dynamic port Heroku provides. Let's walk through the fixes step by step:
1. Use Heroku's Dynamic Port in Your Configuration
Heroku assigns a unique port via the $PORT environment variable—your app can't hardcode a port like 8080. Update your config files to respect this variable:
- For
application.properties:server.port=${PORT:8080} - For
application.yml:server: port: ${PORT:8080}
The :8080 part is a fallback for local testing, so you don't have to change anything when running your app on your machine.
2. Validate Your Procfile
If you have a Procfile (Heroku uses this to know how to start your app), make sure it's correctly set up without hardcoding ports. A standard Procfile for Spring Boot looks like this:
web: java -jar target/your-app-filename.jar
If you're using the Heroku Maven/Gradle plugins, the Procfile might be auto-generated, but double-check it doesn't have any port-specific flags.
3. Test Locally with the Port Override
Before deploying again, confirm your app can pick up the PORT variable locally:
- On Linux/macOS:
PORT=9090 ./mvnw spring-boot:run - On Windows (Command Prompt):
set PORT=9090 && mvn spring-boot:run
If your app starts on port 9090, you know it's correctly reading the environment variable.
4. Check for Programmatic Port Overrides
If you have any custom configuration classes that set the server port, make sure they pull from the environment instead of hardcoding. For example:
@Configuration public class ServerConfig { @Bean public ServletWebServerFactory servletContainer() { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); // Use Heroku's PORT or fall back to 8080 factory.setPort(Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"))); return factory; } }
Avoid hardcoding numbers here—always prioritize the environment variable.
5. Address Slow Startup (If Needed)
If your app takes longer than 90 seconds to start, Heroku will kill it. To fix this:
- Optimize startup time: Use lazy initialization for non-critical beans, trim unused dependencies, or pre-compile assets.
- Temporary fix: You can request a longer timeout from Heroku support, but it's better to fix the slow startup root cause.
Once you've made these changes, redeploy your app to Heroku—it should bind to the correct port and start up without hitting the timeout!
内容的提问来源于stack exchange,提问作者Stefan Falk

