剑指架构师系列-Redis安装与使用
1、安装Redis
我们在VMware中安装CentOS 64位系统后,在用户目录下下载安装Redis。
wget http://download.redis.io/releases/redis-stable.tar.gz tar -xzvf redis-stable.tar.gz cd redis-stable make PREFIX=/usr/local/redis01 install cd /usr/local/redis01/bin
加上`&`号使redis以后台程序方式运行
./redis-server &
或者也可以到 redis-stable/src目录下进行启动。
检测后台进程是否存在
ps -ef |grep redis
检测6379端口是否在监听
netstat -lntp | grep 6379
或者我们可以直接到redis-stable目录下修改配置文redis.conf,找到如下配置:
daemonize yes # When running daemonized, Redis writes a pid file in /var/run/redis.pid by # default. You can specify a custom pid file location here. pidfile "/var/run/redis/redis01.pid" # Accept connections on the specified port, default is 6379. # If port 0 is specified Redis will not listen on a TCP socket. port 7001
将daemonize值改为yes,修改进行pid的存在路径,然后重新指定一下port端口。
最后我们设置一下redis的log日志存放的地方,如果没有redis目录,需要切换到路径下进行新建。
logfile "/var/log/redis/redis01.log"
修改配置文件后我们需要指定使用哪个配置文件启动Redis
./redis-server ../redis.conf
用`redis-cli`客户端检测连接是否正常
./redis-cli 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set key "hello world" OK
我们可以在Windows下直接下载一个RedisClient直接连接VMware中安装的Redis即可。如下图。

如果连接不上,需要关闭一下防火墙,使用
iptables -F
来禁用linux的防火墙或者使用:
vi /etc/selinux/config
然后把修改 SELINUX=enforcing的值为disabled
Spring Boot集成Redis
在mazhi工程下新建Maven Module,名称为mazhi-redis,然后在 pom.xml文件中添加redis的包引用和spring boot的包引用,如下:
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.mazhi</groupId>
<artifactId>mazhi-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- redius -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
</dependencies>
然后引入application.yml文件,指定端口为8081。并且在src/java/main的org.mazhi.redis目录下新建Application.java文件,内容如下:
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
下面就来为系统配置Redis了,在org.mazhi.redis.config目录下新建RedisCacheConfig.java文件,内容如下:
@Configuration
@PropertySource(value = "classpath:/redis.properties")
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Bean
public KeyGenerator wiselyKeyGenerator(){
return new KeyGenerator() {
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(host);
factory.setPort(port);
factory.setTimeout(timeout); //设置连接超时时间
return factory;
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
// Number of seconds before expiration. Defaults to unlimited (0)
cacheManager.setDefaultExpiration(10); //设置key-value超时时间
return cacheManager;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
setSerializer(template); //设置序列化工具,这样ReportBean不需要实现Serializable接口
template.afterPropertiesSet();
return template;
}
private void setSerializer(StringRedisTemplate template) {
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);
template.setValueSerializer(jackson2JsonRedisSerializer);
}
}
然后新建redis.properties配置文件,内容如下:
spring.redis.database=0 spring.redis.host=192.168.2.129 # Login password of the redis server. spring.redis.password= spring.redis.pool.max-active=8 spring.redis.pool.max-idle=8 spring.redis.pool.max-wait=-1 spring.redis.pool.min-idle=0 spring.redis.port=7001 # Name of Redis server. spring.redis.sentinel.master= # Comma-separated list of host:port pairs. spring.redis.sentinel.nodes= spring.redis.timeout=0
注意指定spring.redis.host和spring.redis.port为你的redis配置。
在org.mazhi.redis.web目录下新建RedisTestCtrl.java,对Redis进行简单的CRUD操作,如下:
@RestController
@RequestMapping(value = "/redis")
public class RedisTestCtrl {
@Autowired
private StringRedisTemplate redisTemplate;
@RequestMapping(value = "/addKey")
public void addKey() {
redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.set( // 插入键为test,值为hello的键值对
redisTemplate.getStringSerializer().serialize("test"),
redisTemplate.getStringSerializer().serialize("hello")
);
return null;
}
});
}
@RequestMapping(value = "/deleteKey")
public void deleteKey() {
redisTemplate.delete("test"); // 删除数据库中键为test的键值对
}
}
这样在浏览器中访问一下:
http://localhost:8081/redis/addKey
执行添加的url后,可以在RedisClient中查看,如下:

