Eclipse 4.23.0 连接拒绝主机:localhost;嵌套异常是:java.net.ConnectException:连接被拒绝:连接

Eclipse 4.23.0 Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused: connect

我正在研究 Freeman 和 Robson 编写的 Head First Design Patterns 第 2 版中的代理模式部分。我是 运行 Windows 10,我确定本地主机正常运行,因为当我导航到本地主机或 http://127.0.0.1/. rmiregistry is started beforehand from the cmd line with no issue. I'm running the test with the firewall turned off and I'm still getting the error stack trace listed below. I'm executing the GumballMachineTestDrive class directly in Eclipse by right clicking on the .java file and selecting "Run As" "Java Application". The screenshot below shows the run configurations for GumballMachineTestDrive, where args[0] == "localhost" and args 1 == 112 时出现 Windows IIS 屏幕。我已 ping localhost 和 127.0.0.1,两者均未显示任何问题。我之前尝试设置 rmi 服务器主机名,如“System.setProperty(java.rmi.server.hostname”, “127.0.0.1”); 行所示,但这并没有什么不同。任何正确方向的指导将不胜感激。

服务代码:

package GumballMachineStatePattern;

import java.rmi.Naming;

public class GumballMachineTestDrive {

    public static void main(String[] args) {
        GumballMachineRemote gumballMachine = null;
        
    //  System.setProperty("java.rmi.server.hostname", "127.0.0.1");
        
        int count;
        
        if (args.length < 2) {
            System.out.println("GumballMachine <name> <inventory>");
            System.exit(1);
        }
        
        try {
        
        count = Integer.parseInt(args[1]);
        
        gumballMachine = new GumballMachine(args[0], count);
        Naming.rebind("//" + args[0] + "/gumballmachine", gumballMachine);
        
        } catch (Exception e) {
            e.printStackTrace();
        }
}
}

GumballMachine 代码 class:

package GumballMachineStatePattern;
import java.rmi.*;
import java.rmi.server.*;

public class GumballMachine extends UnicastRemoteObject implements GumballMachineRemote {
    
    private static final long serialVersionUID = 2L;

State soldOutState;
State noQuarterState;
State hasQuarterState;
State soldState;
State winnerState;

State state = soldOutState;
int count = 0;

String location;
    
    public GumballMachine(String location, int count) throws RemoteException {
        soldOutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);
        
        this.location = location;
        
        this.count = count;
        if (count > 0) {
            state = noQuarterState;
        } else {
            state = soldOutState;
        }
    }
    
    public void insertQuarter() {
        state.insertQuarter();
    }
    
    public void ejectQuarter() {
        state.ejectQuarter();
    }
    
    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }
    
    void setState(State state) {
        this.state = state;
    }
    
    void releaseBall() {
        System.out.println("A gumball comes rolling out of the slot...");
        if (count > 0) {
            count = count - 1;
        }
    }
    
    public int getCount() {
        return count;
    }
    
    void refill(int count) {
        this.count += count;
        System.out.println("The gumball machine was just refilled; its new count is: " + this.count);
        state.refill();
    }
    
    public State getState() {
        return state;
    }
    
    public State getSoldOutState() {
        return soldOutState;
    }
    
    public State getNoQuarterState() {
        return noQuarterState;
    }
    
    public State getHasQuarterState() {
        return hasQuarterState;
    }
    
    public State getSoldState() {
        return soldState;
    }
    
    public State getWinnerState() {
        return winnerState;
    }
    
    public String getLocation() {
        return location;
    }
        
    public String toString() {
        StringBuffer result = new StringBuffer();
        result.append("\nMighty Gumball, Inc.");
        result.append("\nJave-enabled Standing Gumball Model #2004\n");
        result.append("Inventory: " + count + " gumball");
        
        if (count != 1) {
            result.append("s");
        }
        result.append("\n");
        result.append("Machine is " + state + "\n");
        
        return result.toString();
    }
}

GumballMachineTestDrive 堆栈跟踪,其中 args[0] == "localhost" 和 args1 = 112

java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused: connect
    at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:626)
    at java.rmi/sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:217)
    at java.rmi/sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:204)
    at java.rmi/sun.rmi.server.UnicastRef.newCall(UnicastRef.java:344)
    at java.rmi/sun.rmi.registry.RegistryImpl_Stub.rebind(RegistryImpl_Stub.java:150)
    at java.rmi/java.rmi.Naming.rebind(Naming.java:177)
    at GumballMachineStatePattern.GumballMachineTestDrive.main(GumballMachineTestDrive.java:24)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:579)
    at java.base/sun.nio.ch.Net.connect(Net.java:568)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at java.base/java.net.Socket.connect(Socket.java:583)
    at java.base/java.net.Socket.<init>(Socket.java:507)
    at java.base/java.net.Socket.<init>(Socket.java:287)
    at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createSocket(TCPDirectSocketFactory.java:40)
    at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:620)
    ... 6 more

我解决了这个问题。我需要 re-do 项目作为一个新的 java 项目来清除可能存在的一些依赖冲突。它也是一个很好的学习课程 rmi 和 xampp 本地服务器配置。