今日の開発には、Mongodb接続に関連する可能性のあるパフォーマンスの問題が発生したため、私はよく見て、誰かがこの関連コンテンツを書いていることがわかりました。時間がある場合は、springforallに拡張機能を記述できますが、主題のアイデアはまだ同じです。この美しい少女、ジョイ・ユーアンの記事に感謝します!
説明します
Spring Bootは、Spring-Boot-Starter-Data-Mongodbを使用して、Spring-Data-Mongodbの自動構成を実装しています。
ただし、デフォルトでは、Spring BootはMySQLやRedisの使用などの接続プール構成関数を提供しません。したがって、MongoDBFactoryを自分で書き換えて、MongoDBクライアント接続のパラメーター構成拡張を実装する必要があります。
MongoDBクライアント自体が接続プールであるため、クライアントを構成するだけでいいことに注意する必要があります。
構成ファイル
Spring Bootの構成を統合するには、spring.data.mongodb.custom(プレフィックスは自由に構成できます)として、接頭辞をapplication.ymlに書き換えた構成をapplication.ymlに構成する必要があります。
春:データ:MongoDB:カスタム:ホスト:-10.0.5.1-10.0.5.1ポート:-27017-27018レプリカセット:MGSET-3590061ユーザー名:JANCEEパスワード:ABC123データベース:JANCEE認証-DATABASE:管理接続の管理 - host:20 minOnnections-per-host:20 per-host:
この構成例では、ホスト10.0.5.1:27017および10.0.5.1:27018を含むレプリカセットが構成されています。他の構成は、Spring Bootの標準構成に似ています。さらに、ホストあたりの接続はクライアントへの接続の数であり、ホストあたりの接続内では、クライアントへの接続の最小数です。
クラスに包まれています
通話と読みやすさの利便性のために、上記の構成は構成エンティティクラスに巻き付けられます。 mongoconfig.javaコードは次のとおりです。
パッケージcom.feidiao.jancee.fdiot.api.config.mongo; Import org.hibernate.validator.constraints.notblank; Import org.hibernate.validator.constraints.notempty; Import org.springframework.stereoty.component java.util.list;@component@validatedpublic class mongosettingsproperties {@notblank private文字列データベース; @notemptyプライベートリスト<String>ホスト; @notemptyプライベートリスト<Integer>ポート;プライベートストリングレプリカセット;プライベート文字列ユーザー名;プライベート文字列パスワード。プライベート文字列AuthenticationDatabase; private integer minconnectionsperhost = 10;プライベート整数ConnectionSperHost = 2; public MongosettingsProperties(){} public String getDataBase(){return Database; } public void setDataBase(String Database){this.DataBase = Database; } public List <String> gethosts(){return hosts; } public void sethosts(list <string> hosts){this.hosts = hosts; } public list <integer> getPorts(){return ports; } public void setports(list <integer> ports){this.ports = ports; } public string getReplicaset(){Replicaset; } public void setReplicaset(string replicaset){this.replicaset = replicaset; } public string getUsername(){return username; } public void setUsername(string username){this.username = username; } public string getPassWord(){パスワードを返します。 } public void setPassword(string password){this.password = password; } public string getAuthenticationDataBase(){return euthenticationDataBase; } public void setAuthenticationDatabase(String AuthenticationDatabase){this.authenticationDatabase = authenticationDatabase; } public Integer getMinconnectionSperhost(){return minconnectionsperhost; } public void setMinconnectionSperhost(integer minconnectionsperhost){this.minconnectionsperhost = minconnectionsperhost; } public Integer getConnectionSperhost(){return ConnectionSperhost; } public void setConnectionSperhost(integer connectionsperhost){this.connectionsperhost = connectionsperhost; }}MongodbFactoryを上書きします
次に、Spring Bootの元のMongodbFactory Beanを上書きし、次のようなコードを使用して、新しいファイルMongoconfig.javaを作成します。
com.mongodb.mongoclient;インポートcom.mongodb.mongoclientoptions; Import com.mongodb.mongocredention; import com.mongodb.serveraddress; Import org.springframework.beans.factory.annotation.autowired; Import; springframework.boot.context.properties.configurationproperties; Import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; Import org.springframework.data.mongodb.mongodbfictory; org.springframework.mongodb.core.simplemongodbfactory; import java.util.arraylist; import java.util.list; @configurationpublic class mongoconfig {// Inject Configuration Entity @autowired MongosettingsProperties MongosettisProperties; @bean @configurationproperties(prefix = "spring.data.mongodb.custom")mongosettingsproperties mongosettingsproperties(){return new MongosettingsProperties(); } //デフォルトのmongodbfactory @bean mongodbfactory mongodbfactory(){//クライアント構成(接続数、レプリカクラスター検証)mongoclientoptions.builder builder = new mongoclientoptions.builder(); Builder.ConnectionSperhost(MongoSettingSproperties.getConnectionSperhost()); builder.minconnectionsperhost(mongosettingsproperties.getminconnectionsperhost()); if(mongosettingsproperties.getReplicaset()!= null){builder.RequiredReplicasetName(mongosettingsproperties.getReplicaset()); } mongoclientoptions mongoclientoptions = builder.build(); // mongodbアドレスリスト<serverAddress> serverAddresses = new ArrayList <>(); for(string host:mongosettingsproperties.gethosts()){integer index = mongosettingsproperties.gethosts()。indexof(host); integer port = mongosettingsproperties.getports()。get(index); serverAddress serverAddress = new ServerAddress(host、port); serverAddresses.add(serverAddress); } system.out.println( "serverAddresses:" + serverAddresses.toString()); //接続認証リスト<Mongocredential> mongocredentiallist = new ArrayList <>(); if(mongosettingsproperties.getUsername()!= null){mongocredentiallist.add(mongocredentional.createscramsha1credential(mongosettingsporties.getusername()、mongosettingsproperties.getauthenticationdatabase()!= null? mongosettingsproperties.getauthenticationDatabase():mongosettingsproperties.getDatabase()、mongosettingsproperties.getPassWord()。toCharArray())); } system.out.println( "mongocredentiallist:" + mongocredentiallist.toString()); //クライアントと工場のmongoclient mongoclient = new Mongoclient(serverAddresses、mongocredentiallist、mongoclientoptions); mongodbfactory mongodbfactory = new simplemongodbfactory(mongoclient、mongosettingsproperties.getDatabase()); MongodbFactoryを返します。 }}ここで、MongoDB接続が実装されると、前の構成で構成されたパラメーターが設定されます。実際の状況に応じて、新しいsimplemongodbfactoryに必要な構成パラメーターを追加および変更できます。
この時点で、すべての構成が完了し、テストが実行されました。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。