August 23 Configuring MongoDB connection Pooling | Docker operations

Source: Internet
Author: User
Tags connection pooling mongoclient mongodb

First, configure the MongoDB connection pool

Attribute Class

Importorg.springframework.boot.context.properties.ConfigurationProperties;ImportOrg.springframework.context.annotation.PropertySource;Importorg.springframework.stereotype.Component;ImportLombok. Getter;ImportLombok. Setter; @Component @propertysource (Value= "Classpath:application-dev.properties") @ConfigurationProperties (prefix= "MONGO") @Getter @setter Public classmongosettingsproperties {PrivateString host; Private intPort; PrivateString database; PrivateInteger minconnectionsperhost = 0; PrivateInteger maxconnectionsperhost = 100; PrivateInteger Maxwaittime = 120000; PrivateInteger ConnectTimeout = 10000; PrivateString username; PrivateString password; PrivateString authenticationdatabase;}

Configuration file

mongo.host=42.159. Xx.xxmongo.port=27018mongo.database=loreal-mdmmongo.username=  Mongo.password=mongo.options.min-connections-per-host=20Mongo.options.max- Connections-per-host=20mongo.options.max-wait-time=120000mongo.options.connect-timeout= 10000mongo.authentication-database:admin

Connection Pool Bean Configuration

Importjava.util.ArrayList;Importjava.util.List;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.data.mongodb.MongoDbFactory;Importorg.springframework.data.mongodb.core.SimpleMongoDbFactory;Importcom.mongodb.MongoClient;Importcom.mongodb.MongoClientOptions;Importcom.mongodb.MongoCredential;Importcom.mongodb.ServerAddress;Importcn.com.connext.commenservice.bean.MongoSettingsProperties; @Configuration Public classMongoconfig {//overriding the default Mongodbfacotry@Bean @Autowired Publicmongodbfactory mongodbfactory (mongosettingsproperties properties) {//Client Configuration (number of connections, replica cluster authentication)Mongoclientoptions.builder Builder =NewMongoclientoptions.builder ();        Builder.connectionsperhost (Properties.getmaxconnectionsperhost ());        Builder.minconnectionsperhost (Properties.getminconnectionsperhost ());        Builder.maxwaittime (Properties.getmaxwaittime ());        Builder.connecttimeout (Properties.getconnecttimeout ()); Mongoclientoptions mongoclientoptions=Builder.build (); List<ServerAddress> serveraddresses =NewArraylist<>(); //MongoDB Address ListServerAddress serveraddress =Newserveraddress (Properties.gethost (), Properties.getport ());        Serveraddresses.add (serveraddress); //Connection AuthenticationList<mongocredential> mongocredentiallist =NewArraylist<>(); Mongocredential mongocredential=mongocredential.createscramsha1credential (Properties.getusername (), Properties.getauthenticationdataba SE ()!=NULL?properties.getauthenticationdatabase (): Properties.getdatabase (), Properties.getpassword (). ToCharArra        Y ());        Mongocredentiallist.add (mongocredential); //creating clients and factoryMongoclient mongoclient =Newmongoclient (serveraddresses,mongoclientoptions); //New Mongoclient (serveraddresses, mongocredential, mongoclientoptions);Mongodbfactory mongodbfactory =Newsimplemongodbfactory (Mongoclient, Properties.getdatabase ()); returnmongodbfactory; }}

Reference:

80585004

Http://www.xiaoqiangge.com/aritcle/1522033666922.html

Second, Docker operation

Locate the MongoDB configuration file and create the username password

-it < container name >/usr/bin/mongo[email protected]:/var/local# cd/usr/bin into MONGO. /mongo 127.0.0.1:27017db.createuser ({User:"LOREALMDM", pwd: "[email protected]", Roles:[{role: " Readwriteanydatabase ", DB:" admin "

Ctrl+d Exit Docker Interactive

You cannot use Vim to edit files in Docker

Update source

apt-get update

Install Vim

apt-get install -y vim

Reference:

52441807

78983741

About roles:

53889990

August 23 Configuring MongoDB connection Pooling | Docker operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.