简体   繁体   English

在微服务架构(春季启动)中分离实体的最佳方法是什么?

[英]What is the best approach to seperate entities in microservices architecture (spring boot)?

I'm designing a new project with microservices, the first principal which was already impleneted is that each microservice has its own DB schema.我正在设计一个带有微服务的新项目,已经实现的第一个原则是每个微服务都有自己的数据库模式。 I have simple question about architecture.我有一个关于建筑的简单问题。 I'll explain with simple example.我会用简单的例子来解释。

I've created a Location service.我创建了一个位置服务。 Here is some code from the controller:这是来自 controller 的一些代码:

@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
@RestController
@RequestMapping("/locations")
@Slf4j
public class LocationController {

    @Autowired
    LocationService  locationService;
    
    @GetMapping("/Cntry")   
    public List<Country> getCountries() {
        return locationService.getdCountries();
    }
        
    @GetMapping("/States/{id}") 
    public List<State> getStatesForCountry(@PathVariable("id") String countryId) {      
        List<State> states = locationService.getStatesForCountry(Integer.valueOf(countryId));
        return states;
    }
    
    @GetMapping("/Cntry/{code}")    
    public Country getCountry(@PathVariable("code") String code) {          
        return locationService.getCountry(code);
    }   

As you can see above, the Location service which has local DB hold all the countries and states.正如您在上面看到的,具有本地数据库的位置服务包含所有国家和州。 The location service hold more entities related to the project such as Location, so it is multi purpose microservice.位置服务包含更多与项目相关的实体,例如位置,因此它是多用途微服务。

The problem I'm facing is that each microservice can have entities with country_id for example, but when running it almost gurantee that it will need the country name which mean a service call (web client).我面临的问题是,例如,每个微服务都可以有带有 country_id 的实体,但是在运行它时几乎可以保证它需要国家名称,这意味着服务调用(Web 客户端)。 Here is sample of such service call:以下是此类服务调用的示例:

@JsonIgnore
    public String getCountryString() {
        String url = MySpringBootServiceApplication.LOCATION_BASE_URL + "locations/CntryStr/" + countryId;
        WebClient client = WebClient.create(url);
        ResponseEntity<String> response = client.get()        
              .retrieve()
              .toEntity(String.class)
              .block();
        String countryStr = response.getBody();
        return countryStr;                  
    }

I have two problems here that I need to solve:我这里有两个问题需要解决:

  1. Is there a solution (Architecture) to avoid calling to get the country string every time and from each micro service?是否有解决方案(架构)来避免每次都从每个微服务调用获取国家字符串?
  2. A DTO in another service has country_id, but the user is looking for the name so, is there better way instead of make a webclient call inside DTO (doens't make sense).另一个服务中的 DTO 具有 country_id,但用户正在寻找名称,所以有没有更好的方法而不是在 DTO 中进行 webclient 调用(没有意义)。

Thanks for your help.谢谢你的帮助。

You can solve both problems by denormalizing: have each service maintain its own local mapping of country codes to names.您可以通过非规范化来解决这两个问题:让每个服务维护自己的国家代码到名称的本地映射。

You can still have the location service govern updating country codes to names;您仍然可以让位置服务管理将国家/地区代码更新为名称; it then publishes (in the rare case when a code-to-name mapping changes) events when they change for consumption by the other services to signal that they should update their respective mappings.然后,它会在事件发生更改以供其他服务使用时发布(在极少数情况下,代码到名称的映射发生更改),以表明它们应该更新各自的映射。 Because the responsibility for updating this mapping is in one service while (many) other services have a responsibility for presenting the mapping, this is an example of the Command/Query Responsibility Segregation pattern (CQRS).因为更新此映射的责任在一个服务中,而(许多)其他服务有责任呈现映射,这是命令/查询责任分离模式 (CQRS) 的一个示例。

In general taking a normalized relational schema and turning it into a microservice architecture leads to the issue you're facing.一般来说,采用规范化的关系模式并将其转换为微服务架构会导致您面临的问题。 Denormalization can avoid the problem, but then focusing on the operations (the verbs) rather than the data (the nouns) lead to a much more effective and easier to work with architecture.非规范化可以避免这个问题,但是关注操作(动词)而不是数据(名词)会导致更有效和更容易使用架构。

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

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