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

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
注册配置 MSE Nacos/ZooKeeper,118元/月
简介: 微服务SpringBoot+Neo4j搭建企业级分布式应用拓扑图

正文


一、环境搭建


最便捷的方式就是访问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数据库的接口,我们先来看看它的继承关系:


9.png


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项目后,我们按照上一篇文章中的示例,依次增加系统节点和关系之后,可以得到图如下:


009.png


实现的系统架构可视化图


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


四、待解决问题


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


相关文章
|
7天前
|
缓存 监控 Java
优化Spring Boot应用的数据库访问性能
优化Spring Boot应用的数据库访问性能
|
8天前
|
缓存 NoSQL Java
Spring Boot中的分布式缓存实现
Spring Boot中的分布式缓存实现
|
8天前
|
NoSQL Java Redis
如何在Spring Boot中实现分布式锁
如何在Spring Boot中实现分布式锁
|
6天前
|
缓存 NoSQL Java
在Spring Boot中实现分布式缓存策略
在Spring Boot中实现分布式缓存策略
|
6天前
|
NoSQL Java MongoDB
使用Spring Boot构建响应式应用
使用Spring Boot构建响应式应用
|
6天前
|
存储 NoSQL Java
使用Spring Boot和MongoDB构建NoSQL应用
使用Spring Boot和MongoDB构建NoSQL应用
|
7天前
|
NoSQL Java 调度
在Spring Boot中实现分布式任务调度
在Spring Boot中实现分布式任务调度
|
7天前
|
缓存 监控 Java
如何优化Spring Boot应用的性能?
如何优化Spring Boot应用的性能?
|
4天前
|
运维 Kubernetes 监控
深入解析微服务架构的演进与实践
本文旨在探究微服务架构从诞生到成熟的发展历程,分析其背后的技术推动力和业务需求,并结合具体案例,揭示实施微服务过程中的挑战与解决策略。通过对微服务架构与传统单体架构的对比,阐明微服务如何优化现代应用开发流程,提高系统的可扩展性、可维护性和敏捷性。
14 0
|
2天前
|
监控 负载均衡 安全
探索微服务架构中的API网关模式
【7月更文挑战第13天】在微服务架构的海洋中,API网关犹如一座灯塔,指引着服务间的通信和客户端请求。本文将深入剖析API网关的核心作用、设计考量以及实现策略,为构建高效、可靠的分布式系统提供实践指南。
18 10