
Spring Boot - Redis 连通性测试
发布日期:2021-05-06 23:02:09
浏览次数:27
分类:原创文章
本文共 6465 字,大约阅读时间需要 21 分钟。
文章目录
准备 Redis 服务器
部署 Redis
Redis 配置文件
提示:我的 Redis 部署在 /usr/local/redis-6.0.8
目录,配置文件存放在 /usr/local/redis
目录。
复制 /usr/local/redis-6.0.8/redis.conf
配置文件到 /usr/local/redis
目录:
$ cp /usr/local/redis-6.0.8/redis.conf /usr/local/redis
提示:因为我的测试程序与 Redis 服务器分别位于不同的主机,所以需要修改配置,方便测试程序访问 Redis 服务器。
编辑 /usr/local/redis
配置文件,注释 bind 127.0.0.1
(第 22 行),监听所有网卡的连接请求:
# By default, if no "bind" configuration directive is specified, Redis listens# for connections from all the network interfaces available on the server.# It is possible to listen to just one or multiple selected interfaces using# the "bind" configuration directive, followed by one or more IP addresses.## Examples:## bind 192.168.1.100 10.0.0.1# bind 127.0.0.1 ::1## ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the# internet, binding to all the interfaces is dangerous and will expose the# instance to everybody on the internet. So by default we uncomment the# following bind directive, that will force Redis to listen only into# the IPv4 loopback interface address (this means Redis will be able to# accept connections only from clients running into the same computer it# is running).## IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES# JUST COMMENT THE FOLLOWING LINE.# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#bind 127.0.0.1
设置认证密码(第 8 行):
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatiblity# layer on top of the new ACL system. The option effect will be just setting# the password for the default user. Clients will still authenticate using# AUTH <password> as usually, or more explicitly with AUTH default <password># if they follow the new protocol: both will work.## requirepass foobaredrequirepass 123456
启动 Redis 服务器
启动 Redis 服务器:
$ /usr/local/redis-6.0.8/src/redis-server /usr/local/redis/redis.conf
查看是否存在关于 redis-server
的进程,以此确认 Redis 服务器启动成功:
$ ps -ef | grep redisroot 4299 1632 0 17:02 pts/0 00:00:00 /usr/local/redis-6.0.8/src/redis-server *:6379root 4306 1985 0 17:03 pts/1 00:00:00 grep --color=auto redis
开放端口
开放 6379 端口:
$ firewall-cmd --list-all # 查看防火墙规则public (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: dhcpv6-client http ssh ports: 22122/tcp 23000/tcp 8761/tcp 8081/tcp 8080/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: $ firewall-cmd --zone=public --add-port=6379/tcp --permanent # 永久性地添加 6379/tcp 端口规则到 public 区域success$ firewall-cmd --reload # 重新加载防火墙规则success$ firewall-cmd --list-all # 查看防火墙规则,确认是否添加成功public (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: dhcpv6-client http ssh ports: 22122/tcp 23000/tcp 8761/tcp 8081/tcp 8080/tcp 6379/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
Java 客户端
项目基于 Spring Boot 2.3.4.RELEASE,新建 Maven Project,修改 pom.xml 文件,引入依赖:
<?xml version="1.0" encoding="UTF-8"?><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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.mk</groupId> <artifactId>spring-boot-redis</artifactId> <version>1.0.0</version> <name>spring-boot-redis</name> <description>Redis 缓存</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
application.yml 配置文件:
spring: redis: database: 0 # Database index used by the connection factory. host: 192.168.88.158 # Redis server host. port: 6379 # Redis server port. password: 123456 # Login password of the redis server. timeout: 3000 # Connection timeout. lettuce: pool: max-active: 8 max-wait: 1 min-idle: 0 max-idle: 8logging: level: com.mk: debug
测试类,这里通过缓存数据并尝试获取,以此测试 Java 客户端与 Redis 服务器之间的连通性:
package com.mk;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;@SpringBootTestclass SpringBootRedisApplicationTests { @Autowired private StringRedisTemplate stringRedisTemplate; @Test void contextLoads() { ValueOperations<String, String> opsForValue = this.stringRedisTemplate.opsForValue(); opsForValue.set("name", "lisi"); // 缓存数据 String value = opsForValue.get("name"); // 获取缓存数据 System.out.println(value); }}
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年03月31日 19时20分39秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Leetcode每日随机2021/4/30
2019-03-04
Java集合框架 List集合、Set集合、Map集合 学习泛型与包装类的装箱拆箱
2019-03-04
Java~分别用顺序表和链表实现栈和队列,以及库的栈和队列的使用
2019-03-04
MySQL~教你满分回答什么是数据库索引? 索引的数据结构是什么? 什么是事务?
2019-03-04
操作系统~进程的状态、转换、控制
2019-03-04
操作系统~线程概念以及多线程模型
2019-03-04
数据结构~缓存淘汰算法--LRU算法(Java的俩种实现方式,时间复杂度均为O(1))
2019-03-04
秒懂HTTP之URL与资源
2019-03-04
什么是正向代理?什么是反向代理?一分钟搞定
2019-03-04
Jmeter:性能测试中常见的名词(一)
2019-03-04
LInux:SELinux
2019-03-04
Python:高阶函数,柯里化Currying
2019-03-04
Python:函数 ----》装饰器函数
2019-03-04
Python:面向对象
2019-03-04
Python练习题 :随机生成一批数
2019-03-04
Spring源码:prepareBeanFactory(beanFactory);方法
2019-03-04
Spring源码:initApplicationEventMulticaster源码解析
2019-03-04
AcWing 786: 第k个数
2019-03-04
AcWing 798. 差分矩阵
2019-03-04
AcWing 828. 模拟栈
2019-03-04