方式一:使用自动化配置
1,添加项目依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<!--添加r2dbc的postgresql实现-->
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-pool</artifactId>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!--添加swagger的依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-webflux</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-bom-r2dbc</artifactId>
<version>0.1.0.M3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2,创建Repository
@Repository
public interface ProvinceReactiveRepository extends R2dbcRepository<Province, String> {
@Query("select * from MGT_PROVINCE where privince_id=:id")
Flux<Plan> findById(String id);
}
注意:默认r2dbc不支持“从方法名创建查询”的方式,如果将上面方法中的注解@Query去掉,则启动时会报以下异常:
Caused by: java.lang.UnsupportedOperationException: Query derivation not yet supported!
3,添加配置
spring.r2dbc.url=r2dbc:postgresql://192.168.30.161:5432/postgres
spring.r2dbc.username=postgres
spring.r2dbc.password=postgres
注意:如果没有配置上面的参数,则会抛出以下异常
Description:
Failed to configure a ConnectionFactory: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable R2DBC Connection URL
Action:
Consider the following:
If you want an embedded database (H2), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
对于更多的配置请参考:https://github.com/spring-projects-experimental/spring-boot-r2dbc
4,启动应用
@EnableWebFlux
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
默认情况下,Spring Boot启用R2DBC存储库(R2DBC repository)支持,并查找@SpringBootApplication所在的包(及其子包)。如果要改变这种默认行为,可以在启动类上添加@EnableR2dbcRepositories注解并指定其basePackage属性。
方式二:手动配置
1,移除自动配置的依赖
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-autoconfigure-r2dbc</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-test-autoconfigure-r2dbc</artifactId>
</exclusion>
</exclusions>
</dependency>
2,自定义配置
@Configuration
@Conditional(PostgresqlCondition.class)
@EnableR2dbcRepositories(basePackages = "com.ultrapower.mgt.team.repository.r2dbc", databaseClientRef = "databaseClient")
@ConfigurationProperties(prefix = "spring.datasource.master")
@EnableTransactionManagement() // R2dbcTransactionManager
public class PostgresqlConfig extends AbstractR2dbcConfiguration {
private Logger log = LoggerFactory.getLogger(getClass());
private String host;
private String database;
private String username;
private String password;
private Integer port = 5432;
public ConnectionFactory connectionFactory() {
log.info("host:{};database:{};username:{};password:{}", host, database, username, password);
ConnectionFactory connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration
.builder().host(host).database(database).username(username).password(password).port(port).build());
return connectionFactory;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setPort(Integer port) {
this.port = port;
}
public void setHost(String host) {
this.host = host;
}
public void setDatabase(String database) {
this.database = database;
}
}
自动化配置简单说明
在spring-boot-autoconfigure-r2dbc-0.1.0.M3.jar/META-INF/spring.factories中自动启动类的配置如下所示:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration, \
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcTransactionManagerAutoConfiguration, \
org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.reactive.ReactiveTransactionAutoConfiguration
# Failure analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryBeanCreationFailureAnalyzer
这几个自动配置类的启动顺序如下所示
1,ConnectionFactoryAutoConfiguration
2,R2dbcDataAutoConfiguration
3,R2dbcRepositoriesAutoConfiguration
-----------------------------------------------------
1,R2dbcTransactionManagerAutoConfiguration
2,TransactionAutoConfiguration;ReactiveTransactionAutoConfiguration
参考链接:https://r2dbc.io/spec/
参考链接:https://r2dbc.io/spec/0.8.0.RELEASE/spec/html/
参考链接:https://github.com/r2dbc/r2dbc-postgresql
参考链接:https://docs.spring.io/spring-data/r2dbc/docs/1.0.x/reference/html/#preface
参考链接:https://spring.io/guides/gs/accessing-data-r2dbc/











网友评论