通用技术 [接口测试平台二期] 多数据源连接

bauul · 2017年12月29日 · 最后由 CC 回复于 2017年12月29日 · 2190 次阅读

缘由

因项目测试需要连接,hive,mysql,mongo 三个数据库

项目依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.carl</groupId>
    <artifactId>test-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mobile-test-service</name>
    <description>Mobile Test Service for Jolly</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <hadoop.version>2.3.2</hadoop.version>
        <java.version>1.8</java.version>
        <fastjson.version>1.2.40</fastjson.version>
        <druid.version>1.1.5</druid.version>

        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator-docs</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jooq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>${hadoop.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.eclipse.jetty.aggregate</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.eclipse.jetty.orbit</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.cloudera</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>2.5</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/HiveJDBC4.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hive-service-cli</artifactId>
            <version>1.7.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/TCLIServiceClient.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hive-ql</artifactId>
            <version>1.7.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/ql.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>materializecss</artifactId>
            <version>0.100.2</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.2.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>${project.basedir}/lib</directory>
                <targetPath>BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

由于使用了本地 lib 目录下的依赖,所以增加了 resources 节点,可以将 lib 目录打进 jar 中去

多数据源配置

### Database for mongo ###
spring.data.mongodb.database=countly
spring.data.mongodb.host=192.168.2.3
spring.data.mongodb.port=27017

### Database for hive ###
spring.datasource.hive.url=jdbc:hive2://192.168.2.3:10100/hivedb
spring.datasource.hive.username=apptest
spring.datasource.hive.password=123456
spring.datasource.hive.driverClassName=com.cloudera.hive.jdbc4.HS2Driver

### Database for mysql(beta) ###
spring.datasource.mysql.url=jdbc:mysql://192.168.2.3:3306/testservice?useUnicode=true&characterEncoding=UTF-8
spring.datasource.mysql.username=test
spring.datasource.mysql.password=test
spring.datasource.mysql.driverClassName=com.mysql.jdbc.Driver

连接配置

@Configuration
@EnableMongoRepositories(basePackages={"com.carl.testservice.mapper.mongo"})//MongoRepository的扫描包
@MapperScan("com.carl.testservice.mapper.mongo")
public class MongoConnectConfig extends AbstractMongoConfiguration{

    @Autowired
    private Environment env;

    @Override
    protected String getDatabaseName() {
        return env.getRequiredProperty("spring.data.mongodb.database");
    }

    @Override
    @Bean
    public Mongo mongo() throws Exception {
        ServerAddress serverAddress = new ServerAddress(env.getRequiredProperty("spring.data.mongodb.host"));
        List<MongoCredential> credentials = new ArrayList<>();
        return new MongoClient(serverAddress, credentials);
    }
}
@Configuration
@MapperScan(basePackages = {"com.carl.testservice.mapper.hive"}, sqlSessionTemplateRef = "sqlSessionTemplateHive")
public class HiveConnectConfig {

    @Bean(name = "hiveDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hive")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "hiveSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("hiveDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "hiveTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("hiveDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionTemplateHive")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("hiveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
@Configuration
@MapperScan(basePackages = {"com.carl.testservice.mapper.mysql"}, sqlSessionTemplateRef = "sqlSessionTemplateMysql")
public class MysqlConnectConfig {

    @Bean(name = "mysqlDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.mysql")
    @Primary
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "mysqlSqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "mysqlTransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionTemplateMysql")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

mapper

public interface EventMapperMongo extends MongoRepository<EventJSON, String> {

    /**
     * 通过DeviceId查找到对应的事件
     * @param deviceId
     * @param startTime
     * @param endTime
     * @return
     */
    @Query("{'d.id':?#{[0]}, 't':'event', 'i.timestamp':{$gt:?#{[1]}, $lt:?#{[2]}}}")
    public List<EventJSON> findAllByQuery(String deviceId, long startTime, long endTime);
}

cotroller

@RestController
@RequestMapping("/mongoEvent")
@Slf4j
public class EventControllerMongo {

    @Autowired
    private EventMapperMongo eventMapperMongo;

    @RequestMapping(method = RequestMethod.GET, value = "/deviceId/{deviceId}/startTime/{startTime}/endTime/{endTime}")
    public List<EventJSON> getEvents(@PathVariable("deviceId") String deviceId, @PathVariable("startTime") long startTime, @PathVariable("endTime") long endTime) {
        return eventMapperMongo.findAllByQuery(deviceId, startTime, endTime);
    }

}

bean

@Data
@Document(collection = "logs5729aa86ff5812096852d481")
public class EventJSON {

    @Id
    private String id;
    private String ts;
    private String reqts;
    private String d;
    private String l;
    private String v;
    private String t;
    private String i;
    private String s;
    private String c;

    public EventJSON() {
    }

}

其他两个类型无特殊情况,mongo 特殊一点,给出示例

日志输出 sql 的配置

logging.level.com.carl.testservice.mapper=DEBUG
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 1 条回复 时间 点赞

哈哈,咋一看我还以为是连接多个不同 mybatis

—— 来自 TesterHome 官方 安卓客户端

bauul 测试平台从搭建到放弃 (二) Mock 篇 中提及了此贴 08月04日 09:38
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册