概述
在 Java中操作使用Redis 的方案很多,比如:Jedis,Spring Data Redis,lettuce等。
下载 Jedis的jar包:https://mvnrepository.com/artifact/redis.clients/jedis
Jedis 是 Redis官方推荐的Java连接开发工具。要在Java开发中使用好Redis,必须对Jedis熟练掌握才能写出优雅的代码。
Jedis 的功能函数和 Redis基本都是一一对应的。
1、确保 Linux中的防火墙关闭
systemctl status firewalld.service #查看firewalld状态
systemctl stop firewalld #关闭
2、Redis的配置文件中默认的绑定本地ip的设置 bind 127.0.0.1后面添加服务器ip地址。顺便设置登录密码
一、Java 项目使用 Redis
1、创建 java项目,添加 Jedis的jar包
Java链接Redis OK.
2、使用Jedis连接池
连接池需要用到 slf4j的日志jar包,引入这几个架包
测试:
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class Main {
public static void main(String[] args) {
// 创建连接池配置对象
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 设置最大闲置个数
jedisPoolConfig.setMaxIdle(30);
// 设置最小闲置个数
jedisPoolConfig.setMinIdle(10);
// 设置最大的连接数
jedisPoolConfig.setMaxTotal(50);
// 创建连接池对象
JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.198.5", 6379);
// 获取连接资源
Jedis jedis = jedisPool.getResource();
//设置密码
jedis.auth("123456");
System.out.println(jedis.ping());
// 测试数据
System.out.println("清空数据库数据:" + jedis.flushDB());
System.out.println("set设置值:" + jedis.set("k_set1", "vvvset333"));
System.out.println(("get取值:" + jedis.get("k_set1")));
System.out.println("获取ttl:" + jedis.ttl("k_set1"));
System.out.println("ttl设置:" + jedis.expire("k_set1",30));
System.out.println("获取ttl:" + jedis.ttl("k_set1"));
}
}
===============
PONG
清空数据库数据:OK
set设置值:OK
get取值:vvvset333
获取ttl:-1
ttl设置:1
获取ttl:30
二、Spring 整合 Redis
创建一个 maven工程,引入相关依赖包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
1、连接 redis 单机版
1)redis.properties配置文件
#ip地址
redis.host=192.168.198.5
#端口号
redis.port=6379
#如果有密码
redis.password=123456
#客户端超时时间单位是毫秒 默认是2000
redis.timeout=3000
#数据库,默认的是0
redis.database=0
#最大空闲链接
redis.maxIdle=300
#最小的空闲链接
redis.minIdle=10
#连接池的最大数据库连接数
redis.maxTotal=400
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
redis.maxWaitMillis=1000
#在空闲时检查有效性, 默认false
redis.testOnBorrow=false
#连接耗尽是否阻塞,false代表抛异常,true代表阻塞直到超时,默认为true
redis.blockWhenExhausted=true
2)spring.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--连接redis单机版,创建了一个JedisClientPool的bean,创建这个bean需要一个jedisPool属性,在下面 -->
<bean class="cn.jq.demo.dao.JedisPoolDaoImpl1" id="jedisPoolDaoImpl1">
<property name="jedisPool" ref="jedisPool"></property>
</bean>
<context:property-placeholder location="classpath:redis.properties"/>
<!--JedisPool这个bean的构造方法需要多个参数 -->
<bean class="redis.clients.jedis.JedisPool" id="jedisPool">
<constructor-arg name="host" value="${redis.host}"></constructor-arg>
<constructor-arg name="port" value="${redis.port}"></constructor-arg>
<constructor-arg name="password" value="${redis.password}"></constructor-arg>
<constructor-arg name="timeout" value="${redis.timeout}"></constructor-arg>
<constructor-arg name="database" value="${redis.database}"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.JedisPoolConfig" id="jedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="minIdle" value="${redis.minIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
<property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>
</bean>
</beans>
3)dao层封装下
public interface JedisPoolDao {
String set(String key, String value);
String get(String key);
Boolean exists(String key);
}
JedisPool
public class JedisPoolDaoImpl1 implements JedisPoolDao {
private JedisPool jedisPool;
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String result = jedis.set(key, value);
jedis.close();
return result;
}
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String result = jedis.get(key);
jedis.close();
return result;
}
@Override
public Boolean exists(String key) {
Jedis jedis = jedisPool.getResource();
Boolean result = jedis.exists(key);
jedis.close();
return result;
}
}
4)测试
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
JedisPoolDaoImpl1 jedisPoolDaoImpl1 = context.getBean("jedisPoolDaoImpl1", JedisPoolDaoImpl1.class);
System.out.println("set设置值:" + jedisPoolDaoImpl1.set("k_set2", "23123asda"));
System.out.println(("get取值:" + jedisPoolDaoImpl1.get("k_set2")));
}
}
2、实现集群版的redis客户端的接口(6个redis服务都打开)
1)spring.xml配置文件修改
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:redis.properties"/>
<!--实现集群版的redis客户端-->
<bean class="cn.jq.demo.dao.JedisClusterClient" id="jedisClusterClient">
<property name="jedisCluster" ref="jedisCluster"></property>
</bean>
<!--JedisCluster类的构造方法选择带密码的,根据你的需要 -->
<bean class="redis.clients.jedis.JedisCluster" id="jedisCluster">
<constructor-arg name="jedisClusterNode" >
<set >
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.198.5"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.198.10"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.198.11"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.198.12"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.198.13"></constructor-arg>
<constructor-arg name="port" value="6379"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.198.14"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
</set>
</constructor-arg>
<constructor-arg name="connectionTimeout" value="${redis.timeout}"></constructor-arg>
<constructor-arg name="soTimeout" value="1000"></constructor-arg>
<constructor-arg name="maxAttempts" value="10"></constructor-arg>
<constructor-arg name="password" value="${redis.password}"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.JedisPoolConfig" id="jedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="minIdle" value="${redis.minIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
<property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>
</bean>
</beans>
2)JedisClusterClient类:JedisCluster
public class JedisClusterClient implements JedisPoolDao {
private JedisCluster jedisCluster;
public JedisCluster getJedisCluster() {
return jedisCluster;
}
public void setJedisCluster(JedisCluster jedisCluster) {
this.jedisCluster = jedisCluster;
}
@Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
}
@Override
public String get(String key) {
return jedisCluster.get(key);
}
@Override
public Boolean exists(String key) {
return jedisCluster.exists(key);
}
}
3)main测试:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// 测试集群版
JedisClusterClient jedisClusterClient = context.getBean("jedisClusterClient", JedisClusterClient.class);
System.out.println("set设置值:" + jedisClusterClient.set("k_cluster", "jedisClusterClient"));
System.out.println(("get取值:" + jedisClusterClient.get("k_cluster")));
}
—— Stay Hungry. Stay Foolish. 求知若饥,虚心若愚。
最后
以上就是懵懂故事为你收集整理的Spring整合Redis的全部内容,希望文章能够帮你解决Spring整合Redis所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复