简体   繁体   English

微服务-连接到单个旧数据库时的连接池

[英]Microservices - Connection Pooling when connecting to a single legacy database

I am working on developing micro services for a monolithic application using spring boot + spring cloud + spring JDBC. 我正在使用Spring Boot + Spring Cloud + Spring JDBC为单片应用程序开发微服务。 Currently, the application is connecting to a single database through tomcat JNDI connection pool. 当前,该应用程序正在通过tomcat JNDI连接池连接到单个数据库。

We have a bottleneck here, not to change the database architecture at this point of time because of various reasons like large number of db objects,tight dependencies with other systems,etc. 我们这里有一个瓶颈,由于各种原因(例如大量db对象,与其他系统的紧密依赖关系等),此时不更改数据库体系结构。

So we have isolated the micro services based on application features. 因此,我们根据应用程序功能隔离了微服务。 My concern is if we develop microservices with each having its own connection pool, then the number of connections to the database can increase exponentially. 我担心的是,如果我们开发的微服务每个都有自己的连接池,那么与数据库的连接数量会成倍增加。

Currently, I am thinking of two solutions 目前,我正在考虑两种解决方案

  1. To calculate the number of connections that is being used currently by each application feature and arriving at max/min connection params per service- which is a very tedious process and we don't have any mechanism to get the connection count per app feature. 要计算每个应用程序功能当前正在使用的连接数,并得出每个服务的最大/最小连接参数-这是一个非常繁琐的过程,我们没有任何机制来获取每个应用程序功能的连接数。

  2. To develop a data-microservice with a single connection pool which gets the query object from other MS, triggers the query to the database and returns the resultset object to the caller. 要开发具有单个连接池的数据微服务,该连接池将从其他MS获取查询对象,将查询触发到数据库,然后将结果集对象返回给调用方。

Not sure whether the second approach is a best practice in the microservices architechture. 不确定第二种方法是否是微服务架构中的最佳实践。

Can you please suggest any other standard approaches that can be helpful in the current situation? 您能否建议其他可以在当前情况下有所帮助的标准方法?

It's all about the tradeoffs. 这都是关于权衡的问题。

  1. To calculate the number of connections that is being used currently by each application feature and arriving at max/min connection params per service. 计算每个应用程序功能当前正在使用的连接数,并得出每个服务的最大/最小连接参数。

Cons : As you said, some profiling and guesswork needed to reach the sweet number of connection per app feature. 缺点 :就像您说的那样,需要进行一些分析和猜测才能达到每个应用程序功能的最佳连接数量。

Pros : Unlike the second approach, you can avoid performance overhead 优点 :与第二种方法不同,您可以避免性能开销

  1. To develop a data-microservice with a single connection pool which gets the query object from other MS, triggers the query to the database and returns the resultset object to the caller. 要开发具有单个连接池的数据微服务,该连接池将从其他MS获取查询对象,将查询触发到数据库,然后将结果集对象返回给调用方。

Pros : Minimal work upfront 优点 :最少的前期工作

Cons : one more layer, in turn one more failure point. 缺点 :多一层,又多一层故障点。 Performance will degrade as you have to deal with serialization -> Http(s) network latency -> deserialization->(jdbc fun stuff which is part of either approach) -> serialization -> Http(s) network latency -> deserialization . 由于您必须处理serialization -> Http(s) network latency -> deserialization->(jdbc fun stuff which is part of either approach) -> serialization -> Http(s) network latency -> deserialization性能会下降。 (In your case this performance cost may be negligible. But if every millisecond counts in your service, then this is a huge deciding factor) (在您的情况下,此性能成本可以忽略不计。但是,如果每毫秒都对您的服务很重要,那么这是一个巨大的决定因素)

In my opinion, I wouldn't split the application layer alone until I have analyzed my domains and my datastores. 我认为,在分析完域和数据存储之前,我不会单独拆分应用程序层。

This is a good read: http://blog.christianposta.com/microservices/the-hardest-part-about-microservices-data/ 这是一本好书: http : //blog.christianposta.com/microservices/the-hardest-part-about-microservices-data/

I am facing a similar dilemma at my work and I can share the conclusions we have reached so far. 我在工作中也面临着类似的困境,我可以分享到目前为止所得出的结论。

There is no silver bullet at the moment, so: 目前没有灵丹妙药,因此:

1 - Calculate the number of connections dividing the total desired number of connections for the instances of microservices will work well if you have a situation where your microservices don't need to drastically elastic scale. 1-如果您的微服务不需要大幅弹性扩展,请计算连接数除以微服务实例所需的总连接数即可。

2 - Not having a pool at all and let the connections be opened on demand. 2-根本没有游泳池,让连接按需打开。 This is what is being used in functional programming (like Amazon lambdas). 这就是函数编程(例如Amazon lambdas)中使用的东西。 It will reduce the total number of open connections but the downside is that you lose performance as per opening connections on the fly is expensive. 它将减少打开的连接总数,但缺点是您会失去性能,因为即时打开连接非常昂贵。

You could implement some sort of topic that let your service know that the number of instances changed in a listener and update the total connection number, but it is a complex solution and goes against the microservice principle that you should not change the configurations of the service after it started running. 您可以实现某种主题,以使您的服务知道侦听器中已更改的实例数并更新总连接数,但这是一个复杂的解决方案,并且违反了微服务原理,即您不应更改服务的配置它开始运行后。

Conclusion: I would calculate the number if the microservice tend to not grow in scale and without a pool if it does need to grow elastically and exponentially, in this last case make sure that a retry is in place in case it does not get a connection in the first attempt. 结论:如果微服务趋于不按比例增长,并且如果它确实需要弹性和指数增长,那么我将计算该数量,如果没有池,那么在最后一种情况下,请确保在没有连接的情况下重试了在第一次尝试中。

There is an interesting grey area here awaiting for a better way of controlling pools of connections in microservices. 这里有一个有趣的灰色区域,等待着更好的方法来控制微服务中的连接池。

In time, and to make the problem even more interesting, I recommend reading the 为了及时解决问题,我建议您阅读
article About Pool Sizing from HikariCP: https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing The ideal concurrent connections in a database are actually smaller than most people think. 关于HikariCP的池大小调整的文章: https : //github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing数据库中理想的并发连接实际上比大多数人想象的要小。

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

相关问题 Spring 微服务 Docker 问题(连接被拒绝和数据库连接) - Spring Microservices Docker Issues (Connection Refused and Database Connection) 如何在Spring Boot中使用自定义前缀配置数据库配置连接池? - How to configure database configuration connection pooling with custom prefix in Spring Boot? 如何限制 spring 启动应用程序运行微服务创建的数据库连接 - how to limit the database connection created by spring boot application running microservices 多个 Spring 引导应用程序连接到同一个 Postgres DB 的 PGBouncer 连接池? - PGBouncer connection pooling for Multiple Spring boot Applications connecting to same Postgres DB? spring WebClient 中的连接池 - Connection pooling in spring WebClient RestTemplate 连接池日志 - RestTemplate Connection Pooling Logs 连接池和实例扩展 - Connection pooling and scaling of instances 连接到(主)数据库的SQL Server远程连接 - SQL Server remote connection in connecting to (master) database 什么是微服务,与 MVC 有什么联系 - What is Microservices and is there a connection with MVC Spring RedisTemplate的连接池问题? - Connection pooling issues with Spring RedisTemplate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM