2016-11-22 31 views
0

我在MongoDB副本集中有2個服務器,一個是主要的,另一個是次要的。我的應用程序運行完全在主服務器上,但是當對次級遊,我得到以下警告:如何使用REPLICA_SET_SECONDARY類型對MongoDB服務器執行寫操作?

INFO: No server chosen by WritableServerSelector from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 11]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=691940, setName='rs2', canonicalAddress=192.168.71.22:27017, hosts=[192.168.70.197:27017, 192.168.71.22:27017, PC:27017], passives=[], arbiters=[], primary='PC:27017', tagSet=TagSet{[]}, electionId=null, setVersion=3}]}. Waiting for 30000 ms before timing out 

讀操作(選擇「2」的代碼)後30秒超時正確執行。寫入操作(代碼中的選擇「1」&「3」)觸發此警告再次出現並且不起作用。我應該在MongoDB設置中更改什麼或添加到代碼才能執行寫入操作?

代碼:

package ru.energodata; 

import com.mongodb.*; 
import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 
import com.mongodb.gridfs.GridFS; 
import com.mongodb.gridfs.GridFSInputFile; 


public class Main { 
public static void main(String[] args) { 
Scanner scanner; 
int selection; 
boolean exit = false; 
    MongoClient mongoClient = new MongoClient("localhost", 27017); 
    DB db = mongoClient.getDB("imagedb"); 
    GridFS gfsPhoto = new GridFS(db, "photo"); 

    while (!exit) 
    { 
     System.out.println("Choose action with the DB"); 
     System.out.println("1 - add files to DB"); 
     System.out.println("2 - list files"); 
     System.out.println("3 - remove files"); 
     scanner = new Scanner(System.in); 
     if(scanner.hasNextInt()) { 
      selection = scanner.nextInt(); 

      if (selection == 1) { 
       String path = "C:\\2015"; 
       String fileName; 
       File dir = new File(path); 
       File[] imageFile = dir.listFiles(); 
       try { 
        for (File anImageFile : imageFile) { 
         GridFSInputFile gfsFile = gfsPhoto.createFile(anImageFile); 
         fileName = anImageFile.getName(); 
         System.out.println("Current file: " + fileName); 
         // set a new filename for identify purpose 
         gfsFile.setFilename(fileName); 
         // save the image file into mongoDB 
         gfsFile.save(); 
        } 
       } catch (MongoException | IOException e) { 
        e.printStackTrace(); 
       } 
      } 

      if (selection == 2) { 
       // print the result 
       System.out.println("Database content:"); 
       DBCursor cursor = gfsPhoto.getFileList(); 
       if (cursor.hasNext()) { 
        while (cursor.hasNext()) { 
         System.out.println(cursor.next()); 
        } 
       } 

        else {System.out.println("none."); 
       } 
      } 
      if (selection == 3) { 
       // remove the image file from mongoDB 
       DBCursor cursor = gfsPhoto.getFileList(); 
       if (cursor.hasNext()) { 
       while (cursor.hasNext()) { 
        gfsPhoto.remove(cursor.next()); 
       } 
       System.out.println("DB content deleted"); 
       } 
       else {System.out.println("DB is empty"); 
       } 
      } 
     } 
    else { 
      System.out.println("Select either 1, 2 or 3"); 
      scanner = new Scanner(System.in); 
     } 
    } 
} 
} 
+1

這應該可以幫到你.. http://stackoverflow.com/questions/14587398/how-to-prefer-reads-on-secondaries-in-mongodb – user641887

回答

1

由我自己發現了。更改後的行:

MongoClient mongoClient = new MongoClient("localhost", 27017); 

MongoClientOptions options = MongoClientOptions.builder() 
      .readPreference(ReadPreference.nearest()) 
      .writeConcern(WriteConcern.W2) 
      .build(); 
    MongoClient mongoClient = new MongoClient(Arrays.asList(
      new ServerAddress(serverList[0]), 
      new ServerAddress(serverList[1]), 
      new ServerAddress(serverList[2])),options); 

凡SERVERLIST []字符串數組包含副本集的所有服務器的地址。現在應用程序找到主服務器並連接到它。

INFO: Discovered replica set primary pc:27017 
INFO: Opened connection [connectionId{localValue:5, serverValue:79}] to pc:27017 

因此,寫入操作變得可用,因爲您只能寫入集中的主服務器。

相關問題