Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建跨服务域模型映射?_Java_Spring_Spring Boot_Jpa_Microservices - Fatal编程技术网

Java 如何创建跨服务域模型映射?

Java 如何创建跨服务域模型映射?,java,spring,spring-boot,jpa,microservices,Java,Spring,Spring Boot,Jpa,Microservices,我在Spring Boot微服务体系结构中有两个微服务。比如说 热点服务 附件服务 这两个服务都包含一个域模型,当然最终都是在单独的jar中 热点域模型(热点服务) 附件域模型(附件服务) 如果您创建了一个新的热点,应该可以添加一个附加附件,如描述图像。因此,热点实体与其附件之间应该存在某种关联/映射。在单片应用程序中,这将通过使用JPA注释@OneToOne或类似的东西进行注释来实现 如何在微服务体系结构中实现这一点?这两个类都在单独的JAR/项目中!我考虑将标识符单独存储为Long,与J

我在Spring Boot微服务体系结构中有两个微服务。比如说

  • 热点服务
  • 附件服务
这两个服务都包含一个域模型,当然最终都是在单独的jar中

热点域模型(热点服务)

附件域模型(附件服务)

如果您创建了一个新的热点,应该可以添加一个附加附件,如描述图像。因此,热点实体与其附件之间应该存在某种关联/映射。在单片应用程序中,这将通过使用JPA注释
@OneToOne
或类似的东西进行注释来实现

如何在微服务体系结构中实现这一点?这两个类都在单独的JAR/项目中!我考虑将标识符单独存储为
Long
,与JPA无关。
有什么“更好的”/其他想法吗?

首先,我想问一下,你是如何划分你的微服务的?将相互依赖的资源分开是没有意义的。在您的示例中,我不确定附件是否可以在没有热点的情况下存在。如果不可能,将这两个实体放在两个单独的微服务中肯定没有意义


假设您的资源应该在两个微服务中分离,那么每个资源都需要一个URI。如果资源A与资源B相关,您可以将B的URI存储在A中,如果您的mircroservices应该是RESTful的,则在A到B中提供一个具有正确关系的链接。任何像OneTONE JPA关系这样的自动系统都不存在。

我们有一个半通用服务(如您示例中的附件服务)用于标记。您可以使用与我们使用的类似的结构,如下所示:

// Contains types of "things" that can accept attachments
// Probably unnecessary if you aren't multi-tenant
public class EntityType
{
   public int Id;
   public Guid TenantId;
   public string Name;
}

// Contains a list of Entities available to Attach to
public class Entity
{
   public int Id;
   public int EntityTypeId;
   public string EntityId; // The unique Identifier for this Instance of Entity
}

// Contains actual attachment values (URI's) assigned to an Entity instance
public class EntityAttachment
{
   public int Id;
   public int EntityId;
   public string Attachment;
}
尽管具体的需求略有不同,但这样的服务对于您构建一个通用的
附件
服务可能非常有用,该服务允许您连接几乎任何内容,但仍然可以保持数据良好的规范化和快速查询


我们动态地构建
EntityType
Entity
数据集-如果有人想向我们没有的类型或ID添加标记,我们会默默地添加它。

您可以将附件的URI存储为Entity类的一部分吗?只存储标识符有什么好处?REST中的资源有一个URI。如果只存储id,则必须在任何位置配置URI。但正如@Didier提到的,将这两个密切相关的微服务分开可能没有意义。微服务应该尽可能独立附件可以独立于热点存在,比如说作为用户的个人资料图像。。。为什么你会建议存储URI而不是标识符?URI就是标识符当然,我和你一样说过这一点。。。我刚才说的是数据的类型。但从战略的角度来看,这并不重要
@Entity
public class Attachment {

    @Id
    private Long id;
}
// Contains types of "things" that can accept attachments
// Probably unnecessary if you aren't multi-tenant
public class EntityType
{
   public int Id;
   public Guid TenantId;
   public string Name;
}

// Contains a list of Entities available to Attach to
public class Entity
{
   public int Id;
   public int EntityTypeId;
   public string EntityId; // The unique Identifier for this Instance of Entity
}

// Contains actual attachment values (URI's) assigned to an Entity instance
public class EntityAttachment
{
   public int Id;
   public int EntityId;
   public string Attachment;
}