上一篇文章中我們介紹了 《【云原生專題】基于Docker+Neo4j圖數(shù)據(jù)庫搭建企業(yè)級分布式應用拓撲圖》,但是只介紹了使用Cypher語言在Neo4j的瀏覽器中執(zhí)行增刪查改的操作,現(xiàn)在我們想要基于SpringBoot來實現(xiàn)代碼層面的增刪查改。
一、環(huán)境搭建
最便捷的方式就是訪問start.spring.io
,新建一個項目,選擇的依賴有:
- spring-boot-starter-data-neo4j
- spring-boot-starter-web
- lombok
然后JDK需要選擇11版本,因為我們當前使用的Neo4j版本是4.4.7,可以在Neo4j的瀏覽器中左下角“About Neo4j”中看到使用的版本號,其對應需要支持的JDK版本可以在官網(wǎng)中查到:
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鏡像安裝的,我們可以進入鏡像內(nèi)部,使用命令查看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數(shù)據(jù)庫的接口,我們先來看看它的繼承關系:
Neo4jRepository的繼承關系
可以看到,通用的增刪查改功能基本都有了,如果我們的數(shù)據(jù)庫操作也是些簡單的操作,那基本就不用再添加方法了,直接使用Neo4jRepository提供的方法即可。當然也支持我們自定義方法進行操作,這個下面再信息講述。
三、代碼演示
這里的代碼示例,只是展示系統(tǒng)節(jié)點的增加、相互之間關系的創(chuàng)建,其它增刪查改操作,可以自行摸索學習。
@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("查詢所有的節(jié)點為:{}", systemEntityList);
return systemEntityList;
}
public void addSystemNode(SystemEntity systemEntity){
SystemEntity result = systemRepository.save(systemEntity);
log.info("添加節(jié)點后的返回結果為:{}", 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表示節(jié)點類,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);
/**
* 使用節(jié)點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項目后,我們按照上一篇文章中的示例,依次增加系統(tǒng)節(jié)點和關系之后,可以得到圖如下:
實現(xiàn)的系統(tǒng)架構可視化圖
該例子和上一篇例子稍微有些不同,此處所有系統(tǒng)節(jié)點的類型都設置為了System,所以同一類型的節(jié)點顏色都是相同的。文章來源:http://www.zghlxwxcb.cn/news/detail-782804.html
四、待解決問題
可能是由于自己Cypher語言不熟悉,很多CRUD語句用的還不順暢,比如為什么用到id時,不能使用n.id的寫法,還有SystemRepository中調(diào)用關系的類型如何參數(shù)化,這樣就不用為每一種調(diào)用關系創(chuàng)建方法了,如果有知道的朋友可以告知下文章來源地址http://www.zghlxwxcb.cn/news/detail-782804.html
到了這里,關于微服務SpringBoot+Neo4j搭建企業(yè)級分布式應用拓撲圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!