In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Overview
In the process of running the project, some operations consume a lot of system resources, such as establishing database connection, establishing Redis connection and so on. We hope to create multiple connection objects at one time, and use the created connection directly when we need to use it in the future, so as to improve performance. Pooling technology achieves the purpose of improving the performance of application services by initializing some objects that take up more resources in advance and saving the initialized objects in the pool for standby. JDBC connection pooling and Jedis connection pooling in the database use pooling technology.
Apache-Commons-Pool2 provides a set of specification interfaces for pooling technology and general logic for implementation, and we only need to implement the methods abstracted from it. This blog post mainly shares the application of Grpc connection pooling based on Apache-Commons-Pool2.
About the content related to Grpc, if you want to know the basic implementation methods, you can refer to my other blog (portal): https://blog.51cto.com/andrewli/2058908
Core component
Let's first take a look at several core components involved in the Apache-Commons-Pool2 specification interface, including:
ObjectPool
Object pooling, which is used to store objects and manage their pooling and pooling. The implementation class of object pool is GenericObjectPool;PoolConfig
Pool properties, which are used to set some configuration information about the connection pool, such as the maximum pool capacity, the processing logic after exceeding the pool capacity, and so on. The implementation class of the pool property is: GenericObjectPoolConfig;ObjectFactory
Object factory, generate new object instances when needed, and put them in the pool. The interface of the object factory is: interface PooledObjectFactory;ClientObject
Pooled objects are created by the object factory and put into the object pool; when needed, they are taken out of the object pool, the business logic of the object is executed, and then put back into the object pool after use. The interface of the pool object is: interface PooledObject. The dependency of core components and the dependency between workflow interfaces and classes
Before combing through the workflows of core components related to connection pooling, let's take a look at the inheritance and implementation relationships between classes and interfaces involved in core components.
Inheritance relationship of object pool class
The top-level interface of object pooling is ObjectPool, which defines the basic methods of object pooling, including adding, retrieving, verifying and returning objects, as well as obtaining the number of objects in Idle hibernation, obtaining the number of objects in Active state, emptying the pool, and closing the pool.
Abstract class BaseGenericObjectPool defines the initial configuration of object pool and implements the basic interface methods of object pool.
The pool class GenericObjectPool inherits the abstract class BaseGenericObjectPool and implements the ObjectPool interface. It adds attributes such as the object factory, the Map that stores all objects, the chained blocking queue that stores Idle objects, the number of objects currently created, and so on.
Because the GenericObjectPool class supports the paradigm, what we need to do is to specify the pool object type returned by the GenericObjectPool pool class and set the pool properties such as object factory class and configuration class, or inherit the GenericObjectPool class to add more custom pool features.
Inheritance relationship of pool property class
The uppermost interface for pool properties is interface Cloneable, which is implemented by the abstract class BaseObjectPoolConfig and defines the default pool configuration properties.
The GenericObjectPoolConfig class inherits from BaseObjectPoolConfig and also defines the default pool configuration property value.
We can either use the GenericObjectPoolConfig class directly or inherit the GenericObjectPoolConfig class to set custom pool configuration properties according to our own needs.
Inheritance relationship of object classes in pool
The object class in the pool implements the upper PooledObject interface, which defines various methods that a pool object needs to implement.
In addition, the object class in the pool also needs to define the member attributes and business methods that the class itself needs to have.
Inheritance relationship of object factory class
The object factory class implements the top-level PooledObjectFactory interface, which defines the core functional methods of the object factory, including: creating objects, destroying objects, verifying objects, activating objects, and passivating objects.
Work flow
According to the above analysis of the class inheritance relationship of the core components, we can sort out a process, gradually implement each component, and combine it into a set of connection pool architecture suitable for our business. Let's take a look at how this process is defined.
(1) define our pool object class ClientObject, and combine our actual business to implement the upper layer interface method.
(2) define the object factory class ClientFactory, and combine our actual business to implement the upper interface method.
(3) define the pool property class ClientPoolConfig, and set the attribute value according to our actual needs.
(4) use the object pool GenericObjectPool to specify the generic type GenericObjectPool.
Core business logic within the connection pool:
The creation and return logic of objects in the pool is the key in pooling technology. You can check the borrowObject method of the pool object to learn the details.
Application practice code implementation
According to the above analysis of the characteristics and implementation process of Apache-commons-pool2, we carry out code practice based on the application scenario of Grpc client connection pool, which mainly includes the implementation of object class ClientObject and object factory class ClientFactory.
The specific code can be downloaded from my Baidu network disk, the link is as follows:
Https://pan.baidu.com/s/1eaGpz6XN2a3ssw0eYsNLww
Code testing
In order to verify the role of our Grpc connection pool, I wrote a test method to simulate the following scenario, that is, 10 threads are started, each thread circulates 10 times to send messages to the grpc server using Grpc connections, and then check the cumulative number of connection objects created in the thread pool, the number of times each connection object is used in the thread pool, and so on.
By testing the output information, I have come to the conclusion that when connection pooling is not used, a total of 100 Grpc connections need to be made and messages are sent; after using connection pooling, only two Grpc connections are established to send messages, and each connection is called 50 times.
The test code is as follows.
Package com.cmcc.littlec.grpc.poolclient;import com.cmcc.littlec.grpc.util.Constants;import org.apache.commons.pool2.impl.GenericObjectPool;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;public class test {@ SuppressWarnings ("unchecked") public static GenericObjectPool getClientPool () {ClientPoolFactory factory = new ClientPoolFactory (Constants.grpcHost, Constants.grpcPort); GenericObjectPoolConfig config = new GenericObjectPoolConfig (); config.setMaxIdle (8); config.setMinIdle (3) Config.setMaxTotal (18); config.setTestOnBorrow (true); config.setTestOnReturn (true); GenericObjectPool clientPool = new GenericObjectPool (factory, config); return clientPool;} public static void main (String [] args) {final GenericObjectPool clientPool = getClientPool (); for (int iTuno; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.