文章目录
1 Java socket编程连接redis服务器
使用java通过socket连接到redis-server
可以实现redis的各种命令
1.1 创建maven工程
1.2 使用java手写redis客户端
在redis里创建MyClient.java
package edu.xja.redis;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import static com.oracle.jrockit.jfr.ContentType.Bytes;
/**
* Java socket编程连接
* 针对无密码的redis服务器
*/
public class MyClient {
//定义属性
//redis服务器ip地址
private static final String IP_ADDRESS="192.168.10.148";
//redis服务器端口号
private static final Integer SERVER_PORT=6379;
/**
* 主函数
* @param args
*/
public static void main(String[] args) throws IOException {
//1.建立socket
Socket scoket = new Socket(IP_ADDRESS, SERVER_PORT);
//2.判断是否连接到服务器
System.out.println("是否连接到redis服务器:"+ scoket.isConnected());
//3. 获取iOutputStream输入流,这是发给服务器的请求信息
OutputStream outputStream = scoket.getOutputStream();
//4. 获取inputStream输入流,这是服务器返回的响应
InputStream inputStream = scoket.getInputStream();
//5.开始执行命令
//(1)执行auth命令
auth(outputStream,inputStream);
//(12)执行ping命令
ping(outputStream,inputStream);
//(3)执行set命令
set(outputStream,inputStream);
//6.关闭资源
inputStream.close();
outputStream.close();
scoket.close();
}
/**
* auth发送用户信息
* @param outputStream
* @param inputStream
*/
private static String auth(OutputStream outputStream, InputStream inputStream) throws IOException {
//1.redis协议字符串 auth 123456
String sendInfo="*2\r\n$4\r\nauth\r\n$6\r\n123456\r\n";
//2. 发送授权命令
outputStream.write(sendInfo.getBytes());
//3.创建字节数组装响应数据
byte[] responseByte = new byte[1024];
//4.读取服务器响应到字节数组
int length = inputStream.read(responseByte);
//5.将字节数组转换成字符串
return new String(responseByte, 0, length);
}
/**
* ping查看是否联通服务器
* @param outputStream
* @param inputStream
*/
private static void ping(OutputStream outputStream, InputStream inputStream) throws IOException {
//1.redis协议字符串 ping
String sendInfo="*1\r\n$4\r\nping\r\n";
//2. 发送授权命令
outputStream.write(sendInfo.getBytes());
//3.创建字节数组装响应数据
byte[] responseByte = new byte[1024];
//4.读取服务器响应到字节数组
int length = inputStream.read(responseByte);
//5.将字节数组转换成字符串
String responseInfo = new String(responseByte, 0, length);
System.out.println(responseInfo);
}
/**
* set创建key
* @param outputStream
* @param inputStream
*/
private static void set(OutputStream outputStream, InputStream inputStream) throws IOException {
//1.redis协议字符串 set animal monkey
String sendInfo="*3\r\n$3\r\nset\r\n$6\r\nanimal\r\n$6\r\nmonkey\r\n";
//2. 发送授权命令
outputStream.write(sendInfo.getBytes());
//3.创建字节数组装响应数据
byte[] responseByte = new byte[1024];
//4.读取服务器响应到字节数组
int length = inputStream.read(responseByte);
//5.将字节数组转换成字符串
String responseInfo = new String(responseByte, 0, length);
System.out.println(responseInfo);
}
}
2 使用Jedis和JedisPool连接redis服务器
2.1 导入Jedis依赖
<!--jedis依赖-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>
2.2 编写测试代码
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class RedisClientTest {
@Test
@DisplayName("jedis连接redis服务器")
public void jedis(){
//创建jedis
Jedis jedis = new Jedis("192.168.10.148",6379);
//执行命令
jedis.auth("123456");
String pong = jedis.ping();
jedis.keys("*");
String set = jedis.set("hello", "world");
System.out.println(set);
}
@Test
@DisplayName("jedis连接池连接redis服务器")
public void jedisPool(){
//创建jedis
JedisPool jedisPool = new JedisPool("192.168.10.148",6379);
Jedis jedis = jedisPool.getResource();
//执行命令
jedis.auth("123456");
String pong = jedis.ping();
String set = jedis.set("animal", "monkey");
System.out.println(set);
}
}
3 使用lettuce连接redis服务器
3.1 导入依赖
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.2.0.RELEASE</version>
</dependency>
</dependencies>
3.2 编写测试代码
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class RedisClientTest {
@Test
@DisplayName("生菜连接redis服务器")
public void lettce(){
//创建jedis
RedisClient redisClient = RedisClient.create("redis://123456@192.168.10.148:6379/0");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> jedis = connection.sync();
//执行命令
String pong = jedis.ping();
String set = jedis.set("friut", "apple");
}
}