首页 > 其他分享 >230101_50_RPC底层原理

230101_50_RPC底层原理

时间:2023-01-01 23:00:58浏览次数:66  
标签:bill name 230101 50 id RPC import com public

1.RPC

代码参考学习:https://www.bilibili.com/video/BV17Z4y1s7cG?p=1&vd_source=e4f205f9f8b63c316aab97e0421a77a9
1.1 RPC,remote procedure call,远程过程调用,它本身是一个概念,它不是一个协议,也不是一个框架,是一个概念性的东西,这是远程通信的一种方式;

1.2 从单机到分布式 -> 分布式通信->底层原理实现:二进制数据传输 TCP/IP

package com.bill.rpc.common;

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/1 - 01 - 01 - 21:02
 * @Description: com.bill.rpc.common
 * @version: 1.0
 */
public interface   IUserService {
    public User findUser(Integer id);
}

package com.bill.rpc.common;

import java.io.Serializable;

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/1 - 01 - 01 - 20:54
 * @Description: com.bill.rpc.common
 * @version: 1.0
 */
public class User implements Serializable {
    private Integer id;
    private String name;

    public User(Integer id,String name){
        this.id = id;
        this.name = name;
    }

    public User() {
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString(){
        return "user:{"+
                "id="+id+
                ",name="+name+'\''+
                "}";
    }
}
package com.bill.rpc01;

import com.bill.rpc.common.IUserService;
import com.bill.rpc.common.User;

import javax.naming.Name;

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/1 - 01 - 01 - 22:21
 * @Description: com.bill.rpc01
 * @version: 1.0
 */
public class UserServiceImpl implements IUserService {
    public User findUser(Integer id) {
        return new User(id,"bill");
    }
}
package com.bill.rpc01;

import com.bill.rpc.common.IUserService;
import com.bill.rpc.common.User;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/1 - 01 - 01 - 22:39
 * @Description: com.bill.rpc01
 * @version: 1.0
 */
public class Server {
    private static boolean running = true;

    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(8888);
        while (running){
            Socket s = ss.accept();
            process(s);
            s.close();
        }
        ss.close();
    }

    private static void process(Socket s) throws IOException {
        InputStream in = s.getInputStream();
        OutputStream out = s.getOutputStream();
        DataInputStream dis = new DataInputStream(in);
        DataOutputStream dos = new DataOutputStream(out);

        int id = dis.readInt();
        IUserService service = new UserServiceImpl();
        User user = service.findUser(id);
        dos.writeInt(user.getId());
        dos.writeUTF(user.getName());
        dos.flush();
    }
}

标签:bill,name,230101,50,id,RPC,import,com,public
From: https://www.cnblogs.com/wcwblog/p/17019203.html

相关文章