如何限制 spring 启动应用程序 运行 微服务创建的数据库连接
how to limit the database connection created by spring boot application running microservices
我正在做我的学校项目,用微服务架构制作一个应用程序(使用 maven 和 spring boot JPA)
我正在使用的硬件:
aws RDS(最大数据库连接数为 66,这导致了我的问题)
aws ec2 实例免费套餐
我有 3 个微服务:员工、薪水和休假。
但是,只有 3 个微服务 运行,我的数据库连接就达到了 40+。
我不知道如何限制服务可以创建的数据库连接数。
经过一些研究,我遇到了术语 "connection pool",所以我尝试设置 tomcat 连接池。
这是配置(我都做了)但它不起作用
如果配置正确,我的应用程序最多只能建立 5 个活动连接,我说得对吗?
spring.datasource.tomcat.initial-size=5
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=5
spring.datasource.tomcat.max-idle=5
spring.datasource.tomcat.min-idle=1
spring.datasource.tomcat.default-auto-commit=true
这是查询代码
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
@Query(
value = "select * from itsa.Employee;",
nativeQuery = true)
List<Employee> findAllEmployee();
@Query(
value = "SELECT * FROM Employee where id = :id",
nativeQuery = true)
List<Employee> findEmp(@Param("id") int id);
Spring 引导中的默认 CP 是 HikariCP。只要你在 pom.xml 中包含 spring-boot-starter-data-jpa,你就会为你配置默认值的 HikariCP。
如果你想让你的配置生效,你需要排除HikariCP并包含tomcat-cp。为此,您需要对 pom.xml.
进行以下更改
<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>
希望这有助于解决您的问题。
我正在做我的学校项目,用微服务架构制作一个应用程序(使用 maven 和 spring boot JPA) 我正在使用的硬件: aws RDS(最大数据库连接数为 66,这导致了我的问题) aws ec2 实例免费套餐 我有 3 个微服务:员工、薪水和休假。 但是,只有 3 个微服务 运行,我的数据库连接就达到了 40+。 我不知道如何限制服务可以创建的数据库连接数。
经过一些研究,我遇到了术语 "connection pool",所以我尝试设置 tomcat 连接池。
这是配置(我都做了)但它不起作用 如果配置正确,我的应用程序最多只能建立 5 个活动连接,我说得对吗?
spring.datasource.tomcat.initial-size=5
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=5
spring.datasource.tomcat.max-idle=5
spring.datasource.tomcat.min-idle=1
spring.datasource.tomcat.default-auto-commit=true
这是查询代码
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
@Query(
value = "select * from itsa.Employee;",
nativeQuery = true)
List<Employee> findAllEmployee();
@Query(
value = "SELECT * FROM Employee where id = :id",
nativeQuery = true)
List<Employee> findEmp(@Param("id") int id);
Spring 引导中的默认 CP 是 HikariCP。只要你在 pom.xml 中包含 spring-boot-starter-data-jpa,你就会为你配置默认值的 HikariCP。
如果你想让你的配置生效,你需要排除HikariCP并包含tomcat-cp。为此,您需要对 pom.xml.
进行以下更改<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>
希望这有助于解决您的问题。