https://github.com/spring-projects/spring-boot/blob/5490e73922b37a7f0bdde43eb318cb1038b45d60/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesClientSettingsBuilderCustomizer.java#L91

如代码所示,不对代码做StringUtils.isBlank处理,会导致ClusterSettings的ClusterType=REPLICA_SET,这可能会产生负面影响。

Comment From: philwebb

Courtesy of Google Translate:

As shown in the code, not doing StringUtils.isBlank processing in the code will result in ClusterType=REPLICA_SET of ClusterSettings, which may have negative effects.

Comment From: philwebb

@miaoGod I'm not sure we should make the change you're suggesting. We follow the same logic as the MongoDB driver (see https://github.com/mongodb/mongo-java-driver/blob/634fd094992f09cf39deec0ea2afeb12b4db61ef/driver-core/src/main/com/mongodb/connection/ClusterSettings.java#L621) which doesn't check for empty string.

What triggered you to raise this issue? Have you actually tried to use a blank string in your own application?

Comment From: ooooym

@philwebb 是的,我确实遇到了这种场景,详情如下: 由于预算有限,在测试环境的mongodb配置仅有一台实例,而生产的配置为两台实例(一主一从),而在使用主从复制配置时,我使用的方式如下代码:

spring:
  data:
    mongodb:
      replica-set-name: ${mongo.replicaSetName}

其中的mongo.replicaSetName引用的是application-{env}.properties中的配置

按照以上配置启动项目以后,我发现mongo.replicaSetName无论是否有值,ClusterSettings的ClusterType都会为REPLICA_SET, 但是如果我将replica-set-name配置项直接去掉而不是让他等于一个空字符串,ClusterSettings的ClusterType则为UNKNOWN

Comment From: philwebb

Translated:

Yes, I did encounter this scenario, the details are as follows:

Due to limited budget, the mongodb configuration in the test environment only has one instance, while the production configuration has two instances (one master and one slave). When using the master-slave replication configuration, I use the following code:

spring:
  data:
    mongodb:
      replica-set-name: ${mongo.replicaSetName}

The mongo.replicaSetName refers to the configuration in application-{env}.properties

After starting the project according to the above configuration, I found that regardless of whether mongo.replicaSetName has a value or not, the ClusterType of ClusterSettings will be REPLICA_SET.

But if I remove the replica-set-name configuration item directly instead of making it equal to an empty string, the ClusterType of ClusterSettings is UNKNOWN.

Comment From: philwebb

That seems like a valid use-case. We'll fix this.