如何在春季启动时在微服务中创建Super/Base/Parent类-在子类中扩展该类 [英] How to make Super/Base/Parent class in microservices in spring-boot - to extend that class in child class

查看:102
本文介绍了如何在春季启动时在微服务中创建Super/Base/Parent类-在子类中扩展该类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1) 我有3个微服务项目(国家微,州微和城市微),每个微服务中分别定义了3个实体,分别是国家,州和城市.请注意,所有3个微服务在pom.xml中具有相同的组ID

1) I have 3 microservices project (Country-micro, State-micro and City-micro) with 3 entities Country, State and City in each microservices define respectively. Please note that all 3 microservices has same group id in pom.xml

所有实体都有共同的4个字段

All the entity has common 4 fields

  1. id
  2. created_date
  3. modified_date
  4. is_active

如何为以上字段创建基础实体类,并将其用于不同的微服务中.就像在单片式架构中,我们可以通过扩展在此定义的类来轻松使用:

How can I make base entity class for above fields and use it in different microservices. Like, in monolithic architecture we can use easily by extending class as define here :

我的基本班级

@MappedSuperclass
@Data
public class BaseEntity {
    private Long id;
    private Date created_date;
    private Date modified_date;
    private Boolean is_active;

}

我的孩子上课

@Entity
@Data
@EqualsAndHashCode(callSuper=false)
@Table(name="tbl_country")
public class Country extends BaseEntity {
    private String countryName;
    private String countryCode;
}

但是,在微服务架构中,所有实体都是分离的.然后,如何定义要作为Super类进行的服务.

But, in microservices architecture, all entities are separated. Then, how can I define service to make as Super class.

2)对于上述相同的问题,我还想在每个微服务中使用我的常量类.就像,在整体结构中,我们可以:

2) With the same question above, I also want to use my constant class in each microservices. Like, in monolithic structure, we can :

public interface Constants {
    static String HTTP = "http";
    static String COMMA_AND_SPACE = ", ";
}

并且可以在以下任何类中使用此常量:

And can use this constants in any class like:

public String myMethod(MyDto myDto) {
    StringBuilder sb = new StringBuilder();

    sb.append("My First String").append(Constants.COMMA_AND_SPACE);
    sb.append("My Second String").append(Constants.COMMA_AND_SPACE);
}

我可以在哪个服务中定义常量,我应该创建单独的服务吗?或者我必须在每个微服务中定义相同的常量(例如代码冗余).我也以相同的方式在整体结构中使用Utility类.

In which service I can define constants, Should I create separate service? or I have to define same constants in every microservice (like code redundant). I am also using Utility class in monolithic structure same way.

请解释结构或指导我定义基类或实用程序.

Please explain structure or guide me to define base classes or Utils.

谢谢.

推荐答案

您可以将一个新模块创建为Common,并将所有通用代码放入其中.

You can create one new module as Common and put all your common code in it.

只需在pom.xml中需要的任何项目中添加它的依赖项即可.

Just add it's dependency in whichever project needed in pom.xml.

因此,即使单独运行Microservice1,您也必须在其中绑定Common模块.

So even if you run Microservice1 separately you will have to bind Common module as well in it.

例如在Microservice1 pom.xml中添加公共依赖项

eg in Microservice1 pom.xml add common dependency

<dependency>
    <groupId>com.sample.common</groupId>
    <artifactId>Common</artifactId>
    <version>${project.version}</version>
</dependency>

也可以阅读此书

这篇关于如何在春季启动时在微服务中创建Super/Base/Parent类-在子类中扩展该类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