SpringBoot 整合 Neo4j、MySQL 多数据源方案(Druid Mybatis DynamicDatasource)

这篇具有很好参考价值的文章主要介绍了SpringBoot 整合 Neo4j、MySQL 多数据源方案(Druid Mybatis DynamicDatasource)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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文章来源地址https://www.toymoban.com/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)
    .........

到了这里,关于SpringBoot 整合 Neo4j、MySQL 多数据源方案(Druid Mybatis DynamicDatasource)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • springboot整合neo4j模糊查询

    1.场景 查询与content相似的实体 解决方案: 1.直接从neo4j中查询所有实体并使用杰卡德相似度算法计算相似度,返回top n,该方案由于要匹配图中所有实体,性能较差。 2.模糊查询neo4j中的实体,并对查询结果与content做相似度计算,相似度算法为hutool中的TextSimilarity.similar()接口

    2024年02月13日
    浏览(28)
  • 图数据库_Neo4j和SpringBoot整合使用_创建节点_删除节点_创建关系_使用CQL操作图谱---Neo4j图数据库工作笔记0009

    首先需要引入依赖   springboot提供了一个spring data neo4j来操作 neo4j   可以看到它的架构   这个是下载下来的jar包来看看 有很多cypher对吧   可以看到就是通过封装的驱动来操作graph database   然后开始弄一下 首先添加依赖

    2024年02月12日
    浏览(38)
  • springboot整合neo4j-使用原生cypher

    该文的实现有更简单的方式,详见我的另一篇博客springboot整合neo4j–采用Neo4jClient和Neo4jTemplate方式 Neo4j 提供 JAVA API 以编程方式执行所有数据库操作。它支持三种类型的API: 1、Neo4j 原生的 Java API 原生 Java API 是一种低级别的纯 JAVA API,用于执行数据库操作。 2、Neo4j Cypher Jav

    2024年02月12日
    浏览(32)
  • 【微服务】springboot整合neo4j使用详解

    在上一篇我们详细了解了neo4j的使用,从搭建到相关的语法操作,本篇紧接着之前的内容,来详细聊聊如何在springboot应用中集成和使用neo4j。 和很多其他的中间件类似,都提供了类似jpa的方式与springboot进行集成,比如大家熟悉的springdata-jpa,操作es的jpa,操作mongo的jpa等,而

    2024年02月08日
    浏览(33)
  • springboot整合neo4j-使用原生cypher Java API

    该文的实现有更简单的方式,详见我的另一篇博客springboot整合neo4j–采用Neo4jClient和Neo4jTemplate方式 Neo4j 提供 JAVA API 以编程方式执行所有数据库操作。它支持三种类型的API: 1、Neo4j 原生的 Java API 原生 Java API 是一种低级别的纯 JAVA API,用于执行数据库操作。 2、Neo4j Cypher Jav

    2024年02月09日
    浏览(39)
  • 图数据库Neo4j——SpringBoot使用Neo4j & 简单增删改查 & 复杂查询初步

    图形数据库是专门用于存储图形数据的数据库,它使用图形模型来存储数据,并且支持复杂的图形查询。常见的图形数据库有Neo4j、OrientDB等。 Neo4j是用Java实现的开源NoSQL图数据库,本篇博客介绍如何在SpringBoot中使用Neo4j图数据库,如何进行简单的增删改查,以及如何进行复杂

    2024年02月06日
    浏览(42)
  • Springboot项目连接neo4j数据库

    首先创建一个springboot项目,这里不再介绍。 连接 neo4j 数据库的依赖包 spring-boot-starter-data-neo4j依赖包 mybatis-plus依赖包

    2024年02月12日
    浏览(37)
  • Spring Boot整合neo4j

    相关版本信息 1、配置文件 Pom文件中引入依赖 Spring生态中Spring-data部分不仅仅提供了Spring-data-jpa , 也提供了Spring-data-neo4j 支持spring和 neo4j的完美融合,pom.xml 文件中依赖 yml文件中配置连接属性 2、实体类(NodeEntity) @NodeEntity: 标明是一个节点实体@RelationshipEntity:标明是一个

    2024年02月10日
    浏览(28)
  • SpringBoot版本和Neo4j图数据库版本对应关系

    Neo4j OGM Version Neo4j Version Bolt Version# Spring Data Neo4j Version Spring Boot Version 3.1.0+ 3.1.x, 3.2.x, 3.3.x 1.5.0+ (compatible with 1.4.0+) 5.1.0+ (compatible with 5.0.0+) 2.0.0+ 3.0.0+ 3.1.x, 3.2.x, 3.3.x 1.4.0+ 5.0.0+ 2.0.0+ 2.1.0+ 2.3.x, 3.0.x, 3.1.x 1.1.0+ 4.2.0+ 1.5.0+ 2.0.2+ 2.3.x, 3.0.x 1.0.0+ 4.1.2 - 4.1.6+ 1.4.x 2.0.1* 2.2.x, 2.3.x 1.0.0-

    2024年02月09日
    浏览(38)
  • springboot+neo4j

    请通过依赖项管理包含启动器模块并配置要使用的 Bolt URL,例如 spring.neo4j.uri=bolt://localhost:7687 。启动器假设服务器已禁用身份验证。由于 SDN 启动器依赖于 Java 驱动程序的启动器,因此此处所说的有关配置的所有内容也适用于此处。有关可用属性的参考,请在 spring.neo4j 命名

    2024年01月20日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包