【问题标题】:HikariCP for MongoDB in springboot春季启动中用于 MongoDB 的 HikariCP
【发布时间】:2018-12-04 13:41:37
【问题描述】:

我希望在 Springboot 中为 mongoDB 创建一个连接池。我目前正在使用 Springdata Mongo 存储库来连接数据库和集合,但不确定如何创建数据池连接

这是当前的实现

PersonRepository.java

package com.test.TestAPI.repository;

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.test.TestAPI.dto.Person;

@Repository
public interface PersonRepository extends MongoRepository<Person, String> {


}

人员服务

package com.test.TestAPI.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.test.TestAPI.repository.PersonRepository;
import com.test.TestAPI.dto.Person;

@Service
public class PersonService {
    @Autowired
    private PersonRepository personRepo;

    public List<Person> findAllPersons() {
        return personRepo.findAll();
    }

    public Person createPerson(Person person) {
        return personRepo.save(person);
    }

}

PersonController

package com.test.TestAPI.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.test.TestAPI.service.impl.PersonService;
import com.test.TestAPI.dto.Person;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(path="/v1/personController")
@Api(value="Controller for Person document")
public class PersonController {

    @Autowired
    PersonService service;

    @GetMapping("/getAllPersons")
    @ApiOperation(produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET", response = List.class, 
            value = "getAllPersons from the database", notes = "Sample note")
    public ResponseEntity<List<Person>> getAllPersons(){
        List<Person> personList = service.findAllPersons();
        return new ResponseEntity<List<Person>>(personList, HttpStatus.OK);
    }
}

SimpleCommandLineConfig

package com.test.TestAPI.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.stereotype.Component;

import com.test.TestAPI.repository.PersonRepository;
import com.test.TestAPI.dto.Person;


@Component
@Order(3)
public class SimpleCommandLineConfig implements CommandLineRunner {

    @Autowired
    PersonRepository repo;

    @Override
    public void run(String... args) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("third command line runner");
        System.out.println(repo.save(new Person("rr","rr",4)));
    }

}

App.java

package com.test.TestAPI.main;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@ComponentScan(basePackages= {"com.test.TestAPI"})
@EnableMongoRepositories(basePackages= {"com.test.TestAPI.repository"})
public class App 
{
     public static void main(String[] args) throws Exception {
        SpringApplication.run(App.class, args);
    }

}

另外,我想知道像 MongoDB 的自定义 repo 这样的 spring 数据 repos 是否负责连接池机制?在这种情况下,连接池是如何发生的?你能帮我解决这个问题吗

