����λ�ã���ҳ > �����̳� > �̳� > Dubbo��ܵ�1��������Ƶ�

Dubbo��ܵ�1��������Ƶ�

��Դ������������|��ʱ�䣺2024-09-14 09:54:51 |���Ķ���154��|�� ��ǩ�� ���� ��� �� |����������

Java����Ҫ˵�����������RPC��ܵ���Dubbo��ԭ�������࣬�����������ҵĻ�������Զ�̵������������Ƶú���������

Java����Ҫ˵�����������RPC��ܵ���Dubbo��ԭ�������࣬�����������ҵĻ�������Զ�̵������������Ƶú���������

1��Dubbo�ŵ�϶࣬��ֻ������һ

1.1���ŵ�

ҵ�ڶ���΢����֮����õĿ��ѡ��϶࣬������Spring Cloud��Rest��ʽ �� Dubbo��ʽ����ʹ��Dubbo��ʽ�ӶࡣDubbo��ҵ�����ã��ȶ��ָ�Ч�����ܸ���˾�з�ͬѧ��ϲ����

Dubbo���ŵ�϶࣬���磺

  • ������ ��Dubbo ʹ�õ��ǻ��� Netty ���Զ���ͨ��Э�飬�ṩ�˸�Ч�Ķ��������ݴ��䣬ʹ��Զ�̷���������ܴ��������
  • ģ�黯��� ��Dubbo �ļܹ��dz�ģ�黯����Ҫ��������ģ����ɣ�Զ�̵���ģ�飨RPC������Ⱥģ�顢���ؾ���ģ�顢�ݴ�ģ���ע������ģ�顣
  • ÿ��������֧�ֶ�Э�� ��ÿ��������֧�ֶ���Э�飬����ע�����ģ�֧��ZK��Redis��Nacos�ȵȡ�
  • ���ؾ�����ݴ� ��Dubbo �ṩ�˶����ݴ����ƣ�����ʧ�����ԡ�ʧ��ת�Ƶȡ���֧�ֶ��ָ��ؾ��⣬�����������ѯ��һ���Թ�ϣ�ȡ�
  • ����ע��ͷ��� ��Dubbo������ע�����ĵĸ��ʵ���˷�����Զ�ע��ͷ��֡�
  • SPI ��չ���� ���� ���˹��� �����£�Dubbo���ἰ���ľ���ʹ��������Java��SPI���ƣ��������չ�ԣ���һ�����߼������߼��ǰɡ�

1.2��������һ

���ǣ�Dubbo�������˵ģ� ��֧�� ���÷�����������RPC���á�Dubbo�Ķ�λ��һ��RPC��ܣ��������ĺ��ĺ�����֮�أ�����Dubbo��RPC�ĵ��ù���͸������ʹ�ÿ����߿���רע��ҵ���߼��������ù�ע�ײ�ͨ�����⡣

һ��RPC���ֻ�о۽�������������RPC���ù������ģ�飬�Ż����˹�ע��������ŵ㶼������֮����������������

���߽�RPC���õ�������̣������ һ��Э����Ϣ�Ĵ������ ����ͨ�� ���ƺ��̵߳ĵȴ��ͻ��� ����ʵ��Զ�̷������á���һ���˼·�������������������ߵ��ǻۡ�

2��RPC����ʾ��

ѧDubbo�����Ⱦ���Ҫѧϰ����������������˼·�����ڴˣ���ʵ��һ�����׵�Զ�̷������ã���Dubbo��RPC���̼��׻���

2.1��ʾ������

���׵�RPC���̲������£����·�5��������ʹ��Netty����SocketͨѶ���ߡ�

  1. ʹ��2��Java������ģ��2��ϵͳ֮��ĵ��ã�A���� �� B���̡�
  2. A���̵�ij��������ʹ�������������B���̵�ij��������
  3. Ȼ��A���̵ķ����ʹ��ڵȴ�״̬��
  4. ��B���̵ķ���ִ����֮������������֪ͨ��A���̡�
  5. Ȼ��A���̵ķ��������ѣ���������ִ�С�

Dubbo¿ò¼ÜµÄ1¸öºËÐÄÉè¼Æµã

2.2��ʾ������

  • B������Ϊ����ˣ������������
public class BProcessServer {
    private final int port;
    public BProcessServer(int port) {
        this.port = port;
    }

    public void start() throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ch.pipeline().addLast(new BProcessServerHandler());
                        }
                    });

            ChannelFuture future = bootstrap.bind(port).sync();
            System.out.println("B�����˷��񣬶˿ں�: " + port);
            future.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new BProcessServer(8088).start();
    }
}
  • B���̽���������������������л�֮��ִ�ж�Ӧ�ķ������ٽ�ִ�н�����أ�
