目录
- 一、安装和启动
- 1、官网下载redis-3.2.10.tar.gz
- 2、上传到linux服务器解压
- 3、cd 解压⽂件⽬录,对解压的redis进⾏编译
- 4.然后cd 进⼊src⽬录,执⾏命令
- 5.修改解压⽬录中的配置⽂件redis.conf,关掉保护模式
- 6、启动服务
- 二、命令说明
- 1、redis服务启动命令
- 2、redis服务关闭命令
- 3、Redis客户端启动
- 三、Java代码中使⽤redis
- 1、关闭RedisServer端的防火墙
- 2、pom依赖
- 3、Java代码
- 四、Spring访问Redis
- (一)pom依赖
- (二)redis.properties
- (三)application.xml配置文件
- (四)测试
- 五、SpringBoot访问Redis
- (一)pom依赖
- (二)application.properties
- (三)RedisConfig
- (四)Controller层
- (五)测试
一、安装和启动
注意:
需要先安装 C 语言需要的 GCC 环境
yum install -y gcc-c++
yum install -y wget
1、官网下载redis-3.2.10.tar.gz
2、上传到linux服务器解压
tar -zxvf redis-3.2.10.tar.gz
或者,直接在服务器上下载
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
3、cd 解压⽂件⽬录,对解压的redis进⾏编译
make
若报错,找不到gcc
yum install gcc-c++
查看版本
gcc -v
4.然后cd 进⼊src⽬录,执⾏命令
make install
若需要指定redis安装路径,需要通过 PREFIX 命令。
mkdir /usr/redis -p
make install PREFIX=/usr/redis
5.修改解压⽬录中的配置⽂件redis.conf,关掉保护模式
设置后端启动:
6、启动服务
在src⽬录下执⾏
./redis-server ../redis.conf
二、命令说明
1、redis服务启动命令
./redis-server redis.conf
2、redis服务关闭命令
./redis-cli shutdown
3、Redis客户端启动
./redis-cli -h 127.0.0.1 -p 6379
参数说明
-h:redis服务器的ip地址 -p:redis实例的端口号
默认方式
- h -p 可省略
- 默认主机地址是127.0.0.1
- 默认端口是6379
- redis-server :启动 redis 服务
- redis-cli :进入 redis 命令客户端
- redis-benchmark : 性能测试的工具
- redis-check-aof : aof 文件进行检查的工具
- redis-check-dump : rdb 文件进行检查的工具
- redis-sentinel : 启动哨兵监控服务
三、Java代码中使⽤redis
使⽤Jedis客户端调⽤Reids的incr命令获得⼀个全局的id
1、关闭RedisServer端的防火墙
systemctl stop firewalld(默认) systemctl disable firewalld.service(设置开启不启动)
2、pom依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
3、Java代码
(此处我们就是连接单节点,也不使⽤连接池)
Jedis jedis = new Jedis("127.0.0.1",6379); try {
long id = jedis.incr("id");
System.out.println("从redis中获取的分布式id为:" + id); } finally {
if (null != jedis) {
jedis.close();
}
}
@Test
public void testConn(){
//与Redis建立连接 IP+port
Jedis redis = new Jedis("127.0.0.1", 6379);
//在Redis中写字符串 key value
redis.set("jedis:name:1","jd-zhangfei");
//获得Redis中字符串的值
System.out.println(redis.get("jedis:name:1"));
//在Redis中写list
redis.lpush("jedis:list:1","1","2","3","4","5");
//获得list的长度
System.out.println(redis.llen("jedis:list:1"));
}
四、Spring访问Redis
(一)pom依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
(二)redis.properties
redis.server=127.0.0.1
redis.port=6379
redis.timeout=50000
redis.pool.maxActive=100
redis.pool.maxIdle=50
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true
(三)application.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>
<!-- redis config -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="${redis.pool.maxActive}"/>
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<property name="maxWait" value="${redis.pool.maxWait}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.server}"/>
<property name="port" value="${redis.port}"/>
<property name="timeout" value="${redis.timeout}"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
<bean class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="KeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="ValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
</bean>
</beans>
(四)测试
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import java.io.Serializable;
@ContextConfiguration({"classpath:application.xml"})
public class RedisTest extends AbstractJUnit4SpringContextTests {
@Autowired
private RedisTemplate<Serializable,Serializable> redisTemplate;
@Test
public void test(){
redisTemplate.opsForValue().set("name","zhangsan");
System.out.println(redisTemplate.opsForValue().get("name"));
}
}
五、SpringBoot访问Redis
创建SpringBoot项目,创建的时候选择Spring Web和Spring Data Redis
(一)pom依赖
以下为自动生成
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
(二)application.properties
server.port=8080
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.timeout=30000
spring.redis.jedis.pool.max-active=80
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=30000
spring.redis.jedis.pool.min-idle=0
(三)RedisConfig
添加配置类
package com.lagou.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate(){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
(四)Controller层
package com.lagou.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping(value = "/redis")
public class RedisController {
@Autowired
private RedisTemplate redisTemplate;
// localhost:8080/redis/addRedis
@GetMapping("addRedis")
public String addRedis(@RequestParam(required = true) String key,
@RequestParam(required = true) String value) {
//设置过期时间为20秒
redisTemplate.opsForValue().set(key, value, 20, TimeUnit.SECONDS);
return "SUCCESS";
}
// localhost:8080/redis/getRedis/
@GetMapping("getRedis")
public String getRedis(String key) {
//设置过期时间为20秒
Object result = redisTemplate.opsForValue().get(key);
return result.toString();
}
}
(五)测试