学习自狂神说Redis:链接: https://blog.csdn.net/DDDDeng_/article/details/108118544.
Redis下载
官网下载稳定版本: www.redis.io
Linuk上安装Redis
解压Redis的安装包 程序一般放在 /opt 目录下
解压 redis tar -zxvf redis-x.x.x.tar.gz
环境安装
yum install gcc-c++
# 然后进入redis目录下执行
make
make install
安装出现问题:redis 6.0 以上 需要升级到gcc 9.3
升级到gcc 9.3:
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。
如果要长期使用gcc 9.3的话:
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
安装成功
redis默认安装路径 /usr/local/bin
启动及设置redis
自建文件夹 mkdir xxxx
在redis-x.x.x 目录下复制redis.conf到自建文件夹内 cp -r redis.conf /usr/local/bin/xxxx
将redis.conf 拷贝 至 bin 目录下 自建文件夹内 方便修改启动
启动redis
redis-server xxx/redis.conf
查看redis进程
ps -ef | grep redis
idea连接服务器上的redis首先要在redis.conf改几个设置
1.daemonize yes
2.注释 bind 127.0.0.1(在Bind前面加个#号)
3.protected-mode no
4.在redis.conf 进行密码设置
搜索 # requirepass foobared
requirepass xxxx //注意,行前不能有空格
auth password //重启redis 后验证密码
注意: 看一下自己防火墙对端口号开没开放,部署远程服务器还要查看是否开放该端口
firewall-cmd --query-port=6379/tcp
如果是yes就是开放的
是no的话就开启端口号:
firewall-cmd --zone=public --add-port=6379/tcp --permanent
如果报出了FirewallD is not running的错误.
解决方案:
- 开启防火墙
systemctl start firewalld - 查看防火墙状态,显示running即开启成功
systemctl status firewalld - 再次执行需要的命令
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload
springboot整合redis
application.yml设置
spring:
redis:
host: xx.xx.xx.xx
port: 6379
timeout: 5000
password: xxxxxx
pom.xml引入redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
RedisConfig 配置类
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
Redis工具类
工具类参考博客:
链接: https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html.
/**
* Redis工具类 交由spring托管即可使用
* */
@Component
public class RedisUtil {
@Autowired
private StringRedisTemplate redisTemplate;
//省略...........
}
测试类
@SpringBootTest
class WebApplicationTests {
@Autowired
private RedisUtil RedisUtil;
@Test
void test() {
RedisUtil.set("zhou","min");
System.out.println(RedisUtil.get("zhou"));
}
}
Redis消息订阅
/**
* redis接收消息订阅类
*/
public class RedisReceiver {
/**
* MessageListenerAdapter
* @param message
*/
public void receiveMessage(String message) {
System.out.println("receiveMessage消息来了:"+message);
}
/**
* 价格趋势和市场队列消息接收方法
*/
public void priceReceive( String message) {
System.out.println("priceReceive消息来了:"+message);
}
}
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zm.manager.redis.RedisReceiver;
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.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
/**
* redis消息监听器容器
* 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器
* 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
* @param connectionFactory
* @param listenerAdapter
* @return
*/
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter,
MessageListenerAdapter priceListenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//可以添加多个messageListener,将订阅频道与MessageListenerAdapter绑定
// PatternTopic 订阅频道
container.addMessageListener(listenerAdapter, new PatternTopic("index"));
container.addMessageListener(priceListenerAdapter, new PatternTopic("PRICE_TOPIC"));
return container;
}
/**
* 注册redis接收消息订阅类
*/
@Bean
public RedisReceiver receiver() {
return new RedisReceiver();
}
/**
* 消息监听器适配器,绑定消息处理器,利用反射技术调用消息处理器的业务方法
* defaultListenerMethod 消息到来时调用的方法
* @param redisReceiver
* @return
*/
@Bean
public MessageListenerAdapter listenerAdapter(RedisReceiver redisReceiver) {
// 消息来时在调用的方法:RedisReceiver.receiveMessage();
return new MessageListenerAdapter(redisReceiver, "receiveMessage");
}
/**
* 价格趋势订阅处理器,并指定处理方法
* @param receiver
* @return
*/
@Bean
public MessageListenerAdapter priceListenerAdapter(RedisReceiver receiver) {
return new MessageListenerAdapter(receiver, "priceReceive");
}
/**
* redis序列化配置
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
@Test
void test1() {
RedisUtil.push("index", "订阅index频道的消息");
RedisUtil.push("PRICE_TOPIC", "订阅PRICE_TOPIC频道的消息");
}
Redis可视化工具 Redis Desktop Manager下载
参考链接: https://blog.csdn.net/lizhiyuan_eagle/article/details/88742993.
下载百度网盘
链接: http://pan.baidu.com/s/1kU8sY3P.
安装一路next就行了
使用
windows 下载安装 redis
链接: windows 版本下载地址
防火墙开放对应端口
#启动命令:cmd 执行
redis-server.exe redis.windows.conf
注意命令中的“redis.windows.conf”是以.conf 为后缀的那个文件名,不要用“redis.windows-service.conf”
常见的问题是在启动Redis 时没有指定配置了密码的配置文件启动。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BXjhSLUp-1684977443262)(D:\work\img\image-20230518211507630.png)]