public class BProcessServerHandler extends SimpleChannelInboundHandler {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        String reqData = msg.toString(CharsetUtil.UTF_8);
        System.out.println("B���̽��ܵ�����������: " + reqData);

        executeMethod(ctx);
    }

    /**
     * ִ�з���
     *
     * @param ctx
     * @throws InterruptedException
     */
    private void executeMethod(ChannelHandlerContext ctx) throws InterruptedException {
        // TODO ��������Ϣ����ij�ֹ�������ɷ����������������ȣ���ʵ���Ƿ����л��Ĺ��̡�
        System.out.println("�Խ��ܵ������������л���Ȼ��ʼִ�� ��Ϣ����ָ���ķ���...");

        // ģ�ⷽ��ִ��
        Thread.sleep(2000);
        System.out.println("ִ����ϣ����ؽ��...");

        // ����� ֪ͨ�� A ����
        ByteBuf dataByteBuf = ctx.alloc().buffer().writeBytes("Task completed".getBytes(CharsetUtil.UTF_8));
        ctx.writeAndFlush(dataByteBuf);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}
  • A��������Netty�ͻ��ˣ�������B���̵�ͨ�ţ�Ȼ����Զ�̵��ã����ڵȴ�״̬��
public class AProcessClient {

    private final String host;
    private final int port;
    private final Object lock = new Object();  // ����������

    public AProcessClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void start() throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ch.pipeline().addLast(new AProcessClientHandler(lock));
                        }
                    });

            ChannelFuture future = bootstrap.connect(host, port).sync();
            System.out.println("A������B���̽�����ͨ������");

            Channel channel = future.channel();

            // ����Զ�̵���
            callRemoteMethod(channel);

            channel.closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }

    /**
     * ִ�з���
     *
     * @param channel
     * @throws InterruptedException
     */
    private void callRemoteMethod(Channel channel) throws InterruptedException {
        //TODO �˴���Ҫ�����õķ����Ͳ���������Э��������л����������ʡȥ�˹��̡�
        System.out.println("A���̽� ����ķ����Ͳ��� �������л���Ȼ����B���̷����������...");

        ByteBuf dataByteBuf = channel.alloc().buffer().writeBytes("Start call method".getBytes(CharsetUtil.UTF_8));

        channel.writeAndFlush(dataByteBuf);

        // ʹ��wait�ȴ�B����֪ͨ
        synchronized (lock) {
            System.out.println("A���̵ȴ�B���̵���Ӧ...");
            lock.wait();  // �ȴ�֪ͨ
        }

        System.out.println("A�����յ���B���̵���Ӧ֪ͨ����������...");
    }

    public static void main(String[] args) throws InterruptedException {
        new AProcessClient("localhost", 8088).start();
    }
}
  • A���̽���B���̵���Ӧ��ͬʱ�����ѣ�Ȼ������ lock.wait() �Ժ�Ĵ�����Լ���ִ�С�
public class AProcessClientHandler extends SimpleChannelInboundHandler {

    private final Object lock;

    public AProcessClientHandler(Object lock) {
        this.lock = lock;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        String resData = msg.toString(CharsetUtil.UTF_8);
        System.out.println("A���̽��ܵ�����Ӧ����: " + resData);

        // B ����������ɣ�ʹ�� notify ���ѵȴ����߳�
        synchronized (lock) {
            lock.notify();  // ���� A ����
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}

3���ܽ�

Dubbo���������˼·�����࣬��ֻ������һ���Ǿ���RPC�ĵ��ù��̡�������һ�����׵�RPCԶ�̵��õ�ʾ������������Dubbo��ԭ����Դ�룬ϣ�������а�����

��ƪ��ᣡ��ӭ ��ע����V(yclxiao)������ȫ������(����Ա��֧��)

ԭ�����ӣ� https://mp.weixin.qq.com/s/J0fzDH-iqGnnnjqaXMLs-A

Dubbo¿ò¼ÜµÄ1¸öºËÐÄÉè¼Æµã

С���Ƽ��Ķ�

�������������Ľ�Ϊ������Ϣ����������������ͬ���޹۵��֤ʵ��������

ºËÐÄ
����
���ͣ���������������Ӫ״̬����ʽ��Ӫ�������ԣ� Ӣ�� ����

��Ϸ����

��Ϸ���

��Ϸ��Ƶ

��Ϸ����

��Ϸ�

�����ġ���CORE������Ϸ��FURYJAM���µ�һ�������������Σ���Ϸ�У���ҽ����Ʒ���İ�ɫС���ģ�������

�����Ƶ����

����

ͬ������

����

ɨ��ά�����������ֻ��汾��

ɨ��ά����������΢�Ź��ںţ�

��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]

��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������