spring boot 集成螞蟻sofa zdal

說明:java

       本文主要用於記錄當時本身項目中集成的步驟,zdal 使用單庫單表,多庫多表等後續更新mysql

1、sofa zdal依賴,排除了一些不用的選項spring

<dependency>
			<groupId>com.alipay.boot</groupId>
			<artifactId>zdal4-jdbc-spring-boot-starter</artifactId>
			<version>1.1.0</version>
			<exclusions>
				<exclusion>
					<groupId>com.alipay.kmi</groupId>
					<artifactId>*</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.alipay.common</groupId>
					<artifactId>tracer-biz</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.alipay.common</groupId>
					<artifactId>tracer</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.alipay.drm</groupId>
					<artifactId>drm-client</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.taobao.configserver</groupId>
					<artifactId>*</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.alipay.configserver</groupId>
					<artifactId>*</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.taobao.logstat</groupId>
					<artifactId>*</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.alipay.sofa</groupId>
					<artifactId>*</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.alipay.sofa.common.log</groupId>
					<artifactId>sofa-middleware-log</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>log4j-over-slf4j</artifactId>
			<scope>provided</scope>
		</dependency>

2、集成配置sql

 

   1. 在項目resources下新建文件 zdal/config/db/cxinscn-doc-dev-ds.xml數據源配置xml具體內容以下數據庫

   注意:該文件的命名規範爲 {appname}-{environmentId}-ds.xmlspring-mvc

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd"
       default-autowire="byName">

    <bean id="cxinscn-doc" class="com.alipay.zdal.client.config.bean.ZdalAppBean">
        <property name="appName" value="cxinscn-doc"/>
        <property name="dbmode" value="dev"/>
        <property name="appDataSourceList">
            <list>
                <ref bean="appDataSource"/>
            </list>
        </property>
    </bean>

    <!-- 單證單庫數據源 -->
    <bean id="appDataSource"  primary="true" class="com.alipay.zdal.client.config.bean.AppDataSourceBean">
        <property name="appDataSourceName" value="appDataSource"/>
        <property name="dataBaseType" value="MYSQL"/>
        <property name="physicalDataSourceSet">
            <set>
                <ref bean="physicalDataSource"/>
            </set>
        </property>
        <property name="groupDataSourceRuleMap">
            <map>
                <entry key="singleG_0" value="singleM_0:r10w10" />
            </map>
        </property>
    </bean>
    <bean id="physicalDataSource" class="com.alipay.zdal.client.config.bean.PhysicalDataSourceBean">
        <property name="name" value="physicalDataSource"/>
        <property name="type" value="MYSQL"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/xx?allowMultiQueries=true"/>
        <property name="userName" value="數據庫帳號"/>
        <property name="password" value="數據庫密碼"/>
        <property name="minConn" value="1"/>
        <property name="maxConn" value="100"/>
        <property name="blockingTimeoutMillis" value="60000"/>
        <property name="idleTimeoutMinutes" value="1"/>
        <property name="preparedStatementCacheSize" value="0"/>
        <property name="queryTimeout" value="0"/>
        <!--<property name="prefill" value="true"/>-->
        <property name="maxReadThreshold" value="100"/>
        <property name="maxWriteThreshold" value="100"/>
        <property name="logicDbNameSet">
            <set>
                <value>singleM_0</value>
            </set>
        </property>
        <property name="connectionProperties">
            <map>
                <entry key="connectTimeout" value="10000"/>
                <entry key="socketTimeout" value="10000"/>
            </map>
        </property>
    </bean> 
</beans>

 

 2.在 Spring 配置中添加 zdal 數據源與mybits集成,在項目resources下新建 resource/META-INF/spring/spring-mybatis.xml,命名隨意mybatis

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:sofa="http://schema.alipay.com/sofa/schema/slite"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
	   xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://schema.alipay.com/sofa/schema/slite http://schema.alipay.com/sofa/slite.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd" default-autowire="byName">


 <!-- DDS DataSource -->
    <sofa:zdal-jdbc id="dataSource">
        <sofa:appDataSourceName value="appDataSource"/>
    </sofa:zdal-jdbc>
   
    
    <!-- 事務管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
 	
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <!-- mybatis 配置 -->
    <bean id="sqlSessionFactoryMybatis" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:META-INF/mybatis/mybatis-configuration.xml"/>
        <property name="mapperLocations" value="classpath:META-INF/mybatis/mapper/*Mapper.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>
  
    <!--menbership數據庫-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cathay.dao.documents"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryMybatis"/>
    </bean>
   
</beans>

   

3.在 application-dev.properties 添加sofa一些配置mvc

zdal.jdbc.dbmode=dev  設置開發環境運行app

4.在spring boot 啓動類(Application) 中經過@ImportResource 讀取數據源配置xml。socket

package com.cathay;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;

@ImportResource({"classpath*:META-INF/spring/*.xml","classpath*:META-INF/cxinscn-doc/*.xml","classpath*:config/*.xml"})
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,
		DataSourceTransactionManagerAutoConfiguration.class})
public class DocApplication {
	// init the logger
    private static final Logger logger = LoggerFactory.getLogger(DocApplication.class);

    public static void main(String[] args){

    	System.setProperty("spring.profiles.active","dev");
        SpringApplication springApplication = new SpringApplication(DocApplication.class);
        ApplicationContext applicationContext = springApplication.run(args);
 
        if (logger.isInfoEnabled()){
            logger.info("application start");
        }

    }
}
相關文章
相關標籤/搜索