Jedis 使用
配置
客户端要能够成功连接上 redis 服务器,需要检查如下三个配置:
1.远程 Linux 防火墙已经关闭,以我这里的 CentOS7 为例,关闭防火墙命令 systemctl stop firewalld.service
,同时还可以再补一刀 systemctl disable firewalld.service
表示禁止防火墙开机启动。
2.关闭 redis 保护模式,在 redis.conf 文件中,修改 protected 为 no,如下:
protected-mode no
3.注释掉 redis 的 ip 地址绑定,还是在 redis.conf 中,将 bind:127.0.0.1 注释掉,如下:
# bind:127.0.0.1
确认了这三步之后,就可以远程连接 redis 了。
Java 端配置
上面的配置完成后,我们可以创建一个普通的 JavaSE 工程来测试下了,Java 工程创建成功后,添加 Jedis 依赖,如下:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
然后我们可以通过如下一个简单的程序测试一下连接是否成功:
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.248.128", 6379);
String ping = jedis.ping();
System.out.println(ping);
}
运行之后,看到如下结果表示连接成功了:
连接成功之后,剩下的事情就比较简单了,Jedis 类中方法名称和 redis 中的命令基本是一致的,看到方法名小伙伴就知道是干什么的,因此这些我这里不再重复叙述。
频繁的创建和销毁连接会影响性能,我们可以采用连接池来部分的解决这个问题:
public static void main(String[] args) {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(100);
config.setMaxIdle(20);
JedisPool jedisPool = new JedisPool(config, "192.168.248.128", 6379);
Jedis jedis = jedisPool.getResource();
System.out.println(jedis.ping());
}
这样就不会频繁创建和销毁连接了,在 JavaSE 环境中可以把连接池配置成一个单例模式,如果用了 Spring 容器的话,可以把连接池交给 Spring 容器管理。
上面这种连接都是连接单节点的 Redis,如果是一个 Redis 集群,要怎么连接呢?很简单,如下:
Set<HostAndPort> clusterNodes = new HashSet<HostAndPort>();
clusterNodes.add(new HostAndPort("192.168.248.128", 7001));
clusterNodes.add(new HostAndPort("192.168.248.128", 7002));
clusterNodes.add(new HostAndPort("192.168.248.128", 7003));
clusterNodes.add(new HostAndPort("192.168.248.128", 7004));
clusterNodes.add(new HostAndPort("192.168.248.128", 7005));
clusterNodes.add(new HostAndPort("192.168.248.128", 7006));
JedisCluster jc = new JedisCluster(clusterNodes);
jc.set("address", "深圳");
String address = jc.get("address");
System.out.println(address);
JedisCluster 中的方法与 Redis 命令也是基本一致,我就不再重复介绍了。
SpringMVC 整合redis
Spring Data Redis 是 Spring 官方推出,可以算是 Spring 框架集成 Redis 操作的一个子框架,封装了 Redis 的很多命令,可以很方便的使用 Spring 操作 Redis 数据库,Spring 对很多工具都提供了类似的集成,如 Spring Data MongDB、Spring Data JPA 等, Spring Data Redis 只是其中一种。
环境搭建
要使用 SDR,首先需要搭建 Spring+SpringMVC 环境,由于这个不是本文的重点,因此这一步我直接略过,Spring+SpringMVC 环境搭建成功后,接下来我们要整合 SDR,首先需要添加如下依赖:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>RELEASE</version>
</dependency>
然后创建在 resources 目录下创建 redis.properties 文件作为 redis 的配置文件,如下:
redis.host=192.168.248.128
redis.port=6379
redis.maxIdle=300
redis.maxTotal=600
redis.maxWait=1000
redis.testOnBorrow=true
在 spring 的配置文件中,添加如下 bean:
<!--引入redis.properties文件-->
<context:property-placeholder location="classpath:redis.properties"/>
<!--配置连接池信息-->
<bean class="redis.clients.jedis.JedisPoolConfig" id="poolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<!--配置基本连接信息-->
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" id="connectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!--配置RedisTemplate-->
<bean class="org.springframework.data.redis.core.RedisTemplate" id="redisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!--key和value要进行序列化,否则存储对象时会出错-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
好了,在 Spring 中配置了 redisTemplate 之后,接下来我们就可以在 Dao 层注入 redisTemplate 进而使用了。
接下来我们首先创建实体类 User ,注意 User 一定要可序列化:
public class User implements Serializable{
private String username;
private String password;
private String id;
//get/set省略
}
然后在 Dao 层实现数据的添加和获取,如下:
@Repository
public class HelloDao {
@Autowired
RedisTemplate redisTemplate;
public void set(String key, String value) {
ValueOperations ops = redisTemplate.opsForValue();
ops.set(key, value);
}
public String get(String key) {
ValueOperations ops = redisTemplate.opsForValue();
return ops.get(key).toString();
}
public void setuser(User user) {
ValueOperations ops = redisTemplate.opsForValue();
ops.set(user.getId(), user);
}
public User getuser(String id) {
ValueOperations<String, User> ops = redisTemplate.opsForValue();
User user = ops.get(id);
System.out.println(user);
return user;
}
}
SDR 官方文档中对 Redistemplate 的介绍,通过 Redistemplate 可以调用 ValueOperations 和 ListOperations 等等方法,分别是对 Redis 命令的高级封装。但是 ValueOperations 等等这些命令最终是要转化成为 RedisCallback 来执行的。也就是说通过使用 RedisCallback 可以实现更强的功能。
最后,给大家展示下我的 Service 和 Controller ,如下:
@Service
public class HelloService {
@Autowired
HelloDao helloDao;
public void set(String key, String value) {
helloDao.set(key,value);
}
public String get(String key) {
return helloDao.get(key);
}
public void setuser(User user) {
helloDao.setuser(user);
}
public String getuser(String id) {
String s = helloDao.getuser(id).toString();
return s;
}
}
Controller:
@Controller
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/set")
@ResponseBody
public void set(String key, String value) {
helloService.set(key, value);
}
@RequestMapping("/get")
@ResponseBody
public String get(String key) {
return helloService.get(key);
}
@RequestMapping("/setuser")
@ResponseBody
public void setUser() {
User user = new User();
user.setId("1");
user.setUsername("深圳");
user.setPassword("sang");
helloService.setuser(user);
}
@RequestMapping(value = "/getuser",produces = "text/html;charset=UTF-8")
@ResponseBody
public String getUser() {
return helloService.getuser("1");
}
}
测试过程就不再展示了,小伙伴们可以用 POSTMAN 等工具自行测试。
Spring boot 整合redis
在传统的 SSM 中,需要开发者自己来配置 Spring Data Redis ,这个配置比较繁琐,主要配置 3 个东西:连接池、连接器信息以及 key 和 value 的序列化方案。
在 Spring Boot 中,默认集成的 Redis 就是 Spring Data Redis,默认底层的连接池使用了 lettuce ,开发者可以自行修改为自己的熟悉的,例如 Jedis。
Spring Data Redis 针对 Redis 提供了非常方便的操作模板 RedisTemplate 。这是 Spring Data 擅长的事情,那么接下来我们就来看看 Spring Boot 中 Spring Data Redis 的具体用法。
方案一:Spring Data Redis
创建工程
创建工程,引入 Redis 依赖:
创建成功后,还需要手动引入 commos-pool2 的依赖,因此最终完整的 pom.xml 依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
这里主要就是引入了 Spring Data Redis + 连接池。
配置 Redis 信息
接下来配置 Redis 的信息,信息包含两方面,一方面是 Redis 的基本信息,另一方面则是连接池信息:
spring.redis.database=0
spring.redis.password=123
spring.redis.port=6379
spring.redis.host=192.168.66.128
spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=1ms
spring.redis.lettuce.shutdown-timeout=100ms
自动配置
当开发者在项目中引入了 Spring Data Redis ,并且配置了 Redis 的基本信息,此时,自动化配置就会生效。
我们从 Spring Boot 中 Redis 的自动化配置类中就可以看出端倪:
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
这个自动化配置类很好理解:
- 首先标记这个是一个配置类,同时该配置在 RedisOperations 存在的情况下才会生效(即项目中引入了 Spring Data Redis)
- 然后导入在 application.properties 中配置的属性
- 然后再导入连接池信息(如果存在的话)
- 最后,提供了两个 Bean ,RedisTemplate 和 StringRedisTemplate ,其中 StringRedisTemplate 是 RedisTemplate 的子类,两个的方法基本一致,不同之处主要体现在操作的数据类型不同,RedisTemplate 中的两个泛型都是 Object ,意味者存储的 key 和 value 都可以是一个对象,而 StringRedisTemplate 的 两个泛型都是 String ,意味者 StringRedisTemplate 的 key 和 value 都只能是字符串。如果开发者没有提供相关的 Bean ,这两个配置就会生效,否则不会生效。
使用
接下来,可以直接在 Service 中注入 StringRedisTemplate 或者 RedisTemplate 来使用:
@Service
public class HelloService {
@Autowired
RedisTemplate redisTemplate;
public void hello() {
ValueOperations ops = redisTemplate.opsForValue();
ops.set("k1", "v1");
Object k1 = ops.get("k1");
System.out.println(k1);
}
}
Redis 中的数据操作,大体上来说,可以分为两种:
- 针对 key 的操作,相关的方法就在 RedisTemplate 中
- 针对具体数据类型的操作,相关的方法需要首先获取对应的数据类型,获取相应数据类型的操作方法是 opsForXXX
调用该方法就可以将数据存储到 Redis 中去了,如下:
k1 前面的字符是由于使用了 RedisTemplate 导致的,RedisTemplate 对 key 进行序列化之后的结果。
RedisTemplate 中,key 默认的序列化方案是 JdkSerializationRedisSerializer 。
而在 StringRedisTemplate 中,key 默认的序列化方案是 StringRedisSerializer ,因此,如果使用 StringRedisTemplate ,默认情况下 key 前面不会有前缀。
不过开发者也可以自行修改 RedisTemplate 中的序列化方案,如下:
@Service
public class HelloService {
@Autowired
RedisTemplate redisTemplate;
public void hello() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
ValueOperations ops = redisTemplate.opsForValue();
ops.set("k1", "v1");
Object k1 = ops.get("k1");
System.out.println(k1);
}
}
当然也可以直接使用 StringRedisTemplate:
@Service
public class HelloService {
@Autowired
StringRedisTemplate stringRedisTemplate;
public void hello2() {
ValueOperations ops = stringRedisTemplate.opsForValue();
ops.set("k2", "v2");
Object k1 = ops.get("k2");
System.out.println(k1);
}
}
另外需要注意 ,Spring Boot 的自动化配置,只能配置单机的 Redis ,如果是 Redis 集群,则所有的东西都需要自己手动配置。