初学分布式

分布式系统是指由多台计算机通过网络连接,协同工作以完成共同任务的系统。其核心目标是通过并行处理提升性能、增强可靠性和扩展性。

1. 核心概念

节点:系统中的独立计算机或服务器。

通信:节点通过网络进行数据交换。

并发:多个节点同时执行任务。

一致性:确保所有节点的数据状态一致。

容错:系统在部分节点故障时仍能正常运行。

2. 优势

可扩展性:通过增加节点提升系统处理能力。

高可用性:部分故障不影响整体服务。

资源共享:节点间共享计算和存储资源。

性能提升:并行处理加快任务完成速度。

3. 常见架构

客户端-服务器:客户端请求服务,服务器提供资源。

对等网络(P2P):所有节点平等,既提供也使用资源。

微服务:将应用拆分为多个独立服务,各自运行。

以下是我自己写的一个简单的分布式程序:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        int numNodes = 5;

        // 初始化节点
        List<Node> nodes = new ArrayList<>();
        for (int i = 1; i <= numNodes; i++) {
            boolean isCoordinator = (i == numNodes - 1);
            Node node = new Node(i, isCoordinator);
            nodes.add(node);
        }


        /**for (int i = numNodes; i >= 1; i--) {
            int finalI = i;
            new Thread(() -> {
                Coordinator coordinator = new Coordinator(finalI - 1, 5000);
                coordinator.start();
                coordinator.requestResource();
                coordinator.run();
                coordinator.releaseResource();
            }).start();
         **/


            // 启动线程运行每个节点的逻辑
            for (Node node : nodes) {
                new Thread(() -> {

                    node.start();
                    node.requestResource();
                    node.run();
                    node.releaseResource();
                }).start();

            }
        }}




import java.util.List;

public class BullyElectionAlgorithm implements ElectionAlgorithm {
    private List<Node> nodes;

    public void startElection(int nodeId) {
        // implement bully election algorithm
    }
}

public class Coordinator extends Node {
    private final ElectionAlgorithm electionAlgorithm;
    public Coordinator(int nodeId) {
        super(nodeId, true);
        this.electionAlgorithm = new BullyElectionAlgorithm();
    }

    public void run() {
        // do something as coordinator
    }

    public void startElection() {
        electionAlgorithm.startElection(this.getNodeId());
    }
}

public interface ElectionAlgorithm {
    void startElection(int nodeId);
}

public class Node {
    private final int nodeId;
    private final boolean isCoordinator;
    private boolean isAlive;
    private final ResourceLockManager lockManager;


    public Node(int nodeId, boolean isCoordinator) {
        this.nodeId = nodeId;
        this.isCoordinator = isCoordinator;
        this.isAlive = true;
        this.lockManager = new ResourceLockManager();

    }

    public int getNodeId() {
        return nodeId;
    }

    public boolean isCoordinator() {
        return isCoordinator;
    }

    public boolean isAlive() {
        return isAlive;
    }

    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }

    public void requestResource() {


        /**try {
            // 连接到其他节点的地址和端口
            Socket socket = new Socket("其他节点的IP地址",port);

            // 获取输出流,向其他节点发送请求
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(("资源请求来自节点" + nodeId).getBytes());
            outputStream.flush();

            // 获取输入流,接收其他节点的响应
            InputStream inputStream = socket.getInputStream();
            byte[] buffer = new byte[1024];
            int length = inputStream.read(buffer);
            String response = new String(buffer, 0, length);
            System.out.println("节点" + nodeId + "收到响应:" + response);

            // 关闭连接
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }**/

        lockManager.lock();
        System.out.println("节点" + nodeId + "请求资源");
        // do something with the resource
    }

    public void releaseResource() {

            // do something with the resource

            System.out.println("节点" + nodeId + "释放资源\n");

            lockManager.unlock();

    }

    public void start() {
        System.out.println("节点" + nodeId + "启动...");

    }


    public void run() {
        System.out.println("节点"+nodeId+"运行");
    }
}

public class ResourceLockManager {
    private boolean locked = false;

    public synchronized void lock() {
        while (locked) {
            try {
                wait();
            } catch (InterruptedException e) {
                // handle exception
            }
        }
        locked = true;
    }

    public synchronized void unlock() {
        locked = false;
        notifyAll();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值