Basic tutorial


1. Use examples

a. Add dependent Jar

<dependency>
    <groupId>org.xson</groupId>
    <artifactId>tangyuan-mongo</artifactId>
    <version>1.2.0</version>
</dependency>

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.3.0</version>
</dependency>

b. Add a service component

Add the mongo component to the tangyuan configuration file tangyuan.xml

<?xml version="1.0" encoding="UTF-8"?>
<tangyuan-component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://xson.org/schema/tangyuan/component.xsd">

    <!--Add the mango service component -->
    <component resource="component-mongo.xml" type="mongo" />

</tangyuan-component>

c. Configure the component

Configure the tangyuan-mongo component in the component-mongo.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<mongo-component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://xson.org/schema/tangyuan/mongo/component.xsd">

    <!-- Mongo dataSource -->
    <dataSource id="mongods">
       <property name="url" value="mongodb://127.0.0.1:27017/mdb" />
    </dataSource>

    <!-- Mongo service plugin -->
    <plugin resource="test-mongo.xml"/>

</mongo-component>

d. Define the Mongo service

Define the Mongo service in the service plug-in test-mongo.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mongoservices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://xson.org/schema/tangyuan/mongo/service.xsd" ns="demo">

    <!-- Mongo query service -->
     <selectOne id="getUser" dsKey="mongods">
       SELECT * from user_info where id = 1
     </selectOne>

</mongoservices>

e. Unit test

@Test
public void testMongo() {
    XCO request = new XCO();
    // set
    Object obj = ServiceActuator.execute("demo/getUser", request);
    System.out.println(obj);
}

f. Return the result

The current service getUser returns the result is an XCO object, the XML format is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<X>
    <S K="_id" V="59950f7f2932a11a68285d21"/>
    <I K="id" V="1"/>
    <S K="name" V="xson.org"/>
    <I K="age" V="26"/>
    <I K="amount" V="1000"/>
</X>

Through the previous steps, we achieved a complete process. From the configuration of the component, the definition of the service, the call to the service. Note: All Mongo services need to be defined in the service plug-in file, such as the test-mongo.xml file in the example, which is no longer specified in the following example.

2. Data source configuration

Tangyuan-mongo components in the data source configuration is divided into two kinds, one is an ordinary data source, suitable for ordinary Mongo applications, the other is the data source group, suitable for data volume and data concurrent access large application scenarios, and need to cooperate with the sharding module to use. The configuration of the data source is in component-mongo.xml.

2.1 Ordinary data source

Configuration example

<dataSource id="mongods">
    <property name="url" value="mongodb://127.0.0.1:27017/mdb" />
</dataSource>

dataSource node description

Attribute name use and description Required value
id The unique identifier for this data source is not repeatable Y String,But does not allow “.”
isDefault Whether it is the default data source. If you configure multiple data sources in your system, only one is the default N true/false

property node description

Attribute name Use and description Required Value
name Attribute name Y String
value Attribute value Y String

property node attribute name and attribute value description

Attribute name Use and description Value default value
url Mongo URL protocol String
username user name String
password password String
socketKeepAlive Sets whether socket keep alive is enabled. boolean true
maxWaitTime Sets the maximum time that a thread will block waiting for a connection. int 10 * 60 * 1000
connectTimeout Sets the connection timeout. int 60 * 1000
socketTimeout the socket timeout, in milliseconds int 60 * 1000
connectionsPerHost maximum number of connections int 30
minConnectionsPerHost minimum number of connections int
maxConnectionIdleTime the maximum idle time, in milliseconds, which must be > 0 int
maxConnectionLifeTime the maximum life time, in milliseconds, which must be > 0 int
minHeartbeatFrequency the minimum heartbeat frequency, in milliseconds, which must be > 0 int
serverSelectionTimeout the server selection timeout, in milliseconds int
sslInvalidHostNameAllowed whether invalid host names are allowed in SSL certificates. boolean
sslEnabled set to true if using SSL boolean
requiredReplicaSetName Sets the required replica set name for the cluster. String
writeConcern Sets the write concern. String
readConcern Sets the read concern. String

2.2 Data source group

Configuration example

<dataSourceGroup groupId="dsGourp" start="0" end="9">
    <property name="url" value="mongodb://127.0.0.1:27017/mdb{}" />
</dataSourceGroup>

Description

The essence of the data source group is based on the user’s set of start and end indexes, creating multiple data sources. The above code represents the creation of 100 data sources.

mongodb://127.0.0.1:27017/mdb0
mongodb://127.0.0.1:27017/mdb1
...
mongodb://127.0.0.1:27017/mdb9

dataSourceGroup node description

Attribute name Use and description Required Value
id The unique identifier for this data source is not repeatable Y String,But does not allow “.”
isDefault Whether it is the default data source. If you configure multiple data sources in your system, only one is the default N true/false
start Start index, defaults to 0 N int
end End index Y int

property node description

In addition to the url attribute can use placeholders, the other please refer to dataSource->property node.

3. Plugin configuration

Tangyuan-mongo components can be Through the plug-in to define the service, management and functional expansion. According to the use can be divided into three categories:

  1. Result maping plugin
  2. Sharding plugin
  3. Mongo service plugin

The result mapping plugin is responsible for returning the configuration of the result mapping, sharding plug-in, as the name suggests is sharding configuration, the service plugin is used to define the specific Mongo service. The result mapping plugin and sharding plugin can only have one at most, the service plug-in can be more than one. The plug-in configuration is located in the component configuration file component-mongo.xml.

Configuration example

<!-- Mongo result maping plugin -->
<mapper     resource="mapper-mongo.xml" />
<!-- Mongo sharding plugin -->
<sharding   resource="sharding-mongo.xml" />
<!-- Mongo service plugin -->
<plugin     resource="test-mongo.xml"/>

mapper, sharding, plugin node description

Attribute name Use and description Required Value
resource The plugin’s resource file path Y String