0


微服务SpringBoot+Neo4j搭建企业级分布式应用拓扑图

在这里插入图片描述

文章目录

上一篇文章中我们介绍了
《【云原生专题】基于Docker+Neo4j图数据库搭建企业级分布式应用拓扑图》,但是只介绍了使用Cypher语言在Neo4j的浏览器中执行增删查改的操作,现在我们想要基于SpringBoot来实现代码层面的增删查改。

一、环境搭建

最便捷的方式就是访问

start.spring.io

,新建一个项目,选择的依赖有:

  • spring-boot-starter-data-neo4j
  • spring-boot-starter-web
  • lombok

然后JDK需要选择11版本,因为我们当前使用的Neo4j版本是4.4.7,可以在Neo4j的浏览器中左下角“About Neo4j”中看到使用的版本号,其对应需要支持的JDK版本可以在官网中查到:

1. JDK 11
Neo4j 4.0isthe first major release that requires JDK 11. Custom extensions and procedures can be compiled now for JDK 11,for example,-target 11. It isgenerally recommended to use the latest available JDK 11 to access all available fixes and performance improvements.

如果本地Neo4j是通过Docker镜像安装的,我们可以进入镜像内部,使用命令查看Neo4j运行的Java版本信息:

# java -version
openjdk version "11.0.15"2022-04-19
OpenJDK Runtime Environment 18.9(build 11.0.15+10)
OpenJDK 64-Bit Server VM 18.9(build 11.0.15+10, mixed mode, sharing)

如果不是通过Docker镜像安装的,那么本地就需要自行安装JDK11了,同样的,在运行如上我们下载的SpringBoot工程时,同样需要选择项目基于的JDK版本为11,然后才能正常运行。

二、Neo4jRepository介绍

Neo4jRepository是SpringData为我们提供的用来操作Neo4j数据库的接口,我们先来看看它的继承关系:

img

Neo4jRepository的继承关系

可以看到,通用的增删查改功能基本都有了,如果我们的数据库操作也是些简单的操作,那基本就不用再添加方法了,直接使用Neo4jRepository提供的方法即可。当然也支持我们自定义方法进行操作,这个下面再信息讲述。

三、代码演示

这里的代码示例,只是展示系统节点的增加、相互之间关系的创建,其它增删查改操作,可以自行摸索学习。

@Slf4j@RestControllerpublicclassSystemController{@AutowiredprivateSystemService systemService;publicstaticfinalString SUCCESS_RESULT ="success";@GetMapping("/getAllSystemNode")publicList<SystemEntity>getAllSystemNode(){return systemService.getAllSystemNode();}@GetMapping("/findSystemById/{id}")publicSystemEntityfindSystemById(@PathVariable("id")Long id){SystemEntity result = systemService.findSystemById(id);
        log.info("{}", result);return result;}@PostMapping("/addSystemNode")publicStringaddSystemNode(@RequestBodySystemEntity systemEntity){
        systemService.addSystemNode(systemEntity);return SUCCESS_RESULT;}@GetMapping("addInvokeRelation/{from}/{to}")publicStringaddInvokeRelation(@PathVariable("from")Long from,@PathVariable("to")Longto){
        systemService.addInvokeRelation(from,to);return SUCCESS_RESULT;}@GetMapping("addConsumeRelation/{from}/{to}")publicStringaddConsumeRelation(@PathVariable("from")Long from,@PathVariable("to")Longto){
        systemService.addConsumeRelation(from,to);return SUCCESS_RESULT;}@GetMapping("addProduceRelation/{from}/{to}")publicStringaddProduceRelation(@PathVariable("from")Long from,@PathVariable("to")Longto){
        systemService.addProduceRelation(from,to);return SUCCESS_RESULT;}}
@Slf4j@ServicepublicclassSystemService{@ResourceprivateSystemRepository systemRepository;publicList<SystemEntity>getAllSystemNode(){List<SystemEntity> systemEntityList = systemRepository.findAll();
        log.info("查询所有的节点为:{}", systemEntityList);return systemEntityList;}publicvoidaddSystemNode(SystemEntity systemEntity){SystemEntity result = systemRepository.save(systemEntity);
        log.info("添加节点后的返回结果为:{}", result);}publicvoidaddInvokeRelation(Long from,Longto){
        systemRepository.addInvokeRelation(from,to);}publicvoidaddConsumeRelation(Long from,Longto){
        systemRepository.addConsumeRelation(from,to);}publicvoidaddProduceRelation(Long from,Longto){
        systemRepository.addProduceRelation(from,to);}publicSystemEntityfindSystemById(Long id){return systemRepository.findSystemById(id);}}
/**
 * Neo4jRepository<T, ID>
 * T表示节点类,ID表示主键类型
 */publicinterfaceSystemRepositoryextendsNeo4jRepository<SystemEntity,Long>{@Query("MATCH (a),(b) WHERE id(a)=$from and id(b)=$to MERGE (a)-[:invoke]->(b)")voidaddInvokeRelation(@Param("from")Long from,@Param("to")Longto);@Query("MATCH (a),(b) WHERE id(a)=$from and id(b)=$to MERGE (a)-[:consume]->(b)")voidaddConsumeRelation(@Param("from")Long from,@Param("to")Longto);@Query("MATCH (a),(b) WHERE id(a)=$from and id(b)=$to MERGE (a)-[:produce]->(b)")voidaddProduceRelation(@Param("from")Long from,@Param("to")Longto);/**
     * 使用节点id时只能用id(n)这种写法,其它属性可以用n.name这样的写法
     * @param id
     * @return
     */@Query("MATCH (n) where id(n)=$id RETURN n")SystemEntityfindSystemById(@Param("id")Long id);//等价写法@Query("MATCH (n:SystemEntity {leader: $leader}) RETURN n")@Query("MATCH (n) where n.leader=$leader RETURN n")SystemEntityfindSystemByLeader(@Param("leader")String leader);}

然后,运行如上SpringBoot项目后,我们按照上一篇文章中的示例,依次增加系统节点和关系之后,可以得到图如下:

img

实现的系统架构可视化图

该例子和上一篇例子稍微有些不同,此处所有系统节点的类型都设置为了System,所以同一类型的节点颜色都是相同的。

四、待解决问题

可能是由于自己Cypher语言不熟悉,很多CRUD语句用的还不顺畅,比如为什么用到id时,不能使用n.id的写法,还有SystemRepository中调用关系的类型如何参数化,这样就不用为每一种调用关系创建方法了,如果有知道的朋友可以告知下


本文转载自: https://blog.csdn.net/weixin_42469135/article/details/127011564
版权归原作者 掂掂三生有幸 所有, 如有侵权,请联系我们删除。

“微服务SpringBoot+Neo4j搭建企业级分布式应用拓扑图”的评论:

还没有评论