【问题讨论】:

    标签: mongodb spring-boot hikaricp


    【解决方案1】:

    我想我找到了答案。就像我们使用 RDBMS 的 JDBC 模板来存储数据库一样,spring 提供了一个叫做 MongoTemplates 的东西,它根据给定的 db 配置形成一个连接池

    这是一个示例实现

    MongoClientFactory.java

     public @Bean MongoClientFactoryBean mongo() throws Exception {
                  MongoClientFactoryBean mongo = new MongoClientFactoryBean();
                  mongo.setHost("localhost");
                  MongoClientOptions clientOptions = MongoClientOptions.builder().applicationName("FeddBackAPI_DB")
                          .connectionsPerHost(2000)
                          .connectTimeout(4000)
                          //.maxConnectionIdleTime(1000000000)
                          .maxWaitTime(3000)
                          .retryWrites(true)
                          .socketTimeout(4000)
                          .sslInvalidHostNameAllowed(true)//this is very risky
    
                          .build();
                  mongo.setMongoClientOptions(clientOptions);
    
    
                  return mongo;
             }
    

    DataSourceConfig.java(另一个配置类。同样的配置类也可以用来定义所有的bean)

    package com.fmr.FeedBackAPI.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    import org.springframework.core.env.Environment;
    import org.springframework.data.mongodb.MongoDbFactory;
    import org.springframework.data.mongodb.core.MongoOperations;
    import org.springframework.data.mongodb.core.MongoTemplate;
    import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
    
    import com.mongodb.Mongo;
    import com.mongodb.MongoClient;
    
    
    @Configuration
    @Import(value=MongoClientFactory.class)
    public class DataSourceConfig {
    
        @Autowired
        Mongo mongo;
    
        @Autowired
        Environment env;
    
        @Bean
        public String test() {
            System.out.println("mongo"+mongo);
            return "rer";
        }
    
    
        private MongoTemplate mongoTemplate() {
    
    
            MongoDbFactory factory = new SimpleMongoDbFactory((MongoClient) mongo, "mongo_test");
            MongoTemplate template = new MongoTemplate(factory);
    
            return template;
        }
    
        @Bean
        @Qualifier(value="customMongoOps")
        public MongoOperations mongoOps() {
            MongoOperations ops = mongoTemplate();
            return ops;
        }
    
    
        @Bean
        public MongoDbFactory factory() {
            MongoDbFactory factory = new SimpleMongoDbFactory((MongoClient) mongo, "mongo_test");
            return factory;
        }
    
    //  @Bean
    //  public GridFsTemplate gridFsTemplate() {
    //      return new GridFsTemplate(mongo, converter)
    //  }
    
        @Bean
        public javax.sql.DataSource dataSource() {
            HikariDataSource ds = new HikariDataSource();
            ds.setMaximumPoolSize(100);
          //  ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
           // ds.setJdbcUrl(env.getProperty("spring.datasource.url"));;
            //ds.setUsername(env.getProperty("spring.datasource.username"));
            //ds.setPassword(env.getProperty("spring.datasource.password"));
            ds.addDataSourceProperty("cachePrepStmts", true);
            ds.addDataSourceProperty("prepStmtCacheSize", 250);
            ds.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
            ds.addDataSourceProperty("useServerPrepStmts", true);
            return ds;
        }
    }
    

    现在在你的 dao 实现或服务中自动装配模板/mongoOperations 并使用它

    package com.fmr.FeedBackAPI.service.impl;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.data.mongodb.core.MongoOperations;
    import org.springframework.data.mongodb.core.MongoTemplate;
    import org.springframework.stereotype.Service;
    
    import com.fmr.FeedBackAPI.dto.ConfigDTO;
    
    import static org.springframework.data.mongodb.core.query.Criteria.where;
    import static org.springframework.data.mongodb.core.query.Query.query;
    
    
    @Service
    public class PlanConfigService {
    
        @Autowired
        @Qualifier(value="customMongoOps")
        MongoOperations mongoOps;
    
        public List<ConfigDTO> createConfigDTOList(List<ConfigDTO> configDTOList) {
            List<ConfigDTO> configList = new ArrayList<>();
            if(!mongoOps.collectionExists(ConfigDTO.class)) {
                mongoOps.createCollection("ConfigDTO_table");
    
            }
            //create the configDTOList
            mongoOps.insert(configDTOList, ConfigDTO.class);
            configList = mongoOps.findAll(ConfigDTO.class, "ConfigDTO_table");
            return configList;
        }
    
        public List<ConfigDTO> createConfigDTO(ConfigDTO configDTO) {
            List<ConfigDTO> configList = new ArrayList<>();
            if(!mongoOps.collectionExists(ConfigDTO.class)) {
    
                mongoOps.createCollection("ConfigDTO_table");
    
            }
            //create the configDTOList
             mongoOps.save(configDTO);
    
            configList = mongoOps.find(query(where("planId").is(configDTO.getPlanId())), ConfigDTO.class,"ConfigDTO_table");
            return configList;
        }
    }
    

    这是 application.properties(这是在本地运行的默认实例)

    spring.data.mongodb.host=localhost
    spring.data.mongodb.port=27017
    spring.data.mongodb.database=mongo_test
    spring.data.mongodb.repositories=true
    #
    spring.datasource.url=jdbc:mongodb://localhost:27017/mongo_test
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多