然后执行:
http://localhost:8081/redis/delete
查看RedisClient,键值被删除。
剑指架构师系列-Redis安装与使用的更多相关文章
- 剑指架构师系列-Redis集群部署
初步搭建Redis集群 克隆已经安装Redis的虚拟机,我们使用这两个虚拟机中的Redis来搭建集群. master:192.168.2.129 端口:7001 slave:192.168.2.132 ...
- 剑指架构师系列-MySQL的安装及主从同步
1.安装数据库 wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-commun ...
- 剑指架构师系列-Nginx的安装与使用
Nginx可以干许多事情,在这里我们主要使用Nginx的反向代理与负载均衡功能. 1.Nginx的下载安装 在安装Nginx前需要安装如下软件: GCC Nginx是C写的,需要用GCC编译 PCR ...
- 剑指架构师系列-Logstash分布式系统的日志监控
Logstash主要做由三部署组成: Collect:数据输入 Enrich:数据加工,如过滤,改写等 Transport:数据输出 下面来安装一下: wget https://download.el ...
- 剑指架构师系列-持续集成之Maven+Nexus+Jenkins+git+Spring boot
1.Nexus与Maven 先说一下这个Maven是什么呢?大家都知道,Java社区发展的非常强大,封装各种功能的Jar包满天飞,那么如何才能方便的引入我们项目,为我所用呢?答案就是Maven,只需要 ...
- 剑指架构师系列-Linux下的调优
1.I/O调优 CentOS下的iostat命令输出如下: $iostat -d -k 1 2 # 查看TPS和吞吐量 参数 -d 表示,显示设备(磁盘)使用状态:-k某些使用block为单位的列强制 ...
- 剑指架构师系列-MySQL调优
介绍MySQL的调优手段,主要包括慢日志查询分析与Explain查询分析SQL执行计划 1.MySQL优化 1.慢日志查询分析 首先需要对慢日志进行一些设置,如下: SHOW VARIABLES LI ...
- 剑指架构师系列-ActiveMQ队列的使用
安装ActiveMQ只需要下载包后解压,然后就可以启动与关闭ActiveMQ了,如下: ./activemq start ./activemq stop 访问管理页面: http://10.10.20 ...
- 剑指架构师系列-spring boot的logback日志记录
Spring Boot集成了Logback日志系统. Logback的核心对象主要有3个:Logger.Appender.Layout 1.Logback Logger:日志的记录器 主要用于存放日志 ...
随机推荐
- 在Debian或Ubuntu中安装和使用'搜狗输入法for linux'
下载搜狗输入法 for linux点击 搜狗输入法 for linux 以下载安装包到本地 安装搜狗输入法 for linuxA.准备工作: (1) 连接网络.挂载系统安装盘 此安装过程需要网络连接, ...
- [论文阅读] Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks(MTCNN)
相关论文:Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks 概论 用于人脸检测和对 ...
- 一个关于C8051F350模拟电源的小问题
前言 多做重要而不紧急的工作,慢慢的就会发现重要而紧急的工作没那么多了 工作方法 今天有好几个同事出差去现场实验了,为了今天的顺利成行,昨天加了个班,但是从项目管理的角度或者说做事的方法上来讲,这次加 ...
- Java基础之关键字,标识符,变量
Java基础 首先,来看一下Java基础知识图解,以下便是在java学习中我们需要学习设计到的一些知识(当然不是很完全). 这些都是接下来在以后的学习中我们会学到的一些知识. 1 关键字 首次先来学习 ...
- 使用开源数据库客户端DBeaver连接DB2数据库
下载安装 首先进入 官网 选择对应的版本进行安装. 下载下来后,一直惦记next即可完成安装(期间包括选择文件安装路径等操作,可按需修改). 连接db2 打开DBeaver,新建连接-->DBe ...
- sumo快速运行简单仿真实例详细教程
本文旨在让大家快速的了解sumo,并给出运行一个简单的sumo的例子的教程,进而了解基本sumo工程的架构,使大家对该软件产生兴趣并持续学习下去,刚开始学习仿真的确枯燥,项目"跑起来&quo ...
- Java内存回收机制.md
1.java的内存 java的内存结构分为 堆 (是gc的主要区域) 线程共享,主要是用于分配实例对象和数组 栈 线程私有,它的生命周期和线程相同,又分成 虚拟机栈和本地方法栈,只有它会报 Stack ...
- 一篇文章说透Nginx的rewrite模块
rewrite模块即ngx_http_rewrite_module模块,主要功能是改写请求URI,是Nginx默认安装的模块.rewrite模块会根据PCRE正则匹配重写URI,然后发起内部跳转再匹配 ...
- mysql优化2:列类型选择原则
1.字段类型优先级 整型>date,time>enum,char>varchar>blog,text 列的特点分析: 整型:定长,没有国家/地区之分,没有字符集的差异 比如ti ...
- How to preview html file in our browser at sublime text?
sublime preview html.md open In Browser what should we do if we want to preview html file in our bro ...