Reactive访问Spring Data Redis
【摘要】
文章目录
1、定义2、pom3、yml4、实体类
1、定义
Lettuce 能够⽀持 Reactive ⽅式 Spring Data Redis 中主要的⽀持
ReactiveRedis...
1、定义
Lettuce 能够⽀持 Reactive ⽅式
Spring Data Redis 中主要的⽀持
- ReactiveRedisConnection
- ReactiveRedisConnectionFactory
- ReactiveRedisTemplate
- opsForXxx()
2、pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zhz</groupId>
<artifactId>reactive-spring-boot-reactive-redis-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>reactive-spring-boot-reactive-redis-demo</name>
<description>Spring Boot中的reactive集成redis</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 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
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
3、yml
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: true
- 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
4、实体类
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
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 {
ReactiveHashOperations<String, String, String> hashOperations = reactiveStringRedisTemplate.opsForHash();
CountDownLatch countDownLatch = new CountDownLatch(1);
List<Coffee> 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();
;
}
}
- 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
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
文章来源: blog.csdn.net,作者:zhz小白,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/zhouhengzhe/article/details/122631585
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)