手写简单jedis架构体系(仿)
总体分析
jedis与redis服务器对接大体分为三层(自下向上)
1.transfer传输层
2.message potocol 协议层
3.api操作层
1.transfer传输层(创建Connection类)
public class Connection {
private Socket socket;
private String host;
private int port;
private OutputStream outputStream;
private InputStream inputStream;
public Connection(String host, int port) {
this.host = host;
this.port = port;
}
public Connection connection(){
if (!isConnectioned()) {
try {
socket = new Socket(host, port);
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
return this;
}
private boolean isConnectioned() {
return this.socket != null && this.socket.isBound() && !this.socket.isClosed() && this.socket.isConnected() && !this.socket.isInputShutdown() && !this.socket.isOutputShutdown();
}
public Connection sendConnection(Potocol.Command co , byte[] ... args){
connection();
Potocol.sendCommand(outputStream,co,args);
return this;
}
public String getStatusCocdereply(){
byte[] bytes = new byte[1024];
try {
socket.getInputStream().read(bytes);
} catch (IOException e) {
e.printStackTrace();
}
return new String(bytes);
}
}
2.message potocol 协议层(创建Potocol类)
public class Potocol {
public static final String DOLLAR_BYTE = "$";
public static final String ASTERISK_BYTE = "*";
public static final String BLANK_BYTE = "\r\n";
public static void sendCommand(OutputStream os,Command co ,byte[] ... args){
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(ASTERISK_BYTE).append(args.length+1).append(BLANK_BYTE);
stringBuffer.append(DOLLAR_BYTE).append(co.name().length()).append(BLANK_BYTE);
stringBuffer.append(co.name()).append(BLANK_BYTE);
for (final byte[] arg:args){
stringBuffer.append(DOLLAR_BYTE).append(arg.length).append(BLANK_BYTE);
stringBuffer.append(new String(arg)).append(BLANK_BYTE);
}
try {
os.write(stringBuffer.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 定义一个枚举类 存放命令
*/
public static enum Command{
SET , GET , KEYS
}
}
3.api操作层(创建Clint类)
public class Clint {
Connection connection;
public Clint(String host ,int port) {
this.connection = new Connection(host,port);
}
public String set(String key, String value){
connection.sendConnection(Potocol.Command.SET,key.getBytes(),value.getBytes());
return connection.getStatusCocdereply();
}
public String get(String key){
connection.sendConnection(Potocol.Command.GET,key.getBytes());
return connection.getStatusCocdereply();
}
}
4.main操作
public class App
{
public static void main( String[] args )
{
Clint jedis = new Clint("127.0.0.1",6379);
String zsh = jedis.set("zsh", "999");
String s = jedis.get("123");
System.out.println(s);
System.out.println(zsh);
}
}
5.使用ServerSocket拦截器获取jedis传输数据的格式
public class Hack {
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(6379);
Socket accept = serverSocket.accept();
byte[] bytes = new byte[1024];
accept.getInputStream().read(bytes);
System.out.println(new String(bytes));
}
}