Java中分布式案例的实现方法

//业务需求:
①一台计算机作为客户端向多台服务端计算机发送jar包和执行该jar包的命令,多台服务端计算机执行该jar包然后将结果返回给客户端计算机;
②本案例要做的是统计多台计算机上指定文件中单词的数量。
代码如下:
1. ConvertUtils自定义类

public class ConvertUtils {
    public static byte[] intToByte(int i) throws Exception{
        // 创建一个字节数组输出流,数据会被写到一个字节数组中
        ByteArrayOutputStream baos = new ByteArrayOutputStream();// 将基本 Java数据类型写入输出流中
        DataOutputStream dos = new DataOutputStream(baos);// 将基本 Java 数据类型写入输出流中
        dos.writeInt(i);// 最终的值被写入到了ByteArraOutputStream的字节数组中
        return baos.toByteArray();// 从字节数组输出流中取出字节数组
    }
    public static int byteToInt(byte[] buf) throws Exception{
        // 从指定的字节数组中读取数据的流
        ByteArrayInputStream bais = new ByteArrayInputStream(buf);
        // 数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型
        DataInputStream dis = new DataInputStream(bais);
        return dis.readInt();   
    }
    public static byte[] longToByte(long l) throws Exception{
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeLong(l);
        return baos.toByteArray();
    }`
    public static long byteToLong(byte[] buf) throws Exception{
        ByteArrayInputStream bais = new ByteArrayInputStream(buf);
        DataInputStream dis = new DataInputStream(bais);
        return dis.readLong();
    }
}

2.RuntimeDemo自定义类

public class RuntimeDemo {
    public static void main(String[] args) throws Exception {
        //在java代码中允许java命令
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("java -jar WordCount1.jar");
        InputStream in = process.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in, "gbk"));
        String result = br.readLine();
        System.out.println(result);
        br.close();
        process.destroy();
    }
}
  1. 客户端Client
public class Client {
    public static void main(String[] args) throws Exception {
        Socket s = new Socket("192.168.6.48", 8888);
        // 用于写出的缓冲字节输出流
        BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
        // 用于读取服务端返回结果的缓冲字符输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

        writeKey(bos);

        // 读取服务端返回结果
        String result = br.readLine();
        if(result.equals("1")){
            System.out.println("验证通过");

            // 写出指定服务端存放jar包的路径
            // 将存放jar包路径的字符串所占字节大小写出
            writePath(bos);

            // 将jar包写出到服务端
            // 将jar包所占的字节大小写出去
            writeJar(bos);

            // 读取服务端返回的结果,1表示保存成功,2表示保存失败
            result = br.readLine();
            if(result.equals("1")){
                // 保存成功
                System.out.println("保存jar包成功");

                writeCommand(bos);
                result = br.readLine();
                System.out.println(result);//也可以保存到文件中
            }
            else{
                System.out.println("保存jar包失败");
            }   
        }
        else{
            System.out.println("非法请求");
        }
        s.close();
    }

    private static void writeCommand(BufferedOutputStream bos) throws IOException, Exception {
        // 发送执行jar包的命令
        String command  = "java -jar D:/WordCount1.jar";
        byte[] bytes3 = command.getBytes();
        bos.write(ConvertUtils.intToByte(bytes3.length));
        bos.write(bytes3);
        bos.flush();
    }

    private static void writeJar(BufferedOutputStream bos) throws IOException, Exception, FileNotFoundException {
        File file = new File("D:/WordCount1.jar");
        bos.write(ConvertUtils.longToByte(file.length()));
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        int len = -1;
        byte[] buf = new byte[1024];
        while((len=bis.read(buf))!=-1){
            bos.write(buf, 0, len);
            bos.flush();
        }
    }

    private static void writePath(BufferedOutputStream bos) throws IOException, Exception {
        String path = "D:/java/WordCount1.jar";
        byte[] bytes2 = path.getBytes();
        // 将保存jar包的字符串以字节数组的方式写出
        bos.write(ConvertUtils.intToByte(bytes2.length));
        bos.write(bytes2);
        bos.flush();
    }

    private static void writeKey(BufferedOutputStream bos) throws IOException, Exception {
        String key = "#&*&@jfb248";
        byte[] bytes = key.getBytes();
        //先写key所占的字节数组大小写出去
        bos.write(ConvertUtils.intToByte(bytes.length));
        //再把byte字节数组内容写出去
        bos.write(bytes);
        bos.flush();
    }

}
  1. 服务端Sever
public class Sever {
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(8888);
        while (true) {
            Socket s = ss.accept();
            BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

            // 读取四个字节表示key所占的大小
            String key = readContent(bis);
            // 进行校验
            if (key.equals("#&*&@jfb248")) {
                System.out.println("验证通过了");
                writeResult(bw, "1");

                // 读取jar包保存路径
                String path = readContent(bis);
                System.out.println(path);

                // 保存jar包
                saveJar(bis, bw, path);

                // 读取执行jar包的命令
                String command = readContent(bis);
                Runtime runtime = Runtime.getRuntime();
                Process process = runtime.exec(command);
                InputStream in = process.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(in, "gbk"));
                String result = br.readLine();
                writeResult(bw, result);// 将结果返回给客户端

            } else {
                System.out.println("非法请求");
                bw.write("2");
                bw.newLine();
                bw.flush();
            }
        }
    }

    private static void saveJar(BufferedInputStream bis, BufferedWriter bw, String path) {
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(path));
            byte[] buf = new byte[8];
            bis.read(buf);
            long l = ConvertUtils.byteToLong(buf);// 已经知道了需要读取多少个字节表示jar包了
            if (l > 1024) {
                byte[] contentBuf = new byte[1024];
                int count = (int) (l / 1024);
                for (int i = 0; i < count; i++) {
                    rdContentBuf(bis, bos, contentBuf);
                }
                // 读写剩余的字节
                l = l - count * 1024;
                contentBuf = new byte[(int) l];
                rdContentBuf(bis, bos, contentBuf);
            } else {
                byte[] contentBuf = new byte[(int) l];// 有可能会丢失文件的长度
                rdContentBuf(bis, bos, contentBuf);
            }
            System.out.println("保存jar包成功");
            writeResult(bw, "1");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("保存失败");
            try {
                writeResult(bw, "2");
            } catch (IOException e1) {
                e1.printStackTrace();
                System.out.println("保存出现异常");
            }
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void rdContentBuf(BufferedInputStream bis, BufferedOutputStream bos, byte[] contentBuf)
            throws IOException {
        bis.read(contentBuf);
        bos.write(contentBuf);
        bos.flush();
    }

    private static void writeResult(BufferedWriter bw, String result) throws IOException {
        bw.write(result);
        bw.newLine();
        bw.flush();
    }

    private static String readContent(BufferedInputStream bis) throws IOException, Exception {
        byte[] buf = new byte[4];
        bis.read(buf);
        // 将buf转换成int类型的值
        int len = ConvertUtils.byteToInt(buf);// len表示key所占的字节大小
        byte[] contentBuf = new byte[len];
        bis.read(contentBuf);
        // 将字节数组转换成字符串
        String content = new String(contentBuf);
        return content;
    }
}

本文结束!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值