{"record":{"id":"dd7af9d770f508c5","repo":"apache/shenyu","slug":"can-not-find-port-automatically","errorCode":null,"errorMessage":"can not find port automatically ! ","messagePattern":"can not find port automatically ! ","errorType":"exception","errorClass":"ShenyuException","httpStatus":null,"severity":"error","filePath":"shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/utils/PortUtils.java","lineNumber":65,"sourceCode":"     * @throws ShenyuException when can not find port\n     */\n    @SuppressWarnings(\"all\")\n    public static int findPort(final BeanFactory beanFactory) {\n        try {\n            //works fine for springboot 2.x\n            return getPort(beanFactory, \"org.springframework.boot.web.server.AbstractConfigurableWebServerFactory\");\n        } catch (Exception ignored) {\n        }\n        try {\n            //works fine for springboot 1.x\n            return getPort(beanFactory, \"org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer\");\n        } catch (Exception ignored) {\n        }\n        try {\n            //works for springmvc with external tomcat\n            return PortUtils.getPort();\n        } catch (Exception e) {\n            throw new ShenyuException(\"can not find port automatically ! \");\n        }\n    }\n\n    /**\n     * get container port.\n     *\n     * @param beanFactory beanFactory\n     * @param className   className\n     * @return container port\n     */\n    private static int getPort(final BeanFactory beanFactory, final String className) throws ClassNotFoundException,\n            NoSuchMethodException, InvocationTargetException, IllegalAccessException {\n        final Class<?> clazz = Class.forName(className);\n        final Method method = clazz.getMethod(\"getPort\");\n        final Object bean = beanFactory.getBean(clazz);\n        return (int) method.invoke(bean);\n    }\n","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/utils/PortUtils.java#L47-L83","documentation":"PortUtils.findPort tries several strategies to auto-detect the local HTTP port of the running client (Spring WebFlux/Netty via application env, external Tomcat via MBeans). If every strategy throws, it wraps the last failure in a ShenyuException because the client cannot register a valid URI without a port.","triggerScenarios":"Calling findPort when: the Spring environment has no `local.server.port`/`local.management.port` property, the app is not a web app (no WebServerApplicationContext), and no external Tomcat Connector MBean exists — all getPort strategies fail.","commonSituations":"Non-web Shenyu client starting without an embedded server; running in a container where the port property isn't propagated; using a server stack PortUtils doesn't recognize (e.g. Jetty/Undertow without Tomcat MBeans); unit tests without a real web server.","solutions":["Set the port explicitly in client config (e.g. `server.port: 8189` and matching `shenyu.client...port`) so detection isn't needed.","Ensure the app actually starts a web server (spring-boot-starter-web/webflux present) before registration runs.","If using an external Tomcat, confirm exactly one Connector MBean is exposed (see error 109) — multiple instances also break detection.","Register metadata manually or pin `shenyu.client.<framework>.props.port` in yml instead of auto-detection."],"exampleFix":"# before (port auto-detect fails)\nshenyu:\n  client:\n    http:\n      props:\n        contextPath: /demo\n# after (explicit port)\nserver:\n  port: 8189\nshenyu:\n  client:\n    http:\n      props:\n        contextPath: /demo\n        port: 8189","handlingStrategy":"try-catch","validationCode":"boolean canDetectPort = env.containsProperty(\"local.server.port\")\n        || env.containsProperty(\"server.port\")\n        || isExternalTomcatPresent();","typeGuard":null,"tryCatchPattern":"int port;\ntry {\n    port = PortUtils.findPort();\n} catch (ShenyuException e) {\n    port = env.getProperty(\"server.port\", Integer.class, 8189);\n    LOGGER.warn(\"port auto-detect failed ({}), using configured port {}\", e.getMessage(), port);\n}","preventionTips":["Always configure server.port and the matching shenyu client port in yml.","Ensure a real web server starts in the app (not just in tests).","Don't rely on auto-detection under containers/custom server stacks."],"tags":["port","client-registration","config"],"backgroundTag":"missing-config-value","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}