简体   繁体   English

如何在多个实例中使用一个微服务的不同数据源

[英]How to use different datasource of one microservice with multi instances

I'm working on a microservices architecture using Spring boot, my microservice A uses MySQL as a primary datasource I'm wondering could I make an instance of A and change datasource of that instance to MongoDB?我正在使用 Spring 引导开发微服务架构,我的微服务A使用 MySQL 作为主要数据源我想知道我可以创建A的实例并将该实例的数据源更改为 MongoDB 吗?

Why I would do that?为什么我会那样做? well basically I'll have an endpoint which will handle a file uploads of million lines so I wish to have 2 datasources of that microservice.好吧,基本上我将有一个端点来处理百万行的文件上传,所以我希望拥有该微服务的 2 个数据源。

Or should I duplicate A to another microservice B and use MongoDb as datasource and make a specific rest api?或者我应该将A复制到另一个微服务B并使用 MongoDb 作为数据源并创建特定的 rest api?

If you use ORM (and I believe you using it) you have to create a model for both databases, because of the different concepts of MySQL and MongoDB, you have different sets of annotations.如果您使用 ORM(我相信您正在使用它),则必须为两个数据库创建一个 model,因为 MySQL 和 MongoDB 的概念不同,您有不同的注释集。 For example, for MongoDB you must use @Document instead of an @Entity.例如,对于 MongoDB,您必须使用 @Document 而不是 @Entity。 If only your service A targets this file uploads functionality - you can implement it together in one service.如果只有您的服务A针对此文件上传功能 - 您可以在一项服务中一起实现它。 If you have to make uploads from multiple services or if it makes sense to have separate scaling for service A functionality and file upload feature - make it a separate service for uploads.如果您必须从多个服务进行上传,或者如果对服务A功能和文件上传功能进行单独缩放是有意义的 - 将其设为单独的上传服务。 In most cases, the second way is more attractive because it allows avoiding service duty separation in two services when you have requirements changes在大多数情况下,第二种方式更有吸引力,因为当您有需求更改时,它可以避免两个服务中的服务职责分离

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

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