最近接手的几个项目对接形式和以往的有所不同,在此整理出来几篇文章简要分享。
以往和外部系统对接一般是http协议,走接口交互形式即使用restful接口对接,相互调用对方的接口以ip+端口号+方法路由形式构建的一个url。那么有些项目对于数据安全性要求很高,对于数据交互要有记录,会要求接口对接以中间表形式进行系统对接,即下发上报不直接走接口通信,而是对数据库进行读写操作。
业务流程:
使用此种对接形式,要先和上层系统确定好需要交互的接口和对应参数。每个接口对应一张接口表,参数为表字段,要有一个自增的主键id和状态status字段用于区分数据和执行状态,调用方每调用一次创建一条记录,接收方定时任务查询该表中新增未接收的记录,接收到后将状态改为已接收状态。当任务完成时候,上报将此任务状态改为已经完成。上层系统定时任务获取WCS上报完成数据,修改自己的状态。WMS还可以将已经下发但是WCS未接收的任务状态修改,实现任务取消功能。
接口表示例:
mysql
mysql-connector-java
org.mybatis.spring.boot
mybatis-spring-boot-starter
wcs-mysql.properties:
jdbc.wcs.driverClassName = com.mysql.jdbc.Driver
jdbc.wcs.url = jdbc:mysql://localhost:3306/webdemo?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false
jdbc.wcs.username = root
jdbc.wcs.password = root
wms-mysql.properties:
jdbc.wms.driverClassName = com.mysql.jdbc.Driver
jdbc.wms.url = jdbc:mysql://localhost:3306/wms_wcs?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false
jdbc.wms.username = root
jdbc.wms.password = root
项目中创建一个config目录创建对应数据源的配置文件,用于配置数据源的一些基本参数使用范围和使用的数据源。
WcsDataSourceConfig:
/**
* @author zhangqianwei
* @date 2022/7/4 20:27
*/
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration
@PropertySource("classpath:wcs-mysql.properties") //resource目录下对应的配置文件
//basePackages 指定的是此数据源的使用范围(以包为单位)
@MapperScan(basePackages = "com.test.demo.component.dao.wcsdao", sqlSessionTemplateRef = "wcsSqlSessionTemplate")
public class WcsDataSourceConfig {
@Value("${jdbc.wcs.driverClassName}")
private String driverClassName;
@Value("${jdbc.wcs.url}")
private String url;
@Value("${jdbc.wcs.username}")
private String userName;
@Value("${jdbc.wcs.password}")
private String password;
@Bean(name = "wcsDataSource")
public DataSource wcsDataSource() {
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setDriverClassName(driverClassName);
hikariDataSource.setJdbcUrl(url);
hikariDataSource.setUsername(userName);
hikariDataSource.setPassword(password);
hikariDataSource.setPoolName("wcs");
hikariDataSource.setMaxLifetime(290000);
hikariDataSource.setMaximumPoolSize(64);
hikariDataSource.setMinimumIdle(4);
return hikariDataSource;
}
@Bean("wcsSqlSessionFactory")
public SqlSessionFactory wcsSqlSessionFactory(@Qualifier("wcsDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setVfs(SpringBootVFS.class);
//配置xml
// sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
// getResources(""));
return sqlSessionFactory.getObject();
}
@Bean(name = "wcsTransactionManager")
public DataSourceTransactionManager wcsTransactionManager(@Qualifier("wcsDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "wcsSqlSessionTemplate")
public SqlSessionTemplate wcsSqlSessionTemplate(@Qualifier("wcsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
WmsDataSourceConfig:
/**
* @author zhangqianwei
* @date 2022/7/4 20:27
*/
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration
@PropertySource("classpath:wms-mysql.properties")
@MapperScan(basePackages = "com.test.demo.component.dao.wmsdao", sqlSessionTemplateRef = "wmsSqlSessionTemplate")
public class WmsDataSourceConfig {
@Value("${jdbc.wms.driverClassName}")
private String driverClassName;
@Value("${jdbc.wms.url}")
private String url;
@Value("${jdbc.wms.username}")
private String userName;
@Value("${jdbc.wms.password}")
private String password;
@Primary
@Bean(name = "wmsDataSource")
public DataSource wmsDataSource() {
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setDriverClassName(driverClassName);
hikariDataSource.setJdbcUrl(url);
hikariDataSource.setUsername(userName);
hikariDataSource.setPassword(password);
hikariDataSource.setPoolName("wms");
hikariDataSource.setMaxLifetime(290000);
hikariDataSource.setMaximumPoolSize(64);
hikariDataSource.setMinimumIdle(4);
return hikariDataSource;
}
@Primary
@Bean(name = "wmsSqlSessionFactory")
public SqlSessionFactory wmsSqlSessionFactory(@Qualifier("wmsDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setVfs(SpringBootVFS.class);
//配置xml
// sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
// getResources(""));
return sqlSessionFactory.getObject();
}
@Primary
@Bean(name = "wmsTransactionManager")
public DataSourceTransactionManager wmsTransactionManager(@Qualifier("wmsDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "wmsSqlSessionTemplate")
public SqlSessionTemplate wmsSqlSessionTemplate(@Qualifier("wmsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
以下链接是使用文档:
基础必读(免费) · dynamic-datasource · 看云