简体   繁体   English

在具有版本控制的项目之间共享Json Schema文件

[英]Sharing Json Schema files among projects with versioning

Problem 问题

We have a few independent projects (micro-services) that at some point in their life will refer to some JSON schema files, needles to say that each project has its own programming stack (mostly Nodejs and Golang though). 我们有一些独立的项目(微服务),在他们生命中的某些时候会引用一些JSON模式文件,可以说每个项目都有自己的编程堆栈(虽然主要是Nodejs和Golang)。

What are the best practices for sharing such data among different projects while keeping versioning. 在保持版本控制的同时,在不同项目之间共享此类数据的最佳做法是什么。

Bellow I describe my own solution, however I would like to get your feedback as well. 贝娄我描述了自己的解决方案,但我也想得到你的反馈。

my solution 我的解决方案

  1. I have set up a github project and put json schema files there 我已经设置了一个github项目并将json模式文件放在那里
  2. For each change to schemas I update the repo and tag it with a new version 对于模式的每次更改,我都会更新repo并使用新版本对其进行标记
  3. I use jsdelivr to access it like this: https://cdn.jsdelivr.net/gh/[GITHUB-ID]/[GITHUB-PROJECT]@[TAG]/schema.js 我使用jsdelivr来访问它: https//cdn.jsdelivr.net/gh/[GITHUB-ID]/[GITHUB-PROJECT]@[TAG]/schema.js
  4. When I want to clone the code of a random microservice which requires schema file(s), as part of build process I download the schema file from jsdelivr and save it to my local repo, However the downloaded schema file(s) are git ignored. 当我想要克隆需要模式文件的随机微服务的代码时,作为构建过程的一部分,我从jsdelivr下载模式文件并将其保存到我的本地存储库,但是下载的模式文件被忽略了git 。

What do you think about it, Is there a better and smoother way to handle this case? 您如何看待它,是否有更好,更顺畅的方法来处理这种情况?

Handling multiple dependencies is ( at least in my opinion ), one of the major pain points in working with multiple microservices. 处理多个依赖项(至少在我看来)是使用多个微服务的主要难点之一。 A few ways which I can think of or have seen are - 我能想到或已经看到的几种方式是 -

  1. Having a Monorepo - 拥有Monorepo -

    It becomes a lot easier if you have all your services co-located under just one repository. 如果您将所有服务共同位于一个存储库中,则会变得更加容易。 This means you might use monorepo helpers like lerna ( for JS projects ) or similar. 这意味着你可能会使用像lerna(用于JS项目)或类似的monorepo助手。 In this case you can have a folder structure like - 在这种情况下,你可以有一个文件夹结构,如 -

-- root - 根
---- shared schema files ( with versioning ) ----共享模式文件(带版本控制)
---- service 1 ----服务1
---- service 2 ----服务2

  1. Having a service which provides schema files - 拥有提供架构文件的服务 -

    The idea here is to have a separate repo for the schema files and serve them when any of your app loads. 这里的想法是为模式文件设置一个单独的repo,并在任何应用程序加载时提供它们。 This can be done in several ways - 这可以通过几种方式完成 -

    a). 一种)。 By maintaining different packages for different languages - like gems for ruby and node_modules for Node projects. 通过为不同语言维护不同的包 - 例如ruby的gems和Node项目的node_modules。 Then these modules are responsible for fetching relevant schemas from a central repo. 然后这些模块负责从中央仓库获取相关模式。

    b). B)。 By having a docker image running locally - you can also keep these schemas as docker image ( with versioning of course ) and server them by mounting as logical volumes. 通过在本地运行docker镜像 - 您还可以将这些模式保留为docker image(当然还有版本控制),并通过挂载为逻辑卷来服务它们。 The image can have all kinds of helper libraries you might need to maintain the schema ( maybe validators , tests, and UI views for schemas ). 图像可以包含维护模式所需的各种辅助库(可能是模式的验证程序,测试和UI视图)。

PS: you might also wanna have a look at tools like prototool . PS:你可能还想看看像prototool这样的工具。

From my perspective, the best way is to put JSON schema into a separated file (in your project or in a common repo in Github, etc.) and then refer to it from the documentation that you use to describe you APIs. 从我的角度来看,最好的方法是将JSON模式放入一个单独的文件中(在您的项目中或在Github中的公共存储库中等),然后从用于描述API的文档中引用它。 For example, if you use Swagger, you can include a reference to external URL (or file path). 例如,如果使用Swagger,则可以包含对外部URL(或文件路径)的引用。 Example 1 , example 2 . 例1例2

Versioning is anyway up to you, just name your JSON-files like some-entity-response-v2.json , and your APIs will probably have the same version. 版本控制无论如何都取决于您,只需将您的JSON文件命名为some-entity-response-v2.json ,并且您的API可能具有相同的版本。 Keep each version backward-compatible and you will be fine. 保持每个版本向后兼容,你会没事的。

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

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