01redis集成springboot

1 首先解决连接问题
(1)关闭防火墙
(2)修改redis配置 vim redis.cofig
(注释掉 bin127.0.0.1 即 #bin 127.0.0.1,后面写个bin 0.0.0.0)
(protected-mode yes 改为 protected-mode no (即该配置项表示是否开启保护模式,默认是开启,开启后Redis只会本地进行访问,拒绝外部访问)。)
2 开启springboot整合redis
(1)导包

<dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
      </dependency>

(2)配置文件
spring.redis.database=0
# Redis服务器地址
spring.redis.host=116.62.203.162
# Redis服务器连接端口
spring.redis.port=6379
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000
(3)将RedisTemp注入容器
 @Bean
    public RedisTemplate redisTemplate(){
        return new RedisTemplate();
    }

(4)代码实现测试连接redis

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void contextLoads() {
        redisTemplate.opsForValue().set("name","paike");
        System.out.println(redisTemplate.opsForValue().get("name"));
        System.out.println("连接成功");
    }
集成RedisSpring Boot时,您需要完成以下步骤: 1. 导入Redis依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置Redis连接信息: 在application.properties或application.yml文件中添加以下配置: ```properties spring.redis.host=127.0.0.1 spring.redis.port=6379 ``` 这里的host和port分别是Redis服务器的主机名和端口。 3. 创建Redis配置类(可选): 您可以创建一个Redis配置类来自定义Redis连接池等配置。例如: ```java @Configuration public class RedisConfig { @Bean public JedisConnectionFactory jedisConnectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); config.setHostName("127.0.0.1"); config.setPort(6379); return new JedisConnectionFactory(config); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(jedisConnectionFactory()); return template; } } ``` 这里的配置类创建了一个Jedis连接工厂,并将其用于创建RedisTemplate。 4. 在您的应用程序中使用Redis: 您可以在需要使用Redis的地方注入RedisTemplate,并使用其方法来执行Redis操作。例如,您可以使用RedisTemplate的opsForValue()方法来访问Redis的字符串值操作。 ```java @Autowired private RedisTemplate<String, Object> redisTemplate; public void setValue(String key, String value) { redisTemplate.opsForValue().set(key, value); } public String getValue(String key) { return (String) redisTemplate.opsForValue().get(key); } ``` 这些步骤将帮助您在Spring Boot应用程序中集成Redis。请确保您的应用程序已正确配置和依赖于Redis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值