使用Sping Boot 的Spring R2DBC多数据源

3bygqnnd  于 2022-12-02  发布在  Spring
关注(0)|答案(1)|浏览(437)

我创建了一个连接到两个模式(例如fo_pgdb,if_pgdb)的服务。我的问题是,当服务查询if_pgdb模式中的表时,它看起来像是在查询fo_pgdb模式中的表。我已经检查并硬编码了两个类属性中的数据库URL(如下面的代码示例所示)。问题可能是什么?
示例:对fo_pgdb方案中的表的查询是“select * from bid_lines,其中(123,345)中的bidlinseqnumber返回结果集。因为id 123和345在表中有记录。
对if_pgdb方案中的表的查询是“select * from bid_lines,其中(567,8910)中的bidlinseqnumber返回空结果集。但ID为567和8910的那些记录在表中。
测试:当我在if_pgdb模式的表上使用id 123和345进行查询时,我得到的表中的记录与fo_pgdb表中的记录相同。

@Configuration
@EnableR2dbcRepositories(entityOperationsRef = "foEntityTemplate", basePackages = "com.r2dbc.poc.repository")
public class FODatabaseConfig {

    //@Value("${spring.r2dbc.fo.connection.url}")
    private String url = "r2dbc:postgresql://username:password@database-dev-fo-css-rr-db.corp.com:1200/fo_pgdb";

    @Bean
    @Qualifier("foConnectionFactory")
    public ConnectionFactory foConnectionFactory() {
        return ConnectionFactories.get(url);
    }

    @Bean
    public R2dbcEntityOperations foEntityTemplate(@Qualifier("foConnectionFactory") ConnectionFactory connectionFactory) {
        DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);
        DatabaseClient databaseClient = DatabaseClient.builder()
                .connectionFactory(connectionFactory)
                .bindMarkers(PostgresDialect.INSTANCE.getBindMarkersFactory())
                .build();

        return new R2dbcEntityTemplate(databaseClient, strategy);
    }
    
}

@Configuration
@EnableR2dbcRepositories(entityOperationsRef = "ifEntityTemplate")
public class IFDatabaseConfig {

    //@Value("${spring.r2dbc.if.connection.url}")
    private String url = "r2dbc:postgresql://username:password@database-blue-if-CSS-db.corp.com:1200/if_pgdb";

    @Bean
    @Qualifier("ifConnectionFactory")
    public ConnectionFactory ifConnectionFactory() {
        return ConnectionFactories.get(url);
    }

    @Bean
    public R2dbcEntityOperations ifEntityTemplate(@Qualifier("ifConnectionFactory") ConnectionFactory connectionFactory) {
        DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);
        DatabaseClient databaseClient = DatabaseClient.builder()
                .connectionFactory(connectionFactory)
                .bindMarkers(PostgresDialect.INSTANCE.getBindMarkersFactory())
                .build();

        return new R2dbcEntityTemplate(databaseClient, strategy);
    }
}

@Service
@RequiredArgsConstructor
public class CrewMemberSchedulePeriodPaymentService {

    private final FOCrewMemberBidLineRepository foCrewMemberBidlineRepository;

    private final IFCrewMemberBidLineRepository ifCrewMemberBidLineRepository;

    public Flux<FOCrewMemberBidLine> getFOBidLines(List<Long> id) {
        return foCrewMemberBidlineRepository.findAllById(id);
    }

    public Flux<IFCrewMemberBidLine> getIFBidLines(List<Long> id) {
       return ifCrewMemberBidLineRepository.findAllById(id);
    }

}

@Repository
public interface FOCrewMemberBidLineRepository extends R2dbcRepository<FOCrewMemberBidLine, Long> {

    @Override
    Flux<FOCrewMemberBidLine> findAllById(Iterable<Long> longs);
}

@Repository
public interface IFCrewMemberBidLineRepository extends R2dbcRepository<IFCrewMemberBidLine, Long> {
    @Override
    Flux<IFCrewMemberBidLine> findAllById(Iterable<Long> longs);
}

@Table(value = "BID_LINES")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class FOCrewMemberBidLine {

    @Id
    @Column(value = "bidlinseqnumber")
    private Long bidlinseqnumber;

    @Column(value = "bidlinschedperiod")
    private String bidlinschedperiod;
}

@Table(value = "BID_LINES")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class IFCrewMemberBidLine {

    @Id
    @Column(value = "bidlinseqnumber")
    private Long bidlinseqnumber;

    @Column(value = "bidlinschedperiod")
    private String bidlinschedperiod;
}
kuuvgm7e

kuuvgm7e1#

也许你可以在方法中添加连接工厂,如下所示:

@Bean
    @Qualifier("foConnectionFactory")
    public ConnectionFactory foConnectionFactory() {
        return ConnectionFactories.get("r2dbc:postgresql://username:password@database-dev-fo-css-rr-db.corp.com:1200/fo_pgdb");
  }

@Bean
@Qualifier("ifConnectionFactory")
public ConnectionFactory ifConnectionFactory() {
    return ConnectionFactories.get("r2dbc:postgresql://username:password@database-blue-if-CSS-db.corp.com:1200/if_pgdb");
}
     
@Bean
    public R2dbcEntityOperations ifEntityTemplate() {
            DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);
        DatabaseClient databaseClient = DatabaseClient.builder()
                .connectionFactory(ifConnectionFactory()) //<-- change
                .bindMarkers(PostgresDialect.INSTANCE.getBindMarkersFactory())
                .build();

        return new R2dbcEntityTemplate(databaseClient, strategy);
    }

@Bean
    public R2dbcEntityOperations foEntityTemplate() {
        DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);
        DatabaseClient databaseClient = DatabaseClient.builder()
                .connectionFactory(foConnectionFactory()) //<-- change
                .bindMarkers(PostgresDialect.INSTANCE.getBindMarkersFactory())
                .build();

        return new R2dbcEntityTemplate(databaseClient, strategy);
    }

您可以将所有Bean放在同一个类中,并且将使用所需的连接工厂创建每个Bean。
干杯干杯干杯

相关问题