lettuce

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>

standalone properties

1
2
3
4
5
## redis
spring.redis.database=0
spring.redis.host=wsl
spring.redis.port=6379
spring.redis.password=summer123!@#

cluster properties

  • code
    1
    2
    spring.redis.cluster.nodes=192.168.50.28:6379,192.168.50.28:6381,192.168.50.28:6383
    spring.redis.cluster.max-redirects=3

    测试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Service;


    @Service
    public class SimpleService {
    static Logger logger = LoggerFactory.getLogger(SimpleService.class) ;

    @Autowired
    StringRedisTemplate stringRedisTemplate ;

    public void run(){
    logger.info("redis连接工厂:{}",stringRedisTemplate.getConnectionFactory());

    //添加key
    stringRedisTemplate.opsForValue().set("user","张三");
    //获取key
    logger.info("从redis中获取key=user的值为:{}",stringRedisTemplate.opsForValue().get("user"));

    //删除key
    stringRedisTemplate.delete("user");

    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    import java.util.Locale;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Bean;

    import com.example.summermetrics.service.SimpleService;

    import io.micrometer.core.instrument.MeterRegistry;

    @SpringBootApplication
    public class SummerMetricsApplication {
    static {
    Locale.setDefault(Locale.CHINA);
    }

    public static void main(String[] args) {
    // SpringApplication.run(SummerMetricsApplication.class, args);
    ApplicationContext ctx = SpringApplication.run(SummerMetricsApplication.class, args);

    SimpleService simpleService = ctx.getBean(SimpleService.class) ;
    simpleService.run();
    }

    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(
    @Value("${spring.application.name}") String applicationName) {
    return (registry) -> registry.config().commonTags("application", applicationName);
    }
    }

    参考文章

  • Spring boot - data-redis与jedis关系

评论