diff --git a/Docs/ES本机开发部署.ipynb b/Docs/ES本机开发部署.ipynb new file mode 100644 index 0000000..22ec872 --- /dev/null +++ b/Docs/ES本机开发部署.ipynb @@ -0,0 +1,240 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "ES本机开发环境部署\n", + "================" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "基于 Elasticsearch 8.7.0" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 本机单机部署(启动默认安全设置)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Elasticsearch 8.7.0" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 首次启动及配置" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### 常用启动方式:\n", + "+ 双击 bin 目录中的 elasticsearch.bat\n", + "+ 命令行中启动 elasticsearch.bat\n", + " \n", + "注意:\n", + "+ 不要使用管理员帐号运行命令行,es默认不能在root或administrator帐号运行\n", + "+ 最好使用命令行,不要使用 powershell\n", + "+ 首次启动,成功启动后会显示帐号密码、kibana Token、集群加入Token等信息,需要记录下来,因为这些信息只在首次启动时显示。" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### 自定义密码\n", + "ES根目录,命令行执行:`.\\bin\\elasticsearch-reset-password --username 用户名 -i` -i 是交互式执行的意思,执行后输入两次相同密码(输入时光标不会动)就行" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### 关闭ES, 对配置文件进行修改\n", + "+ 配置内存占用为4G `config/jvm.options` 文件中指定 -Xms4g -Xmx4g\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Kibanan 8.7.0" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "+ 启动 Kibana \n", + "+ 使用 首次启动ES服务时记录帐号信息及 `kibana token` 登陆\n", + "+ 关闭kibana服务,打开配置文件进行更改 i18n.locale: \"zh-CN\" 等\n", + " " + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 启动脚本" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在 \"本机单节点要目录(`D:\\ELK\\8.7.0\\SingleNode`)\"下有一个脚本文件`启动脚本.bat`,可以同时打开两个命令行窗口,分别启动`ES服务`和相应的`kibana服务`" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 本机三节点集群(启用安全)\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 文件部署" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "+ 设置集群根文件夹:`D:\\ELK\\8.7.0\\Cluster`\n", + "+ 下载 `Elasticsearch 8.7.0`文件,解压并全量复制3分,分别为`es-master` `es-node1` `es-node2` \n", + "+ 下载 `Kibana 8.7.0` 文件,解压并存放为 `es-master 同级目录`的 `kibana`" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 设置编辑配置文件" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "+ 所有节点配置集群名:cluster.name: study-es-cluster\n", + "+ Master节点:node.name: study-es-cluster-master network.host: 127.0.0.1 http.port: 9201\n", + "+ Node1节点:node.name: study-es-cluster-node1 network.host: 127.0.0.1 http.port: 9202\n", + "+ Node2节点:node.name: study-es-cluster-node2 network.host: 127.0.0.1 http.port: 9203\n", + "+ Kibana: server.port: 5602" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Master 节点操作" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "+ 启动master节点,并记录相关信息:用户名密码、指纹、kibanaToken等\n", + "+ 自定义 elstic 密码:master根目录,运行命令行:`./bin/elasticsearch-reset-password --username elastic -i`\n", + "+ 信任CA证书(在认证时不易出错):进入 `master根目录/config/cert/certs/` 双击 `http_ca.crt` -> 安装证书 -> 本地计算机 -> 下一步 -> 将所有证书都放入下列存储 -> 浏览 -> 受信任的根证书颁发机构\n", + "+ 创建注册令牌,其它节点加入集群时使用:`./bin/elasticsearch-create-enrollment-token -s node` 记住生成的 Token 备用\n", + "+ 在`config/aelasticsearch.yml`的末尾取消注释`transport.host`设置。如果配置项已自动生成则忽略\n", + "+ 最好重新启动下 master 节点" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 其它节点加入集群" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在每个要加入集群的节点上都执行:\n", + "+ 在节点根目录执行命令行:./bin/elasticsearch --enrollment-token Master节点生成的token" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 启动与配置 kibana" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "+ 修改配置 server.port: 5602\n", + "+ 启动kibanan\n", + "+ 使用Master节点首次启动时记录下的 kibana token 登陆,过期的话,使用下面命令行刷新token ./bin/elasticsearch-create-enrollment-token -s kibana --url \"https://127.0.0.1:9201\"\n", + "+ 关闭Kibana,修改配置文件,变成中文设置 i18n.locale: \"zh-CN\"" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".NET (C#)", + "language": "C#", + "name": ".net-csharp" + }, + "language_info": { + "name": "polyglot-notebook" + }, + "orig_nbformat": 4, + "polyglot_notebook": { + "kernelInfo": { + "defaultKernelName": "csharp", + "items": [ + { + "aliases": [], + "name": "csharp" + } + ] + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Docs/启动ES.ipynb b/Docs/启动ES.ipynb new file mode 100644 index 0000000..8b33a83 --- /dev/null +++ b/Docs/启动ES.ipynb @@ -0,0 +1,73 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "启动ES相关服务\n", + "=============" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 启动本机单节点 ES 及 kibana" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "dotnet_interactive": { + "language": "pwsh" + }, + "polyglot_notebook": { + "kernelName": "pwsh" + } + }, + "outputs": [], + "source": [ + "#!powershell\n", + "# 判断ES服务是否启动\n", + "\n", + "cd \"D:\\ELK\\8.7.0\\SingleNode\\\"\n", + ".\\启动脚本.bat" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 启动Kibana" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".NET (C#)", + "language": "C#", + "name": ".net-csharp" + }, + "language_info": { + "name": "polyglot-notebook" + }, + "orig_nbformat": 4, + "polyglot_notebook": { + "kernelInfo": { + "defaultKernelName": "csharp", + "items": [ + { + "aliases": [], + "name": "csharp" + } + ] + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Docs/启动ES.ps1 b/Docs/启动ES.ps1 new file mode 100644 index 0000000..1f5560b --- /dev/null +++ b/Docs/启动ES.ps1 @@ -0,0 +1,4 @@ +# 启动ES及Kibana +Set-Location "D:\ELK\8.7.0\SingleNode\" + +.\启动脚本.bat \ No newline at end of file diff --git a/ElasticSearchStudy.App/ElasticSearchStudy.App.csproj b/ElasticSearchStudy.App/ElasticSearchStudy.App.csproj index b03e64f..c855518 100644 --- a/ElasticSearchStudy.App/ElasticSearchStudy.App.csproj +++ b/ElasticSearchStudy.App/ElasticSearchStudy.App.csproj @@ -7,6 +7,10 @@ enable + + + + diff --git a/ElasticSearchStudy.App/Program.cs b/ElasticSearchStudy.App/Program.cs index b3b2adf..5d9fd48 100644 --- a/ElasticSearchStudy.App/Program.cs +++ b/ElasticSearchStudy.App/Program.cs @@ -1,10 +1,30 @@ -namespace ElasticSearchStudy.App +using RestSharp; +using RestSharp.Extensions; +using RestSharp.Serializers; +using RestSharp.Authenticators; + +using System.Security.Cryptography.X509Certificates; +using System.Net; + +namespace ElasticSearchStudy.App { internal class Program { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + Console.WriteLine("使用 ElistcSearch WebApi"); + + var option = new RestClientOptions() + { + BaseUrl = new Uri("https://localhost:9200"), + Authenticator = new HttpBasicAuthenticator("elastic", "jLAglHZ1Wvt9ikOjb2TY") + + }; + + var restClient = new RestClient(option); + var request = new RestRequest("/_cluster/health", Method.Get); + + var dd = restClient.Get(request); } } } \ No newline at end of file diff --git a/ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs b/ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs index 15598ef..8408319 100644 --- a/ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs +++ b/ElasticSearchStudy.UnitTest/UseElasticSearchTest.cs @@ -11,23 +11,24 @@ namespace ElasticSearchStudy.UnitTest { public class UseElasticSearchTest { - private ITestOutputHelper _output; + private readonly ITestOutputHelper _output; + private readonly ElasticsearchClient _client; public UseElasticSearchTest(ITestOutputHelper outputHelper) { _output = outputHelper; - } - [Fact] - public void Test() - { var elasticSetting = new ElasticsearchClientSettings(new Uri("https://localhost:9200")) .CertificateFingerprint("F8:B0:ED:80:2C:1C:6C:76:6E:CC:21:3A:CD:91:C3:C8:C7:77:D4:41:F4:71:50:FB:E7:0E:66:0D:71:8C:F3:1A") .Authentication(new BasicAuthentication("elastic", "3JI3QjRtjW8-Tl1q=mVx")); - ElasticsearchClient client = new ElasticsearchClient(elasticSetting); + _client = new ElasticsearchClient(elasticSetting); + } - var pingResponse = client.Ping(); + [Fact] + public void ES_Ping_Test() + { + var pingResponse = _client.Ping(); if (pingResponse.IsSuccess()) { @@ -44,40 +45,14 @@ namespace ElasticSearchStudy.UnitTest [Fact] public void CreateIndex_Test() { - var elasticSetting = new ElasticsearchClientSettings(new Uri("https://localhost:9200")) - .CertificateFingerprint("F8:B0:ED:80:2C:1C:6C:76:6E:CC:21:3A:CD:91:C3:C8:C7:77:D4:41:F4:71:50:FB:E7:0E:66:0D:71:8C:F3:1A") - .Authentication(new BasicAuthentication("elastic", "3JI3QjRtjW8-Tl1q=mVx")) - ; - - ElasticsearchClient client = new ElasticsearchClient(elasticSetting); - - - var tweet = new Tweet - { - Id = 1, - User = "stevejgordon", - PostDate = DateTime.Now, - Message = "Trying out the client, so far so good?" - }; - - var response = client.Index(tweet, "tweet-index"); - - if (response.IsValidResponse) - { - Console.WriteLine($"Index document with ID {response.Id} succeeded."); - } - + _client.Ping(); + var dd = _client.Cluster; + } [Fact] public void GetDoc_Test() { - var elasticSetting = new ElasticsearchClientSettings(new Uri("https://localhost:9200")) - .CertificateFingerprint("F8:B0:ED:80:2C:1C:6C:76:6E:CC:21:3A:CD:91:C3:C8:C7:77:D4:41:F4:71:50:FB:E7:0E:66:0D:71:8C:F3:1A") - .Authentication(new BasicAuthentication("elastic", "3JI3QjRtjW8-Tl1q=mVx")); - - ElasticsearchClient client = new ElasticsearchClient(elasticSetting); - var tweet = new Tweet { Id = 2, @@ -86,7 +61,7 @@ namespace ElasticSearchStudy.UnitTest Message = "Trying out the client, so far so good?" }; - var response = client.Create(tweet, "my-tweet-index",3); + var response = _client.Create(tweet, "my-tweet-index",3); if (response.IsValidResponse) {