jedis
学习了狂神的redis
导入jedis包
建一个maven项目
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>
写了两行代码
public static void main(String[] args) {
Jedis jedis = new Jedis("xxx.xxx.xxx.xxx", 6379);
//写自己服务器的地址
System.out.println(jedis.ping());
}
我是虚拟机做服务器的,发现无法访问。。
systemctl status firewalld
查看了一下防火墙的状态,发现是running
关闭一下防火墙
systemcl stop firewalld
Jedis使用测试——连接本地及远程的Redis
后面找到了这篇博客,跟着更改了配置redis.conf 的配置。就输出PONG
Jedis jedis = new Jedis("xxx.xxx.xxx.xxx", 6379);
jedis.flushDB();
HashMap<String, String> hashmap = new HashMap<>();
hashmap.put("key1","value1");
hashmap.put("key2","value2");
hashmap.put("key3","value3");
// jedis.mset("hash", hashmap);
jedis.hset("hash", hashmap);
jedis.hset("hash","key4","value4");
System.out.println(jedis.hgetAll("hash"));
System.out.println(jedis.hkeys("hash"));
System.out.println(jedis.hvals("hash"));
}
输出
{key1=value1, key2=value2,
key3=value3, key4=value4}
[key1, key2, key3, key4]
[value2, value1, value3, value4]
事务
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.249.134", 6379);
jedis.flushDB();
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello","world");
jsonObject.put("name","zhangsan");
jsonObject.put("age","111");
// 开启事务
Transaction multi = jedis.multi();
String result = jsonObject.toJSONString();
// jedis.watch(result);
try {
multi.set("user1",result);
multi.set("user2",result);
multi.exec();
// 执行事务
}catch (Exception e){
multi.discard();
// 放弃事务
e.printStackTrace();
}finally {
System.out.println(jedis.get("user1"));
System.out.println(jedis.get("user2"));
jedis.close();
}
}
整合springboot
新建springboot项目 需要点击web和redis
在springboot2.x之后采用lettuce
jedis:采用的直连,多个线程操作,是不安全的,如果避免不安全,使用jedis pool连接池
lettuce:采用netty 实例可以多线程共享,不存在线程不安全
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
//默认的redistemplate
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
//最常用的string类型
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
配置文件可以配置的redis
public class RedisProperties {
private int database = 0;
private String url;
private String host = "localhost";
private String password;
private int port = 6379;
private boolean ssl;
private Duration timeout;
private String clientName;
private RedisProperties.Sentinel sentinel;
private RedisProperties.Cluster cluster;
private final RedisProperties.Jedis jedis = new RedisProperties.Jedis();
private final RedisProperties.Lettuce lettuce = new RedisProperties.Lettuce();
....................
配置
# 应用名称
spring.application.name=springboot-redis
# 应用服务 WEB 访问端口
server.port=8080
//写自己的服务器ip
spring.redis.host=xxx.xxx.xxx.xxx
spring.redis.port=6379