SpringBoot 整合 Neo4j、MySQL 多数据源方案(Druid Mybatis DynamicDatasource)
本文总结了Neo4j和Spring/SpringBoot、Alibaba Druid、Dynamic Datasource、Mybatis等整合方案,对相应配置做了详细说明。
00 概述
本文总结了Neo4j和Spring/SpringBoot、Alibaba Druid、Dynamic Datasource、Mybatis等整合方案,对相应配置做了详细说明。
01 Spring Data Neo4j 整合方案
添加Neo4j JDBC Driver依赖
<!--Neo4j-Jdbc-Driver-->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>4.0.5</version>
</dependency>
添加application.yml配置
spring:
neo4j:
uri: bolt://localhost:7687 # neo4j+s://xxx.xxx.xxx
authentication:
username: neo4j
password: root
02 Alibaba Druid 整合方案
添加Neo4j JDBC Driver + Alibaba Druid依赖
<!--Neo4j-Jdbc-Driver-->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>4.0.5</version>
</dependency>
<!--Druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
添加application.yml配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: org.neo4j.jdbc.Driver
url: jdbc:neo4j:bolt://localhost:7687 #jdbc:neo4j:neo4j+s://xxx.xxx.xxx
username: neo4j
password: root
03 Dynamic Datasource 多数据源整合方案
添加Neo4j JDBC Driver、Alibaba Druid、Dynamic DataSource依赖
<!--Neo4j-Jdbc-Driver-->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>4.0.5</version>
</dependency>
<!--Druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
<!-- Dynamic DataSource -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!--Mybatis SpringBoot-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
添加application.yml配置
spring:
datasource:
druid:
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall,slf4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
dynamic:
datasource:
# Mysql Datasource
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/master
username: root
password: root
# Neo4j Datasource
neo4j:
driver-class-name: org.neo4j.jdbc.Driver
url: jdbc:neo4j:bolt://localhost:7687 #jdbc:neo4j:neo4j+s://xxx.xxx.xxx
username: neo4j
password: root
# Neo4j Setting
neo4j:
uri: bolt://localhost:7687 #neo4j+s://xxx.xxx.xxx
authentication:
username: neo4j
password: root
Mapper中加入数据源注解:@DS("neo4j")
@DS("neo4j")
@Repository
public interface Neo4jTestMapper {
List<SysDept> selectMovies();
}
Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kevinwong.mapper.Neo4jTestMapper">
<select id="selectMovies" resultType="MovieInfo">
MATCH (people:Movie)
RETURN
id(people) as id,
people.title as title,
people.tagline as tagline,
people.released as released LIMIT 10
</select>
</mapper>
查询日志:
JDBC Connection [com.baomidou.dynamic.datasource.tx.ConnectionProxy@501a99e2] will not be managed by Spring
==> Preparing: MATCH (people:Movie) RETURN id(people) as id, people.title as title, people.tagline as tagline, people.released as released LIMIT 10
==> Parameters:
<== Columns: id, title, tagline, released
<== Row: 0, The Matrix, Welcome to the Real World, 1999
<== Row: 9, The Matrix Reloaded, Free your mind, 2003
.........
<== Row: 56, What Dreams May Come, After life there is more. The end is just the beginning., 1998
<== Total: 10
Committing JDBC Connection [com.baomidou.dynamic.datasource.tx.ConnectionProxy@501a99e2]
04 注意事项
1.datasource中driver的配置
本文driverClassName配置为org.neo4j.jdbc.Driver,方便在uri变化时自动匹配不同driver。neo4j-jdbc-driver提供了HttpDriver、BoltDriver、BoltRoutingNeo4jDriver三种不同Scheme下的驱动,uri前缀不同时需要配置相应的driver,配置出错会产生连接异常。org.neo4j.jdbc.Driver下初始化了三种driver,根据uri配置的前缀自动匹配。
static {
DRIVERS.put("^neo4j(\\+s|\\+ssc)?$", BoltRoutingNeo4jDriver.class);
DRIVERS.put("^bolt(\\+s|\\+ssc)?$", BoltDriver.class);
DRIVERS.put("http[s]?", HttpDriver.class);
}
2.Dynamic Datasource 多数据源的配置
在多数据源配置中,在spring.dynamic.datasource下配置了neo4j数据源, 仍然配置了spring.neo4j相关参数,主要原因为Neo4jDriver会在启动时进行健康检查,如果不配置会产生健康检查失败告警:WARN o.s.b.actuate.neo4j.Neo4jReactiveHealthIndicator - Health check failed
2022-10-10 10:10:00 [Neo4jDriverIO-2-3] WARN o.s.b.actuate.neo4j.Neo4jReactiveHealthIndicator - Health check failed
org.neo4j.driver.exceptions.AuthenticationException: Unsupported authentication token, scheme='none' only allowed when auth is disabled: { scheme='none', user_agent='neo4j-java/dev' }
at org.neo4j.driver.internal.util.ErrorUtil.newNeo4jError(ErrorUtil.java:76)
at org.neo4j.driver.internal.async.inbound.InboundMessageDispatcher.handleFailureMessage(InboundMessageDispatcher.java:122)
.........
at org.neo4j.driver.internal.shaded.io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:750)
Suppressed: org.neo4j.driver.exceptions.ServiceUnavailableException: Connection to the database terminated. Please ensure that your database is listening on the correct host and port and that you have compatible encryption settings both on Neo4j server and driver. Note that the default encryption setting has changed in Neo4j 4.0.
at org.neo4j.driver.internal.util.ErrorUtil.newConnectionTerminatedError(ErrorUtil.java:56)
.........
更多推荐
所有评论(0)