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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

在这里插入图片描述

文章目录


上一篇文章中我们介绍了 《【云原生专题】基于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.0 is the first major release that requires JDK 11. Custom extensions and procedures can be compiled now for JDK 11, for example, -target 11. It is generally 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
@RestController
public class SystemController {
    @Autowired
    private SystemService systemService;
    
    public static final String SUCCESS_RESULT = "success";

    @GetMapping("/getAllSystemNode")
    public List<SystemEntity> getAllSystemNode(){
        return systemService.getAllSystemNode();
    }

    @GetMapping("/findSystemById/{id}")
    public SystemEntity findSystemById(@PathVariable("id") Long id){
        SystemEntity result = systemService.findSystemById(id);
        log.info("{}", result);
        return result;
    }

    @PostMapping("/addSystemNode")
    public String addSystemNode(@RequestBody SystemEntity systemEntity){
        systemService.addSystemNode(systemEntity);
        return SUCCESS_RESULT;
    }

    @GetMapping("addInvokeRelation/{from}/{to}")
    public String addInvokeRelation(@PathVariable("from") Long from, @PathVariable("to") Long to){
        systemService.addInvokeRelation(from, to);
        return SUCCESS_RESULT;
    }

    @GetMapping("addConsumeRelation/{from}/{to}")
    public String addConsumeRelation(@PathVariable("from") Long from, @PathVariable("to") Long to){
        systemService.addConsumeRelation(from, to);
        return SUCCESS_RESULT;
    }

    @GetMapping("addProduceRelation/{from}/{to}")
    public String addProduceRelation(@PathVariable("from") Long from, @PathVariable("to") Long to){
        systemService.addProduceRelation(from, to);
        return SUCCESS_RESULT;
    }
}
@Slf4j
@Service
public class SystemService {
    @Resource
    private SystemRepository systemRepository;

    public List<SystemEntity> getAllSystemNode(){
        List<SystemEntity> systemEntityList = systemRepository.findAll();
        log.info("查询所有的节点为{}", systemEntityList);
        return systemEntityList;
    }

    public void addSystemNode(SystemEntity systemEntity){
        SystemEntity result = systemRepository.save(systemEntity);
        log.info("添加节点后的返回结果为{}", result);
    }

    public void addInvokeRelation(Long from, Long to){
        systemRepository.addInvokeRelation(from, to);
    }

    public void addConsumeRelation(Long from, Long to){
        systemRepository.addConsumeRelation(from, to);
    }

    public void addProduceRelation(Long from, Long to){
        systemRepository.addProduceRelation(from, to);
    }

    public SystemEntity findSystemById(Long id){
        return systemRepository.findSystemById(id);
    }
}
/**
 * Neo4jRepository<T, ID>
 * T表示节点类ID表示主键类型
 */
public interface SystemRepository extends Neo4jRepository<SystemEntity, Long> {

    @Query("MATCH (a),(b) WHERE id(a)=$from and id(b)=$to MERGE (a)-[:invoke]->(b)")
    void addInvokeRelation(@Param("from") Long from, @Param("to") Long to);

    @Query("MATCH (a),(b) WHERE id(a)=$from and id(b)=$to MERGE (a)-[:consume]->(b)")
    void addConsumeRelation(@Param("from") Long from, @Param("to") Long to);

    @Query("MATCH (a),(b) WHERE id(a)=$from and id(b)=$to MERGE (a)-[:produce]->(b)")
    void addProduceRelation(@Param("from") Long from, @Param("to") Long to);

    /**
     * 使用节点id时只能用id(n)这种写法其它属性可以用n.name这样的写法
     * @param id
     * @return
     */
    @Query("MATCH (n) where id(n)=$id RETURN n")
    SystemEntity findSystemById(@Param("id") Long id);

    //等价写法@Query("MATCH (n:SystemEntity {leader: $leader}) RETURN n")
    @Query("MATCH (n) where n.leader=$leader RETURN n")
    SystemEntity findSystemByLeader(@Param("leader") String leader);

}

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

img

实现的系统架构可视化图

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

四、待解决问题

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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring