在Linux下解压redis安装包
进入解压后的目录进行编译
编译完成
将redis安装到指定目录
启动redis
默认端口Port:6379
属于前置启动,会占用整个终端,按Ctrl+C停止
后置启动,将redis.conf复制到redis/bin目录下
修改复制后的配置文件,将no该为yes
Centos7开放端口
启动redis 查看redis是否启动成功
IDEA客户端工具连接redis服务成功
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>springboot-redisartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.6.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>3.3.0version>
dependency>
dependencies>
project>
/**
* @Author: kenewstar
* @Description: Redis配置类
* @Date:Created in 2020/6/27
*/
@Configuration
public class RedisConfig {
//连接池
@Bean
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig config = new JedisPoolConfig();
//最大空闲数(默认8)
config.setMaxIdle(12);
//最小空闲数(默认0)
config.setMinIdle(6);
//最大连接数(默认8)
config.setMaxTotal(24);
return config;
}
/**
* SpringDataRedis2.x版本已废弃使用jedis
* @param config
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
JedisConnectionFactory factory = new JedisConnectionFactory();
//不推荐使用,SpringDataRedis2.x中已过时
factory.setPoolConfig(config);
factory.setHostName("192.168.40.128"); //redis服务的ip
factory.setPort(6379); //redis服务的端口
return factory;
}
//redis操作类
@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//设置key/value的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
/**
* @Author: kenewstar
* @Description: 测试redis操作
* @Date:Created in 2020/6/27
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class TestRedisString {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Test
public void set(){
this.redisTemplate.opsForValue().set("name","muke");
}
@Test
public void get(){
Object name = this.redisTemplate.opsForValue().get("name");
System.out.println(name);
}
}
不推荐该种方式存取java对象,会造成空间浪费,使用json字符串格式存取会更好
/**
* @Author: kenewstar
* @Description: 测试redis操作
* @Date:Created in 2020/6/27
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class TestRedisString {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Test
public void setObject(){
User user = new User();
user.setId(1);
user.setName("kenewstar");
user.setAge(21);
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
this.redisTemplate.opsForValue().set("user",user);
}
@Test
public void getObject(){
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
User user = (User)this.redisTemplate.opsForValue().get("user");
System.out.println(user);
}
}
/**
* @Author: kenewstar
* @Description: 测试redis操作
* @Date:Created in 2020/6/27
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class TestRedisString {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Test
public void setJsonObject(){
User user = new User();
user.setId(2);
user.setName("kenewstar2");
user.setAge(22);
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
this.redisTemplate.opsForValue().set("userJson",user);
}
@Test
public void getJsonObject(){
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
User user = (User) this.redisTemplate.opsForValue().get("userJson");
System.out.println(user);
}
}
将redis数据库连接信息与连接池信息配置在全局配置文件中
#redis单机应用环境配置
spring:
redis:
host: 192.168.40.128
port: 6379
password: #无密码不配置
database: 0 #数据库索引(0-15)默认为0
timeout: 300s #连接超时时间
#redis连接池配置
jedis:
pool:
max-idle: 16 #最大空闲数(默认8)
min-idle: 4 #最小空闲数(默认0)
max-active: 20 #最大连接数(默认8)
max-wait: 60000ms # 连接池最大阻塞等待时间 默认-1ms (-1 :表示没有限制) 这里设置1分钟
/**
* @Author: kenewstar
* @Description: Redis配置类
* @Date:Created in 2020/6/27
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
略…