简体   繁体   English

如何将 Reactivesearch 连接到外部 Elasticsearch 集群?

[英]How to Connect Reactivesearch to an external Elasticsearch cluster?

I am trying to connect my reacetivesearch application to external elasticsearch provider( not AWS).我正在尝试将我的 reacetivesearch 应用程序连接到外部 elasticsearch 提供商(不是 AWS)。 They dont allow making changes to the elasticsearch cluster and also using nginx in front of the cluster.他们不允许对 elasticsearch 集群进行更改,也不允许在集群前面使用 nginx。

As per the reacetivesearch documentation I have cloned the proxy code and only made changes to the target and the authentication setting(as per the code below ).根据 reacetivesearch 文档,我已经克隆了代理代码,并且只对目标和身份验证设置进行了更改(根据下面的代码)。

https://github.com/appbaseio-apps/reactivesearch-proxy-server/blob/master/index.js https://github.com/appbaseio-apps/reactivesearch-proxy-server/blob/master/index.js

Proxy is successfully starting and able to connect the remote cluster.代理已成功启动并能够连接远程集群。 However when I connect reacetivesearch app through proxy I get the following error.但是,当我通过代理连接 reacetivesearch 应用程序时,出现以下错误。

Access to XMLHttpRequest at ' http://localhost:7777/testing/_msearch ?'在“ http://localhost:7777/testing/_msearch ?”访问 XMLHttpRequest from origin ' http://localhost:3000 ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource来自原点' http://localhost:3000 '已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有'Access-Control-Allow-Origin' Z099FB995346F3395EZF6 上的请求资源

I repeated the same steps with my local elasticsearch cluster using the same proxy code and getting the same error.我使用相同的代理代码对本地 elasticsearch 集群重复了相同的步骤并得到相同的错误。

Just was wondering do we need to make any extra changes to make sure the proxy sending the right request to the elasticsearch cluster?只是想知道我们是否需要进行任何额外的更改以确保代理向 elasticsearch 集群发送正确的请求? I am using the below code for the proxy.我正在使用以下代码作为代理。

const express = require('express');
const proxy = require('http-proxy-middleware');
const btoa = require('btoa');
const app = express();
const bodyParser = require('body-parser')

/* This is where we specify options for the http-proxy-middleware
 * We set the target to appbase.io backend here. You can also
 * add your own backend url here */
const options = {
    target: 'http://my_elasticsearch_cluster_adddress:9200/',
    changeOrigin: true,
    onProxyReq: (proxyReq, req) => {
        proxyReq.setHeader(
            'Authorization',
            `Basic ${btoa('username:password')}`
        );
        /* transform the req body back from text */
        const { body } = req;
        if (body) {
            if (typeof body === 'object') {
                proxyReq.write(JSON.stringify(body));
            } else {
                proxyReq.write(body);
            }
        }
    }
}

/* Parse the ndjson as text */
app.use(bodyParser.text({ type: 'application/x-ndjson' }));

/* This is how we can extend this logic to do extra stuff before
 * sending requests to our backend for example doing verification
 * of access tokens or performing some other task */
app.use((req, res, next) => {
    const { body } = req;
    console.log('Verifying requests ✔', body);
    /* After this we call next to tell express to proceed
     * to the next middleware function which happens to be our
     * proxy middleware */
    next();
})

/* Here we proxy all the requests from reactivesearch to our backend */
app.use('*', proxy(options));

app.listen(7777, () => console.log('Server running at http://localhost:7777 🚀'));

Regards问候

Yep you need to apply CORS settings to your local elasticsearch.yaml as well as your ES service provider.是的,您需要将 CORS 设置应用于您的本地elasticsearch.yaml以及您的 ES 服务提供商。

Are you using Elastic Cloud by any chance?您是否有机会使用 Elastic Cloud? They do allow you to modify Elasticsearch settings.它们确实允许您修改 Elasticsearch 设置。

If so:如果是这样:

  1. Login to your Elastic Cloud control panel登录您的 Elastic Cloud 控制面板
  2. Navigate to the Deployment Edit page for your cluster导航到集群的部署编辑页面
  3. Scroll to your '[Elasticsearch] Data' deployment configuration滚动到您的“[Elasticsearch] 数据”部署配置
  4. Click the User setting overrides text at the bottom of the box to expand the settings editor.单击框底部的用户设置覆盖文本以展开设置编辑器。

There's some example ES CORS settings about halfway down the reactivebase page that provide a great starting point.有一些示例 ES CORS 设置大约在 reactivebase 页面的中间,提供了一个很好的起点。 https://opensource.appbase.io/reactive-manual/getting-started/reactivebase.html https://opensource.appbase.io/reactive-manual/getting-started/reactivebase.html

You'll need to update the provided http.cors.allow-origin: setting based on your needs.您需要根据需要更新提供的http.cors.allow-origin:设置。

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

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