java核心技术卷二——Java分布式RMI实现

1.到github下载NanoHTTPD源码(直接百度)

,实现NanoHTTPD抽象类,源码中有示例程序直接启动示例程序即可,启动后效果如图所示:
cmd命令行启动效果
在这里插入图片描述
IE浏览器访问服务器效果:
在这里插入图片描述注:在本机模拟分布式,以及在本机和虚拟机模拟分布式,没有启动服务器也没有问题可以访问到远程方法,博友可以自己测试以下,只需要把客户端的loaclhost改为虚拟机的地址,关闭虚拟机防火墙或者开放相关端口的访问权限即可。

2.在cmd命令界面启动RMI注册表,如图所示:

在这里插入图片描述


3.启动远程服务


1.代码结构如图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190719151926897.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3F3ZTg4NTE2Nzc1OQ==,size_16,color_FFFFFF,t_70)运行的是default package包中的WarehouseServer主程序(也可以把default package包中的程序放到其他包)

需要注意的是服务端文件的包名,在后面会详细说明
Warehouse接口代码

import java.rmi.*;

/**
   The remote interface for a simple warehouse.
   @version 1.0 2007-10-09
   @author Cay Horstmann
*/
public interface Warehouse extends Remote
{  
   double getPrice(String description) throws RemoteException;
}

WarehouseImpl代码

import java.rmi.*;
import java.rmi.server.*;
import java.util.*;

/**
 * This class is the implementation for the remote Warehouse interface.
 * @version 1.0 2007-10-09
 * @author Cay Horstmann
 */
public class WarehouseImpl extends UnicastRemoteObject implements Warehouse
{
   public WarehouseImpl() throws RemoteException
   {
      prices = new HashMap<String, Double>();
      prices.put("Blackwell Toaster", 24.95);
      prices.put("ZapXpress Microwave Oven", 49.95);
   }

   public double getPrice(String description) throws RemoteException
   {
      Double price = prices.get(description);
      return price == null ? 0 : price;
   }

   private Map<String, Double> prices;
}

WarehouseServer代码

import java.rmi.*;
import javax.naming.*;

/**
 * This server program instantiates a remote warehouse object, registers it with the naming
 * service, and waits for clients to invoke methods.
 * @version 1.12 2007-10-09
 * @author Cay Horstmann
 */
public class WarehouseServer
{
   public static void main(String[] args) throws RemoteException, NamingException
   {
      System.out.println("Constructing server implementation...");
      WarehouseImpl centralWarehouse = new WarehouseImpl();

      System.out.println("Binding server implementation to registry...");
      Context namingContext = new InitialContext();
      namingContext.bind("rmi:central_warehouse", centralWarehouse);

      System.out.println("Waiting for invocations from clients...");
   }
}

 2.在WarehouseServer类所在目录下执行:java -Djava.rmi.server.codebase=http://localhost:8080/ WarehouseServer
 效果图如下:

在这里插入图片描述

4.客户端发送请求:

客户端目录下只有中两个文件
客户端结构在这里插入图片描述
客户端(WarehouseClient)代码如下:

import java.io.PrintStream;
import java.rmi.RemoteException;
import java.util.Enumeration;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

public class WarehouseClient
{
  public static void main(String[] paramArrayOfString)
    throws NamingException, RemoteException
  {
    InitialContext localInitialContext = new InitialContext();

    System.out.print("RMI registry bindings: ");
    NamingEnumeration localNamingEnumeration = localInitialContext.list("rmi://localhost/");
    while (localNamingEnumeration.hasMoreElements()) {
      System.out.println(((NameClassPair)localNamingEnumeration.nextElement()).getName());
    }
    String str1 = "rmi://localhost/central_warehouse";
    Warehouse localWarehouse = (Warehouse)localInitialContext.lookup(str1);

    String str2 = "Blackwell Toaster";
    double d = localWarehouse.getPrice(str2);
    System.out.println(str2 + ": " + d);
  }
}

启动客户端:
在这里插入图片描述整体效果图

在这里插入图片描述
:服务端包名和客户端包名问题,如果服务端程序和客户端程序所在包名不一致在启动服务端程序时不会报错,但是在客户端请求服务端的方法时会报错((no security manager: RMI class loader disabled)),如下图:
在这里插入图片描述测试时服务端目录结构如下,在这里插入图片描述
服务端的包名都为server,客户端代码同上

疑问:书上说时要创建一个download目录把客户端和服务端公共的接口放到服务器中,在测试时没有把公共接口放到服务器中,测试也没有出现问题,搞不懂这个时干嘛用的;我在虚拟机和我本机模拟分布式时,没有启动服务器访问也正常 没有出现问题 ,不知道这个服务器是干嘛用的,如果有博友知道,请留言到评论区。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值