00 概述
本文總結(jié)了Neo4j和Spring/SpringBoot、Alibaba Druid、Dynamic Datasource、Mybatis等整合方案,對相應(yīng)配置做了詳細(xì)說明。
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 多數(shù)據(jù)源整合方案
添加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中加入數(shù)據(jù)源注解:@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>
查詢?nèi)罩荆?/p>
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下的驅(qū)動,uri前綴不同時需要配置相應(yīng)的driver,配置出錯會產(chǎn)生連接異常。org.neo4j.jdbc.Driver下初始化了三種driver,根據(jù)uri配置的前綴自動匹配。文章來源:http://www.zghlxwxcb.cn/news/detail-403376.html
static {
DRIVERS.put("^neo4j(\\+s|\\+ssc)?$", BoltRoutingNeo4jDriver.class);
DRIVERS.put("^bolt(\\+s|\\+ssc)?$", BoltDriver.class);
DRIVERS.put("http[s]?", HttpDriver.class);
}
2.Dynamic Datasource 多數(shù)據(jù)源的配置
在多數(shù)據(jù)源配置中,在spring.dynamic.datasource下配置了neo4j數(shù)據(jù)源, 仍然配置了spring.neo4j相關(guān)參數(shù),主要原因為Neo4jDriver會在啟動時進(jìn)行健康檢查,如果不配置會產(chǎn)生健康檢查失敗告警:WARN o.s.b.actuate.neo4j.Neo4jReactiveHealthIndicator - Health check failed文章來源地址http://www.zghlxwxcb.cn/news/detail-403376.html
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)
.........
到了這里,關(guān)于SpringBoot 整合 Neo4j、MySQL 多數(shù)據(jù)源方案(Druid Mybatis DynamicDatasource)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!