Spring Boot - Redis 连通性测试

准备 Redis 服务器

部署 Redis

在 CentOS 7 系统中安装 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 foobared
requirepass 123456

启动 Redis 服务器

启动 Redis 服务器:

$ /usr/local/redis-6.0.8/src/redis-server /usr/local/redis/redis.conf

查看是否存在关于 redis-server 的进程,以此确认 Redis 服务器启动成功:

$ ps -ef | grep redis
root       4299   1632  0 17:02 pts/0    00:00:00 /usr/local/redis-6.0.8/src/redis-server *:6379
root       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: 8

logging:
  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;

@SpringBootTest
class 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);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_29761395/article/details/108714516