springboot集成

maven配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.11.1</version>
</dependency>

 application.yaml配置

spring:
  application:
    name: daisy-web-test
  redis:
    host: 127.0.0.1
    port: 6379
    password: 123456
    lettuce:
      pool:
        max-active: 10
        max-idle: 10
        min-idle: 1
        time-between-eviction-runs: 10s
        enabled: true
    sentinel:
      master: mymaster
      password: 123456
      nodes: 192.168.2.168:26379,192.168.2.168:26380,192.168.2.168:26381

RedisConfig配置



import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        //大多数情况,都是选用<String, Object>
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        StringRedisSerializer serializer = new StringRedisSerializer();
        template.setKeySerializer(serializer);
        template.setHashKeySerializer(serializer);
        return template;
    }


}

cotroller测试


import cn.hutool.core.lang.UUID;
import com.daisy.web.test.util.redis.CacheUtilManager;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.core.lang.UUID;
import com.daisy.web.test.util.redis.CacheUtilManager;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

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

	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * http://127.0.0.1:8888/redis/set
	 * @return
	 */
	@RequestMapping("/set")
	public String user() {
		try {
			for (int i = 0; i < Integer.MAX_VALUE; i++) {
				TimeUnit.SECONDS.sleep(2);
				String key="name";

				Object o = redisTemplate.opsForValue().get(key);
				System.out.println("old value : "+o);
				String value= java.util.UUID.randomUUID().toString();
				redisTemplate.opsForValue().set(key, value);
				Object o2 = redisTemplate.opsForValue().get(key);
				System.out.println("new value : "+o2);
			}
			return UUID.randomUUID().toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}

}

启动项目访问

测试地址 http://127.0.0.1:8888/redis/set

查看日志

 异常测试

停掉集群中的master节点,观察日志,发现报错,然后从节点变为主节点后又连接成功,可以继续使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕菲烟云

坚持原创,希望大家多多交流

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值