Spring Boot 连接 CentOS Redis

介绍

Redis是一个开源的、基于内存的高性能键值对存储数据库。它支持多种数据结构,如字符串、哈希、列表、集合、有序集合等,并提供了丰富的操作命令。Spring Boot是一个用于创建独立的、生产级别的基于Spring的应用程序的框架,它简化了Spring应用程序的配置和部署。

本文将介绍如何使用Spring Boot连接到CentOS上的Redis数据库,并提供一个简单的示例来演示如何进行连接和操作。

准备工作

在开始之前,我们需要确保已经在CentOS上安装了Redis,并启动了Redis服务器。

可以通过以下命令安装Redis:

sudo yum install redis

启动Redis服务器:

sudo systemctl start redis

引入依赖

首先,我们需要在pom.xml文件中添加Redis的依赖:

<dependencies>
    <!-- 其他依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
</dependencies>

配置连接信息

接下来,我们需要在application.properties文件中配置Redis连接信息。可以根据实际情况修改以下配置:

# Redis连接信息
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=

连接Redis

现在,我们可以使用Spring Boot提供的RedisTemplate来连接和操作Redis了。首先,我们需要创建一个RedisConfig类,用于配置Redis连接:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;

@Configuration
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(redisHost);
        jedisConnectionFactory.setPort(redisPort);
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory());
        template.setValueSerializer(new GenericToStringSerializer<>(Object.class));
        return template;
    }
}

在上述代码中,我们使用JedisConnectionFactory为Redis提供连接工厂,并配置连接的主机名和端口号。然后,我们使用RedisTemplate作为Redis操作的核心类。

操作Redis

现在,我们可以使用RedisTemplate来进行Redis操作了。以下是一个简单的示例,演示了如何设置和获取键值对:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisExample {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void setKey(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public Object getValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}

在上述示例中,我们使用redisTemplateopsForValue()方法获取ValueOperations对象,然后可以使用该对象进行操作。

示例应用

为了演示如何使用Spring Boot连接到CentOS上的Redis,我们创建一个简单的Web应用程序,用户可以通过界面设置和获取键值对。

首先,我们创建一个Controller类,用于处理用户的请求:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisExample redisExample;

    @PostMapping("/{key}/{value}")
    public void setKeyValue(@PathVariable String key, @PathVariable String value) {
        redisExample.setKey(key, value);
    }

    @GetMapping("/{key}")
    public Object getValueByKey(@PathVariable String key) {
        return redisExample.getValue(key);
    }
}

在上述代码中,我们使用@RestController注解将该类标记为一个处理HTTP请求的控制器,并使用@RequestMapping注解指定请求的路径。@Autowired注解用于自动注入RedisExample类的实例。