ZooKeeper: java.rmi.server.ExportException: Port already in use: 0 - Troubleshooting and Solutions
This\u0020exception\u0020occurs\u0020when\u0020trying\u0020to\u0020export\u0020a\u0020remote\u0020object\u0020in\u0020Java\u0020RMI\u0020and\u0020the\u0020specified\u0020port\u0020is\u0020already\u0020in\u0020use\u0020by\u0020another\u0020process.\n\nIn\u0020RMI,\u0020when\u0020exporting\u0020a\u0020remote\u0020object,\u0020it\u0020needs\u0020to\u0020be\u0020bound\u0020to\u0020a\u0020specific\u0020port\u0020number\u0020so\u0020that\u0020clients\u0020can\u0020connect\u0020to\u0020it.\u0020If\u0020the\u0020specified\u0020port\u0020number\u0020is\u0020already\u0020in\u0020use\u0020by\u0020another\u0020process,\u0020this\u0020exception\u0020is\u0020thrown.\n\nTo\u0020resolve\u0020this\u0020issue,\u0020you\u0020can\u0020try\u0020the\u0020following\u0020steps:\n\n1.\u0020Make\u0020sure\u0020that\u0020no\u0020other\u0020process\u0020is\u0020already\u0020using\u0020the\u0020specified\u0020port.\u0020You\u0020can\u0020check\u0020this\u0020by\u0020running\u0020the\u0020netstat\u0020-ano\u0020|\u0020findstr\u0020<port>\u0020command\u0020on\u0020Windows\u0020or\u0020lsof\u0020-i\u0020:<port>\u0020command\u0020on\u0020Unix-based\u0020systems.\n\n2.\u0020If\u0020you\u0020find\u0020that\u0020another\u0020process\u0020is\u0020using\u0020the\u0020port,\u0020you\u0020can\u0020either\u0020stop\u0020that\u0020process\u0020or\u0020choose\u0020a\u0020different\u0020port\u0020number\u0020for\u0020your\u0020RMI\u0020application.\n\n3.\u0020If\u0020you\u0020are\u0020unable\u0020to\u0020stop\u0020the\u0020process\u0020using\u0020the\u0020port\u0020or\u0020need\u0020to\u0020use\u0020a\u0020specific\u0020port\u0020number,\u0020you\u0020can\u0020try\u0020changing\u0020the\u0020RMI\u0020registry\u0020port.\u0020Instead\u0020of\u0020using\u0020the\u0020default\u0020port\u0020(1099),\u0020you\u0020can\u0020specify\u0020a\u0020different\u0020port\u0020number\u0020using\u0020the\u0020java.rmi.server.RMISocketFactory.setSocketFactory()\u0020method\u0020before\u0020exporting\u0020the\u0020remote\u0020object.\n\nHere's\u0020an\u0020example\u0020of\u0020how\u0020to\u0020change\u0020the\u0020RMI\u0020registry\u0020port:\n\njava\nint\u0020rmiRegistryPort\u0020=\u00201099;\u0020//\u0020Specify\u0020your\u0020desired\u0020port\u0020number\nLocateRegistry.createRegistry(rmiRegistryPort);\nSystem.setProperty("java.rmi.server.hostname",\u0020"localhost");\u0020//\u0020Set\u0020the\u0020hostname\u0020if\u0020needed\nRMISocketFactory.setSocketFactory(new\u0020RMISocketFactory()\u0020{\n\u0020\u0020public\u0020Socket\u0020createSocket(String\u0020host,\u0020int\u0020port)\u0020throws\u0020IOException\u0020{\n\u0020\u0020\u0020\u0020return\u0020new\u0020Socket(host,\u0020port);\n\u0020\u0020}\n\u0020\u0020public\u0020ServerSocket\u0020createServerSocket(int\u0020port)\u0020throws\u0020IOException\u0020{\n\u0020\u0020\u0020\u0020if\u0020(port\u0020==\u00200)\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020port\u0020=\u0020rmiRegistryPort;\n\u0020\u0020\u0020\u0020}\n\u0020\u0020\u0020\u0020return\u0020new\u0020ServerSocket(port);\n\u0020\u0020}\n});\n\n\nBy\u0020using\u0020the\u0020RMISocketFactory\u0020and\u0020specifying\u0020a\u0020custom\u0020ServerSocket\u0020port,\u0020you\u0020can\u0020avoid\u0020the\u0020"Port\u0020already\u0020in\u0020use"\u0020exception.\n\nRemember\u0020to\u0020replace\u0020<port>\u0020with\u0020the\u0020actual\u0020port\u0020number\u0020you\u0020are\u0020using.\n\nIf\u0020you\u0020are\u0020using\u0020frameworks\u0020like\u0020Spring\u0020Boot\u0020or\u0020Apache\u0020Tomcat,\u0020make\u0020sure\u0020to\u0020check\u0020their\u0020configuration\u0020files\u0020and\u0020see\u0020if\u0020any\u0020other\u0020services\u0020are\u0020already\u0020using\u0020the\u0020specified\u0020port.
原文地址: https://www.cveoy.top/t/topic/qeV7 著作权归作者所有。请勿转载和采集!