配置mysql和pgsql双数据源

我在实现基于postgreSQL实现RAG对话的时候碰到了

Caused by: org.postgresql.util.PSQLException: ERROR: relation "chat_message" does not exist

原因是我之前实现过对话持久到mysql数据库,现在又行加了一个数据库Spring默认后面新加的为主数据库,然而这个数据库中并没有这样的表结构,所以报错了。

爱转牛角尖的我肯定不允许这样的情况存在,下面是我修复这个问题的过程

优化配置

yaml
复制代码
spring: datasource: postgres: driver-class-name: org.postgresql.Driver url: jdbc:postgresql://xxx:5432/xxx username: xxx password: xxx mysql: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://xxx username: xxx password: xxx

新建配置类

java
复制代码
/** * @version v1.0.0 * @belongsProject: feng-ai-agent * @belongsPackage: com.qcdfz.fengaiagent.config * @author: fgh * @description: 多数据源配置类 * @createTime: 2025-05-10 12:14 */ @Configuration public class DataSourceConfig { /** 绑定并创建 MySQL 数据源 */ @Bean @Primary @ConfigurationProperties("spring.datasource.mysql") public DataSourceProperties mysqlProperties() { return new DataSourceProperties(); } @Bean @Primary public DataSource mysqlDataSource() { return mysqlProperties() .initializeDataSourceBuilder() .build(); } /** MySQL 上下文的 JdbcTemplate,其他组件默认注入 */ @Bean @Primary public JdbcTemplate mysqlJdbcTemplate( @Qualifier("mysqlDataSource") DataSource ds) { return new JdbcTemplate(ds); } /** 绑定并创建 PostgreSQL 数据源 */ @Bean @ConfigurationProperties("spring.datasource.postgres") public DataSourceProperties postgresProperties() { return new DataSourceProperties(); } @Bean public DataSource postgresDataSource() { return postgresProperties() .initializeDataSourceBuilder() .build(); } /** PostgreSQL 专用的 JdbcTemplate,供 PgVectorStore 使用 */ @Bean public JdbcTemplate postgresJdbcTemplate( @Qualifier("postgresDataSource") DataSource ds) { return new JdbcTemplate(ds); } }

此时mysql的使用不用做任何改变,是用pgsql的案例如下:

多数据源使用

java
复制代码
/** * @version v1.0.0 * @belongsProject: feng-ai-agent * @belongsPackage: com.qcdfz.fengaiagent.rag * @author: fgh * @description: PgVector配置加载类 * @createTime: 2025-05-10 08:56 */ @Configuration public class PgVectorVectorStoreConfig { @Resource private LoveAppDocumentLoader loveAppDocumentLoader; @Bean public VectorStore pgVectorVectorStore(@Qualifier("postgresJdbcTemplate") JdbcTemplate jdbcTemplate,EmbeddingModel dashscopeEmbeddingModel){ PgVectorStore vectorStore = PgVectorStore.builder(jdbcTemplate, dashscopeEmbeddingModel) .dimensions(1536) // Optional: defaults to model dimensions or 1536 .distanceType(COSINE_DISTANCE) // Optional: defaults to COSINE_DISTANCE .indexType(HNSW) // Optional: defaults to HNSW .initializeSchema(true) // Optional: defaults to false .schemaName("public") // Optional: defaults to "public" .vectorTableName("vector_store") // Optional: defaults to "vector_store" .maxDocumentBatchSize(10000) // Optional: defaults to 10000 .build(); List<Document> documents = loveAppDocumentLoader.loadMarkdowns(); int batchSize = 25; // 根据 DashScope 接口限制设置批次大小 for (int i = 0; i < documents.size(); i += batchSize) { int end = Math.min(i + batchSize, documents.size()); List<Document> subList = documents.subList(i, end); vectorStore.add(subList); } return vectorStore; } }

使用@Qualifier("postgresJdbcTemplate")注解来区分使用哪个数据源

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
青春的疯子
下载 APP