strongloop/loopback-根据路由值更改连接字符串 [英] strongloop/loopback - Change connection string based on route value

查看:53
本文介绍了strongloop/loopback-根据路由值更改连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的用户在地理位置上分散,数据存储在各个区域.每个地区都有自己的数据中心和数据库服务器.

My application's users are geographically dispersed and data is stored in various regions. Each region has it's own data center and database server.

我想包括一个路由值,以指示用户要访问和连接的区域,如下所示:

I would like to include a route value to indicate the region that the user wants to access and connect to, as follows:

/api/region/1/位置/

/api/region/1/locations/

/api/region/2/locations/

/api/region/2/locations/

/api/region/3/locations/

/api/region/3/locations/

根据传入的区域,我想更改正在使用的连接字符串.我认为可以在中间件链中的某处执行此操作,但是不知道在哪里/如何执行.感谢您的帮助!

Depending on the region passed in, I would like to change the connection string being used. I assume this can be performed somewhere in the middleware chain, but don't know where/how. Any help is appreciated!

推荐答案

不应该做的事情

Loopback提供了一种方法 MyModel.attachTo (似乎没有记录,但对其进行了引用

What should not be done

Loopback provides a method MyModel.attachTo (doesnt seem to be documented, but a reference to it is made there ).

但由于它是一个静态方法,它会影响整个模型,而不是单个实例.

But since it is a static method, it affects the entire Model, not a single instance.

因此,要使其在每个请求的基础上工作,必须在调用数据源方法之前立即切换数据库,以确保在两者之间没有异步开始.我不认为这是可能的.

So for this to work on a per-request basis, you must switch the DB right before the call to the datasource method, to make sure nothing async starts in between. I don't think this is possible.

这是一个使用操作钩子的示例(并定义所有数据源,在下面的 datasources.json 中包括 dbRegion1 )

This is an example using an operation hook (and define all datasources, include dbRegion1 below in datasources.json)

不好,请不要在下面说.仅供参考

Bad, don't that below. Just for reference

Region.observe('loaded', function filterProperties(ctx, next) {
   app.models.Region.attachTo(app.dataSources.dbRegion1);
}

但是当您的API在短时间内收到多个请求时,您很可能会遇到并发问题.

But then you will most likely face concurrency issues when your API receives multiple requests in a short time.

(另一种查看方式是服务器不再是真正的无状态,执行将不仅取决于输入,而且取决于共享状态).

(Another way to see it is that the server is no longer truly stateless, execution will not depend only on inputs but also on a shared state).

当该钩子之后调用的方法希望对请求1使用 region1 时,该钩子可以为请求2设置 region2 .在挂钩和实际调用数据源方法之间触发.

The hook may set region2 for request 2 while the method called after the hook was expecting to use region1 for request 1. This will be the case if something async is triggered between the hook and the actual call to the datasource method.

所以最终,我认为您不应该这样做.我之所以把它放在那儿,是因为有人在其他SO帖子中推荐了它,但这很糟糕.

So ultimately, I don't think you should do that. I'm just putting it there because some people have recommended it in other SO posts, but it's just bad.

构建一个外部重新路由服务器,该服务器会将请求从API服务器重新路由到适当的区域数据库.

Build an external re-routing server, that will re-route the requests from the API server to the appropriate region database.

在您的API服务器中使用 loopback-connector-rest 来使用此微服务,并将其用作所有模型的单个数据源.这提供了对数据库选择的抽象.

Use the loopback-connector-rest in your API server to consume this microservice, and use it as a single datasource for all your models. This provides abstraction over database selection.

当然,仍然存在实现微服务的问题,但是也许您可以找到除回送之外的其他ORM,这些ORM将支持数据库分片,并在该微服务中使用它.

Then of course there is still the matter of implementing the microservice, but maybe you can find some other ORM than loopback's that will support database sharding, and use it in that microservice.

创建一个自定义的回送连接器,该连接器将充当 MySQL 查询的路由器.根据查询中传递的区域值,将查询重新路由到适当的数据库.

Create a custom loopback connector that will act as router for MySQL queries. Depending on region value passed inside the query, re-route the query to the appropriate DB.

使用更加分散的体系结构.编写特定于区域的服务器以保留特定于区域的数据.例如,运行3台不同的服务器,每台服务器配置一个区域.+ 1 个用于路由的公共服务器

Use a more distributed architecture. Write a region-specific server to persist region-specific data. Run for instance 3 different servers, each one configured for a region. + 1 common server for routing

然后为您的单个面向用户的REST api服务器构建路由中间件.基本示例:

Then build a routing middleware for your single user-facing REST api server. Basic example:

var express = require('express');
var request = require('request');

var ips = ['127.0.0.1', '127.0.0.2'];

app.all('/api/region/:id', function (req, res, next) {
  console.log('Reroute to region server ' + req.params.id);
  request(ips[req.params.id], function (error, response, body) {
    if (err) return next(err);
    next(null, body);
  });
});

也许这个选项是最容易做的

Maybe this option is the easiest to do

这篇关于strongloop/loopback-根据路由值更改连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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