微服務(wù)SpringBoot+Neo4j搭建企業(yè)級分布式應(yīng)用拓?fù)鋱D

一、環(huán)境搭建

最便捷的方式就是訪問start.spring.io,新建一個項(xiàng)目,選擇的依賴有:

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

然后JDK需要選擇11版本,因?yàn)槲覀儺?dāng)前使用的Neo4j版本是4.4.7,可以在Neo4j的瀏覽器中左下角“About Neo4j”中看到使用的版本號,其對應(yīng)需要支持的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鏡像安裝的,我們可以進(jìn)入鏡像內(nèi)部,使用命令查看Neo4j運(yùn)行的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了,同樣的,在運(yùn)行如上我們下載的SpringBoot工程時,同樣需要選擇項(xiàng)目基于的JDK版本為11,然后才能正常運(yùn)行。

二、Neo4jRepository介紹

Neo4jRepository是SpringData為我們提供的用來操作Neo4j數(shù)據(jù)庫的接口,我們先來看看它的繼承關(guān)系:


Neo4jRepository的繼承關(guān)系

可以看到,通用的增刪查改功能基本都有了,如果我們的數(shù)據(jù)庫操作也是些簡單的操作,那基本就不用再添加方法了,直接使用Neo4jRepository提供的方法即可。當(dāng)然也支持我們自定義方法進(jìn)行操作,這個下面再信息講述。

三、代碼演示

這里的代碼示例,只是展示系統(tǒng)節(jié)點(diǎn)的增加、相互之間關(guān)系的創(chuàng)建,其它增刪查改操作,可以自行摸索學(xué)習(xí)。

@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é)點(diǎn)為:{}", systemEntityList);
        return systemEntityList;
    }

    public void addSystemNode(SystemEntity systemEntity){
        SystemEntity result = systemRepository.save(systemEntity);
        log.info("添加節(jié)點(diǎn)后的返回結(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é)點(diǎn)類,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é)點(diǎn)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);

}

然后,運(yùn)行如上SpringBoot項(xiàng)目后,我們按照上一篇文章中的示例,依次增加系統(tǒng)節(jié)點(diǎn)和關(guān)系之后,可以得到圖如下:


實(shí)現(xiàn)的系統(tǒng)架構(gòu)可視化圖

該例子和上一篇例子稍微有些不同,此處所有系統(tǒng)節(jié)點(diǎn)的類型都設(shè)置為了System,所以同一類型的節(jié)點(diǎn)顏色都是相同的。

四、待解決問題

可能是由于自己Cypher語言不熟悉,很多CRUD語句用的還不順暢,比如為什么用到id時,不能使用n.id的寫法,還有SystemRepository中調(diào)用關(guān)系的類型如何參數(shù)化,這樣就不用為每一種調(diào)用關(guān)系創(chuàng)建方法了,如果有知道的朋友可以告知下

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容