1、定义2、pom3、yml4、实体类
1、定义Lettuce 能够⽀持 Reactive ⽅式
Spring Data Redis 中主要的⽀持
ReactiveRedisConnectionReactiveRedisConnectionFactoryReactiveRedisTemplate
opsForXxx() 2、pom
3、yml4.0.0 org.springframework.boot spring-boot-starter-parent2.1.3.RELEASE com.zhz reactive-spring-boot-reactive-redis-demo0.0.1-SNAPSHOT reactive-spring-boot-reactive-redis-demo Spring Boot中的reactive集成redis 1.8 org.springframework.boot spring-boot-starter-data-redis-reactiveorg.springframework.boot spring-boot-starter-jdbccom.h2database h2runtime org.projectlombok lomboktrue mysql mysql-connector-javaorg.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-maven-plugin
spring: datasource: url: jdbc:mysql://119.29.36.141:3306/spring-test?serverTimezone=GMT%2B8 username: root password: root hikari: connection-test-query: SELECt 1 connection-timeout: 60000 idle-timeout: 500000 max-lifetime: 540000 maximum-pool-size: 12 minimum-idle: 10 pool-name: GuliHikariPool output: ansi: enabled: always redis: host: localhost port: 6379 password: 123456 management: endpoints: web: exposure: include: '*' mybatis: type-handlers-package: com.zhz.springbootmybatisdemo.handler configuration: map-underscore-to-camel-case: true4、实体类
package com.zhz.reactive; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @Builder @NoArgsConstructor @AllArgsConstructor public class Coffee { private Long id; private String name; private Long price; }
5、测试类
package com.zhz.reactive; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; import org.springframework.data.redis.core.ReactiveHashOperations; import org.springframework.data.redis.core.ReactiveRedisTemplate; import org.springframework.data.redis.core.ReactiveStringRedisTemplate; import org.springframework.jdbc.core.JdbcTemplate; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; import java.time.Duration; import java.util.List; import java.util.concurrent.CountDownLatch; @Slf4j @SpringBootApplication public class ReactiveSpringBootReactiveRedisDemoApplication implements ApplicationRunner { private static final String KEY = "COFFEE_MENU"; @Autowired private JdbcTemplate jdbcTemplate; @Autowired private ReactiveStringRedisTemplate reactiveStringRedisTemplate; public static void main(String[] args) { SpringApplication.run(ReactiveSpringBootReactiveRedisDemoApplication.class, args); } @Bean public ReactiveStringRedisTemplate reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) { return new ReactiveStringRedisTemplate(reactiveRedisConnectionFactory); } @Override public void run(ApplicationArguments args) throws Exception { ReactiveHashOperationshashOperations = reactiveStringRedisTemplate.opsForHash(); CountDownLatch countDownLatch = new CountDownLatch(1); List coffees = jdbcTemplate.query("select * from t_coffee", (rs, i) -> Coffee.builder() .id(rs.getLong("id")) .name(rs.getString("name")) .price(rs.getLong("price")) .build() ); Flux.fromIterable(coffees) .publishOn(Schedulers.single()) .doOnComplete(() -> log.info("查询完毕")) .flatMap(c -> { log.info("try to put {},{}", c.getName(), c.getPrice()); return hashOperations.put(KEY, c.getName(), c.getPrice().toString()); }) .concatWith(reactiveStringRedisTemplate.expire(KEY, Duration.ofMinutes(1)))//1分钟 .doOnComplete(() -> log.info("expire ok")) .onErrorResume(e -> { log.error("exception {}", e.getMessage()); return Mono.just(false); }) .subscribe(b -> log.info("Boolean: {}", b), e -> log.error("Exception {}", e.getMessage()), () -> countDownLatch.countDown()); log.info("Waiting"); countDownLatch.await(); ; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)