简体   繁体   English

Neo4J在微服务架构中

[英]Neo4J In Microservices Architecture

To keep in line with DDD and Bounded Contexts, its well known that when you create your microservices you should keep separation of concerns. 为了与DDD和有界上下文保持一致,众所周知,当您创建微服务时,应保持关注点分离。

One of the main benefits of Neo4J is keeping your "connected" data in Neo4J so relationships between them are efficiently queried. Neo4J的主要优点之一是将您的“连接”数据保留在Neo4J中,以便有效地查询它们之间的关系。

These two opposing forces seem to make an microservice architecture decision difficult when choosing to use Neo4J. 当选择使用Neo4J时,这两个相反的力量似乎使微服务架构决策变得困难。

Do you have multiple microservices connect to Neo4J db and persist their own domain accordingly? 您是否有多个微服务连接到Neo4J db并相应地保留其自己的域?

OR 要么

Do you have one microservice with a db connection to Neo4J that controls persistance and querying? 您是否有一个微服务,它具有与Neo4J的数据库连接以控制持久性和查询?

Both dont seem quite right... 两者似乎都不正确...

The pattern of database-per-service is discussed here , and breaks down the options to: 此处讨论了“每个服务的数据库”模式,并细分了以下选项:

  1. Shared database, but private tables per service. 共享数据库,但每个服务都有专用表。
  2. Shared database, but private schema per service. 共享数据库,但每个服务都有专用模式。
  3. Separate database per service. 每个服务使用独立的数据库。

Obviously 3 is going to be the most expensive, as you would want each Neo4j instance to be on its own server so it has dedicated memory and hardware, and if you need a clustering solution then this becomes a separate cluster-per-service. 显然3将是最昂贵的,因为您希望每个Neo4j实例都在其自己的服务器上,以便具有专用的内存和硬件,并且,如果您需要集群解决方案,那么它将变成每个服务一个单独的集群。 Not recommended, especially if just ideology is the driver for this decision. 不建议这样做,尤其是当意识形态是决定的驱动力时。

1 and 2 are better options, especially if the data accessed across microservices is related intrinsically, as Neo4j works best when storing connected data, and loses its value the more the data is siloed between multiple databases. 1和2是更好的选择,特别是如果跨微服务访问的数据具有内在关联性时,因为Neo4j在存储连接的数据时效果最佳,并且在多个数据库之间存储的数据越多,丢失的价值就越大。

That said, there are some challenges with these options. 也就是说,这些选项存在一些挑战。

Neo4j does not use tables, and does not currently have separate schema to divide visibility of data between different users. Neo4j不使用表,并且当前没有单独的架构来划分不同用户之间的数据可见性。

While you can have the microservice only use defined queries that only touch specific parts of the graph, this is often looser control than is required. 虽然您可以让微服务仅使用仅接触图的特定部分的已定义查询,但这通常比所需的控件宽松。

You can instead use the subgraph access control approach, and this is the one I would most recommend for your needs. 您可以改用子图访问控制方法,这是我最推荐的一种方法。

This boils down to creating procedures that encapsulate the queries you want to be available for each microservice (that either use the Java API directly, or make a Cypher query from the procedure code), and then creating custom roles for each microservice (without read permissions), but allowing them to invoke the appropriate procedures. 这归结为创建过程,该过程封装了您希望可用于每个微服务的查询(可以直接使用Java API或从过程代码中进行Cypher查询),然后为每个微服务创建自定义角色(无读取权限) ),但允许他们调用适当的程序。 This ensures that the custom role per microservice is only allowed to interact with the graph through the allowed procedures (and the procedures of course can do whatever they want without being constrained by the roles or permissions of the calling user). 这样可以确保仅允许每个微服务的自定义角色通过允许的过程与图进行交互(并且这些过程当然可以执行它们想要的任何操作,而不受调用用户的角色或权限的约束)。

As far as a multi-tenancy approach, keeping data separate between different graphs on a single database, that is a feature of high interest right now, and implementation is in the works. 就多租户方法而言,将数据保持在单个数据库的不同图形之间是分开的,这是当前引起人们高度关注的功能,并且正在实施中。 Look for this in upcoming releases in 2018. That said, whether this would be useful to you or not depends upon the implementation of this feature, and the nature of your data. 在2018年即将发布的版本中寻找这一点。也就是说,这对您是否有用取决于此功能的实现以及数据的性质。

We do not prefer sharing database across multiple services, that makes their deployments and upgrades difficult. 我们不希望跨多个服务共享数据库,这会使它们的部署和升级变得困难。

Generally, you can take event stream from multiple Microservices and one or multiple Microservice use neo4j to create graph datastructure specific for their use case. 通常,您可以从多个微服务获取事件流,并且一个或多个微服务使用neo4j来创建特定于其用例的图数据结构。

In this data duplication will happen so you have to take a judicious call on when to duplicate the data. 在这种情况下,将发生数据复制,因此您必须明智地决定何时复制数据。

I would recommend to have one Neo4j instance per microservice. 我建议每个微服务有一个Neo4j实例。 Then, every microservice owns their own database. 然后,每个微服务都拥有自己的数据库。

There are several patterns you could use. 您可以使用几种模式。

For once there is still the possibility of MS sharing databases, as long as it is within their bounded context. 一次,仍然存在MS共享数据库的可能性,只要它在其有限上下文内即可。 As only beyond the translation layer there should be no sharing on the data layer. 由于仅在转换层之外,因此在数据层上不应共享任何内容。

Alternatively, you could see Neo4j as the database powering certain microservices that utilize the graph, eg recommendation, fraud etc. That database could then be populated by domain-events in an event-sourcing architecture. 或者,您可以将Neo4j视为支持利用该图的某些微服务的数据库,例如推荐,欺诈等。然后可以通过事件源体系结构中的域事件来填充该数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM