简体   繁体   English

Spring 带 Spring 引导的 R2DBC 多数据源

[英]Spring R2DBC Multi Datasource with Spring Boot

I created a service that connects to two schemas (ex. fo_pgdb, if_pgdb) My issue is when the service queries the table in the if_pgdb schema it looks as though it is querying the table in the fo_pgdb schema.我创建了一个连接到两个模式(例如 fo_pgdb、if_pgdb)的服务 我的问题是,当服务查询 if_pgdb 模式中的表时,它看起来好像在查询 fo_pgdb 模式中的表。 I have checked and hard coded the database URLs in both class attributes (shown in code examples below) look fine.我已经检查并硬编码了两个 class 属性中的数据库 URL(如下面的代码示例所示)看起来不错。 What could be the issue?可能是什么问题?

example: query on table in fo_pgdb schema is "select * from bid_lines where bidlinseqnumber in (123, 345) returns a result set. because ids 123 and 345 have records in the table.示例:查询 fo_pgdb 模式中的表是“select * from bid_lines where bidlinseqnumber in (123, 345) returns a result set. 因为 ids 123 和 345 在表中有记录。

query on table in if_pgdb schema is "select * from bid_lines where bidlinseqnumber in (567, 8910) returns empty result set. But ids 567 and 8910 those records with those ids are in the table. if_pgdb 模式中表的查询是“select * from bid_lines where bidlinseqnumber in (567, 8910) returns empty result set. 但是 ids 567 和 8910 那些具有这些 ids 的记录在表中。

test: when I use the ids 123 and 345 in the query on the table in the if_pgdb schema I get the same records that are in the table that are in the fo_pgdb table.测试:当我在 if_pgdb 模式的表查询中使用 id 123 和 345 时,我得到的记录与 fo_pgdb 表中的表中的记录相同。 That should not happen.那不应该发生。

@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;
}

Maybe be you can add the connection factory invkoing the method, like this:也许您可以添加连接工厂 invkoing 方法,如下所示:

@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);
    }

You can have all of your beans in the same class and each bean will be created with the connection factory that you need.您可以将所有 bean 都放在同一个 class 中,并且每个 bean 都将使用您需要的连接工厂创建。

Cheers.干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM