MongoDB Series - Micro Integration Services

A Micro service address

Because before the authors have transferred prepared the development environment, client tools can be connected to normal, I deploy MongoDB service in the Baidu Cloud Linux server, so I create a new local micro-service module to establish a connection with the MongoDB service in two ways. The following is a micro-services gitee address that I created, which has the code.
MongoDB service integration and micro-code section, click! ! !

Here Insert Picture DescriptionI am here only synchronous interaction with MongoDB service connections, not related to specific operations.

Two must be the dependent JAR

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-context</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

III. Two connections

  1. Code to connect
    public void mongodbNoAutowired()
    {
        try {
            // 连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
            // ServerAddress()两个参数分别为 服务器地址 和 端口
            ServerAddress serverAddress = new ServerAddress("106.12.175.83",27017);
            List<ServerAddress> addrs = new ArrayList<ServerAddress>();
            addrs.add(serverAddress);

            // MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
            MongoCredential credential = MongoCredential.createScramSha1Credential("test", "test", "mongodb".toCharArray());
            List<MongoCredential> credentials = new ArrayList<MongoCredential>();
            credentials.add(credential);

            //通过连接认证获取MongoDB连接
            MongoClient mongoClient = new MongoClient(addrs, credentials);

            //连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            log.info("MongodbJdbcService|mongodbNoAutowired|Connect to database successfully");
            log.info("MongodbJdbcService|mongodbNoAutowired|连接到数据库:={}", mongoDatabase.getCollection("test"));
        } catch (Exception e) {
            log.error( "MongodbJdbcService|mongodbNoAutowired|数据库连接测试异常:={}", e.getClass().getName() + ": " + e.getMessage() );
        }
    }
  1. Notes mapping
    configuration file:
spring:
 data:
   mongodb:
     uri: mongodb://test:[email protected]:27017/test

Add comments to:

	private final MongoTemplate mongoClient;

    @Autowired
    public MongodbJdbcService(MongoTemplate mongoClient)
    {
        this.mongoClient = mongoClient;
    }

	public void mongodbAutowired()
    {
        MongoCollection<Document> people = this.mongoClient.getCollection("people");
        log.info("MongodbJdbcService|mongodbAutowired|集合大小:={}", people.count());
        if (Objects.isNull(people))
        {
            people = this.mongoClient.createCollection("people");
        }
        log.info("MongodbJdbcService|mongodbAutowired|使用注解连接MongoDB|创建集合请求出参:={}", people);
    }

This above two methods can be successfully connected to the MongoDB service, it is noted that:
the Spring-the Boot-Starter-web : package automatically help us to introduce the relevant jar package web module development needs.
Cloud-context-the Spring : The main is to build a Bootstrap container, and let it become a parent container springboot original program built container.

IV. Expand

We are interested can look at yourself, Spring automatic assembly mechanism.

SpringBoot depth (a) Preliminary automatic assembly mechanism
SpringBoot automated assembly principle

Published 215 original articles · won praise 135 · Views 1.14 million +

Guess you like

Origin blog.csdn.net/weinichendian/article/details/103920236