Skip to content

Commit

Permalink
更改mongo连接配置参数为可集群
Browse files Browse the repository at this point in the history
  • Loading branch information
LeiGaoRobot committed Nov 4, 2021
1 parent c85caab commit 10c3e90
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 19 deletions.
3 changes: 1 addition & 2 deletions buyer-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ spring:
# mongodb
data:
mongodb:
host: 127.0.0.1
port: 27017
uri: 127.0.0.1:27017
database: lilishop
username: root
password: lilishop
Expand Down
3 changes: 1 addition & 2 deletions common-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ spring:
# mongodb
data:
mongodb:
host: 127.0.0.1
port: 27017
uri: 127.0.0.1:27017
database: lilishop
username: root
password: lilishop
Expand Down
3 changes: 1 addition & 2 deletions config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ spring:
# mongodb
data:
mongodb:
host: 192.168.0.116
port: 27017
uri: 192.168.0.116:27017
database: lilishop
username: root
password: lilishop
Expand Down
3 changes: 1 addition & 2 deletions consumer/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ spring:
# mongodb
data:
mongodb:
host: 127.0.0.1
port: 27017
uri: 127.0.0.1:27017
database: lilishop
username: root
password: lilishop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.util.Collections;
import java.util.ArrayList;
import java.util.List;

/**
* @author paulG
Expand All @@ -21,11 +22,8 @@ public class MongoConfig extends AbstractMongoClientConfiguration {
@Value("${spring.data.mongodb.database}")
private String databaseName;

@Value("${spring.data.mongodb.host}")
private String host;

@Value("${spring.data.mongodb.port}")
private Integer port;
@Value("${spring.data.mongodb.uri}")
private List<String> uri = new ArrayList<>();

@Value("${spring.data.mongodb.username}")
private String username;
Expand All @@ -44,7 +42,14 @@ protected String getDatabaseName() {
@Override
protected void configureClientSettings(MongoClientSettings.Builder builder) {
builder.credential(MongoCredential.createCredential(username, authenticationDatabase, password.toCharArray()))
.applyToClusterSettings(settings -> settings.hosts(Collections.singletonList(new ServerAddress(host, port))));
.applyToClusterSettings(settings -> {
List<ServerAddress> serverAddresses = new ArrayList<>();
for (String s : uri) {
String[] node = s.split(":");
serverAddresses.add(new ServerAddress(node[0], Integer.parseInt(node[1])));
}
settings.hosts(serverAddresses);
});
}

}
3 changes: 1 addition & 2 deletions manager-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ spring:
# mongodb
data:
mongodb:
host: 127.0.0.1
port: 27017
uri: 127.0.0.1:27017
database: lilishop
username: root
password: lilishop
Expand Down
3 changes: 1 addition & 2 deletions seller-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ spring:
# mongodb
data:
mongodb:
host: 127.0.0.1
port: 27017
uri: 127.0.0.1:27017
database: lilishop
username: root
password: lilishop
Expand Down

0 comments on commit 10c3e90

Please sign in to comment.