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