Review
In the previous section, we have installed and configured our servers for MongoDB replication. In this section, we will update our Spring application to support MongoDB Replica Sets.Table of Contents
Part 1: IntroductionPart 2: Installation and configuration of Replica Sets
Part 3: Spring app configuration
Part 4: Testing the servers
Spring App Configuration
We have setup a MongoDB Replica Set using four servers, and we've examined the output from the logs and verified that all instances are running. Our next step is to configure our Spring MongoDB-base application.Since we're reusing our application from the tutorial Spring MVC 3.1 - Implement CRUD with Spring Data MongoDB, adding a MongoDB Replica Set support in Spring is trivial. All we need to do is modify the spring.properties file with the following contents (this file is located under the WEB-INF directory):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# database properties | |
mongo.db.name=spring_mongodb_tutorial | |
mongo.host.name=localhost | |
mongo.host.port=27017 | |
mongo.replica.set=123.456.78.90:27017,123.456.78.91:27017,123.456.78.92:27017,123.456.78.93:27017 |
Then we have to modify as well the spring-data.xml file with the following updates:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:p="http://www.springframework.org/schema/p" | |
xmlns:c="http://www.springframework.org/schema/c" | |
xmlns:tx="http://www.springframework.org/schema/tx" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:mongo="http://www.springframework.org/schema/data/mongo" | |
xmlns:util="http://www.springframework.org/schema/util" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd | |
http://www.springframework.org/schema/tx | |
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-3.1.xsd | |
http://www.springframework.org/schema/data/mongo | |
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd | |
http://www.springframework.org/schema/util | |
http://www.springframework.org/schema/util/spring-util-3.1.xsd"> | |
<context:property-placeholder properties-ref="deployProperties" /> | |
<!-- Activate Spring Data MongoDB repository support --> | |
<mongo:repositories base-package="org.krams.repository" /> | |
<!-- Add Replica Set support --> | |
<mongo:mongo replica-set="${mongo.replica.set}"/> | |
<!-- Template for performing MongoDB operations --> | |
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" | |
c:mongo-ref="mongo" c:databaseName="${mongo.db.name}"/> | |
<!-- Service for initializing MongoDB with sample data using MongoTemplate --> | |
<bean id="initMongoService" class="org.krams.service.InitMongoService" init-method="init"/> | |
</beans> |
That's all we need to for our Spring application!
0 komentar:
Post a Comment