连接时从Spring Boot项目到PostgreSQL数据库的连接问题

时间:2020-04-16 07:43:08

标签: spring postgresql spring-boot

我正在使用Spring MVC,Spring Boot和PostgreSQL开发面向微服务的多租户应用程序。在我的服务域中,我有30个不同的Spring Boot项目连接到同一数据库。

问题

当我同时启动多个11个Spring Boot项目时,我无法从数据库中检索数据。我只收到空白的JSON响应。但是,如果我仅启动少于11个项目,那么到那时,我就能获得微服务结果。从12号服务开始时遇到问题。

问题排查和我的调查

根据阅读,我做了如下一些改动,

我设置了PostgreSQL配置文件max_connections = 100shared_buffers = 128MB

但这并不能解决我的问题。并在application.property的{​​{1}}连接池中添加了其他行,如下所示,

hikari

即使这也不能解决我的问题。仍然我一次只能启动最多12个Spring Boot项目。

在pom.xml上添加了以下内容,

spring.datasource.hikari.minimumIdle=3
spring.datasource.hikari.maximum-pool-size=3

application.property已更新为以下内容,

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-jpa</artifactId>
 <exclusions>
    <exclusion>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-jdbc</artifactId>
  <version>9.0.10</version>
</dependency>

更新

如上所述,我总共有100个最大连接。当我启动第一个微服务并通过查询(在查询后)监视数据库连接时,我得到10个连接。

spring.datasource.tomcat.initial-size=15
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=20
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=5
spring.datasource.tomcat.default-auto-commit=true

活动屏幕截图-当我仅启动1个微服务时,我会得到这样的提示,

enter image description here

活动屏幕截图2:

enter image description here

因此,在这里,对于每个启动的微服务,后端数据库级别都将启动10个数据库连接。因此,如果我要启动30多个微服务应用程序,它将连接300多个连接。

困惑

  1. 这是因为有任何数据库默认设置吗?
  2. 这是由于连接到DB引擎时的Tomcat问题引起的吗?

即使我尝试在数据库节点中使用连接池工具select max_conn,used,res_for_super,max_conn-used-res_for_super res_for_normal from (select count(*) used from pg_stat_activity) t1, (select setting::int res_for_super from pg_settings where name=$$superuser_reserved_connections$$) t2, (select setting::int max_conn from pg_settings where name=$$max_connections$$) t3 。然后我也遇到了同样的问题。

如果这是由于数据库默认设置或配置引起的,我该如何覆盖呢?

1 个答案:

答案 0 :(得分:1)

微服务体系结构是一种将应用程序分解为多个独立服务的体系结构,这些服务易于理解,开发,测试并且容易出错。

现在,根据上面的定义,每个独立的服务都应该有自己独立的独立数据库。但是,如果需要,这些服务可以相互通信以获取数据。

每个服务应在其特定数据库中具有选定的实体。

如果您为所有这些独立服务拥有一个公共数据库,那么您就违反了微服务体系结构的规则。

在这种情况下,存在很高的数据冗余风险。

在您的情况下,可能是服务包含重复的数据版本,并可能耗尽整个连接池。

更好的方法是为单独的独立服务提供单独的数据库(仅包含特定于该服务的实体)。

说明:

  1. 上面解释。

  2. 为所有spring boot模块/项目维护单独的数据源hikari连接池。

例如:假设您有两个模块/项目的两个数据源-

在application.properties中。

## datasource for first service/module ##
datasource1.url=jdbc:postgresql://localhost:3306/service1
datasource1.username=service1
datasource1.password=password1
datasource1.driver-class-name=org.postgresql.Driver
datasource1.initial-size=15
datasource1.max-wait=20000
datasource1.max-active=20
datasource1.max-idle=20
datasource1.min-idle=5
datasource1.default-auto-commit=true

## datasource for second service/module ##
datasource2.url=jdbc:postgresql://localhost:3306/service2
datasource2.username=service2
datasource2.password=password2
datasource2.driver-class-name=org.postgresql.Driver
datasource2.initial-size=15
datasource2.max-wait=20000
datasource2.max-active=20
datasource2.max-idle=20
datasource2.min-idle=5
datasource2.default-auto-commit=true

点击此链接以了解如何配置:

  1. https://blogs.ashrithgn.com/multiple-data-source-in-spring-boot-using-spring-boot-data-starter/

  2. https://www.ru-rocker.com/2018/01/28/configure-multiple-data-source-spring-boot/

或者您可能需要使用Hikari-CP进行Hibernate Multi-tenancy。