问题描述
我们在项目中使用 spring boot(2.3.1) 反应式编程。使用的数据库是 r2dbc-postgres (0.8.7)。我们无法找出使用reactive编写的api在连接到数据库后停止响应的根本原因。
例如在以下代码中:
@Autowired
PlanPackageCurrencyPriceRepository planPackageCurrencyPriceRepository;
public Mono<Object> viewBySkuCodeAndCountryCode(String skuCode,String countryCode) {
Mono<PlanPackageCurrencyPrice> planPackagePriceInfo = planPackageCurrencyPriceRepository
.findBySkuCodeAndCountryCode(skuCode,countryCode);
return planPackagePriceInfo.map(planInfo -> {
PlanPackageCurrencyPriceDTO currencyPriceDTO = PlanPackageCurrencyPriceDTO.builder()
.skuCode(planInfo.getSkuCode())
.countryCode(planInfo.getCountryCode())
.currencyCode(planInfo.getCurrencyCode())
.price(planInfo.getPrice())
.status(planInfo.getStatus())
.build();
if(planInfo.getStatus() == Status.ACTIVE) {
final Mono<Boolean> monovalue = redistemplate.opsForHash().put("getplanpackagecurrencycodeprice",skuCode + countryCode,currencyPriceDTO);
logger.info(REdis_VALUE,monovalue.subscribe(System.out::println));
return currencyPriceDTO;
} else {
logger.debug(serviceName.concat(LoggerConstants.PLAN_PACKAGE_GROUP_INFO_VIEW_DEBUG_LOG)
.concat(" No items found for Plan/Package Group Info for the sku code {} "),skuCode);
throw new CustomException("VIEW_ERRORMESSAGE",HttpStatus.MULTI_STATUS,10006);
}
});
}
当使用 planPackageCurrencyPriceRepository 对 DB 进行查询时,日志在此查询处停止,以下是超时前看到的响应
2021-03-07 10:52:47.427 DEBUG 1 --- [tor-tcp-epoll-4] o.s.d.r.c.R2dbcTransactionManager : Acquired Connection [Monoretry] for R2DBC transaction
2021-03-07 10:52:47.427 DEBUG 1 --- [tor-tcp-epoll-4] o.s.d.r.c.R2dbcTransactionManager : Switching R2DBC Connection [PooledConnection[PostgresqlConnection{client=io.r2dbc.postgresql.client.ReactorNettyClient@7d1a251f,codecs=io.r2dbc.postgresql.codec.DefaultCodecs@7925be64}]] to manual commit
给一些时间。 API 响应错误提示连接超时。 但是如果我们重新启动我们的 docker 容器它就可以正常工作了。然后在一段时间后观察到相同的行为。我们无法为这种间歇性行为找到解决方案。
以下是使用的数据库配置:
@Configuration
@EnableR2dbcRepositories(basePackages = "com.crm.smsauth.postgresrepo")
public class DatabaseConfig extends AbstractR2dbcConfiguration {
@Value("${postgres.host}")
private String host;
@Value("${postgres.protocol}")
private String protocol;
@Value("${postgres.username}")
private String username;
@Value("${postgres.password}")
private String password;
@Value("${postgres.database}")
private String database;
@Override
@Bean
public ConnectionFactory connectionFactory() {
final ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
.option(ConnectionFactoryOptions.DRIVER,"pool")
.option(ConnectionFactoryOptions.PROTOCOL,protocol)
.option(ConnectionFactoryOptions.HOST,host)
.option(ConnectionFactoryOptions.USER,username)
.option(ConnectionFactoryOptions.PASSWORD,password)
.option(ConnectionFactoryOptions.DATABASE,database)
.option(MAX_SIZE,1000)
.option(INITIAL_SIZE,1)
.build());
return connectionFactory;
}
@Bean
ReactiveTransactionManager transactionManager(ConnectionFactory connectionFactory) {
return new R2dbcTransactionManager(connectionFactory);
}
}
编辑 2:
Postgres 日志:数据库名称为 planPackage。
2021-03-07 16:26:47.389 IST [24368] postgres@planpackage LOG: Could not receive data from client: Connection timed out
两个日志的时间戳不匹配,因为我们的部署虚拟机的时区设置为 GMT,但 postgres 的时区是 IST。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)