Vela 除了可以帮我们编译、部署程序,利用它的docker部署功能,也能用来部署其他线上的docker镜像,例如部署RabbitMQ、PostgreSql、Elasticsearch等等,便于集中管理。
创建文件夹并赋予权限:
mkdir /usr/local/es/data mkdir /usr/local/es/plugins chmod 777 /usr/local/es/data chmod 777 /usr/local/es/plugins
在 Vela 中新增部署程序,并指定使用docker容器运行。
9200:9200,9300:9300
/usr/local/es/data:/usr/share/elasticsearch/data,/usr/local/es/plugins:/usr/share/elasticsearch/plugins
discovery.type=single-node,ES_JAVA_OPTS=-Xms512m -Xmx512m
FROM elasticsearch:8.6.0 AS base
保存这些基本信息,直接发布程序就可以了。
我们知道,docker默认是以root用户运行容器的,如果你希望以其他用户运行,并且容器里的程序也支持普通用户,可以在dockerfile中加入下面语句,可以让容器以Vela运行时的用户去运行。
RUN useradd -u %uid% -m job || true USER %uid%
注意:此功能需要升级到最新的Vela才能支持,如何升级请参考官方文档:
http://jms.jacktan.cn?doc/43/3
到config目录下,打开 elasticsearch.yml ,把 xpack.security.enabled 改为 false
进入docker容器 : docker exec -it es /bin/bash
输入命令:
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.6.0/elasticsearch-analysis-ik-8.6.0.zip
然后重启容器生效。
引入nuget包:Elastic.Clients.Elasticsearch
var settings = new ElasticsearchClientSettings(new Uri("http://localhost:9200")); ElasticsearchClient client = new ElasticsearchClient(settings);
for (int i = 0; i < 100; i++) { client.Index<Document>(new Document { Id = 2 + i, Name = "test" + (2 + i), Content = "不断测试我的" + i + "多个文章内容", Time = DateTime.Now.AddMinutes(i + 1) }, "document"); }
doc.Content += " 附加的。"; var response_update = await client.UpdateAsync<Document, Document>("document", 1, u => u.Doc(doc));
var response = await client.DeleteAsync("document", 1);
var response = await client.GetAsync<Document>(id, idx => idx.Index("document")); if (response.IsValidResponse) { var doc = response.Source; }
var response_search = await client.SearchAsync<Document>(s => s .Index("document") .From(0) .Size(10) .Query(q => q .Term(t => t.Name, "test2") ) );
var searchResponse = client.Search<Document>(s => s .Index("document") .From(0) .Size(10) .Query(q => q .Match(m => m .Field(f => f.Content) // 替换为你的实际字段名 .Query("测试内容") ) ) .Sort(sort => sort .Field(f => f.Time, d => d.Order(SortOrder.Desc)) // 按时间降序排序 ) );
var response_search = await client.SearchAsync<Document>(s => s .Index("document") .From(0) .Size(10) .Query(q => q .Bool(b=>b .Should( s => s.Term(t => t.Name, "test2"), s => s.Term(t => t.Name, "test3") ) ) ) );
var createIndexResponse = client.Indices.Create("document", c => c .Mappings(ms => ms .Properties<Document>(props => props .Text(t => t.Content) // 设置为文本类型,进行全文检索 .Keyword(k => k.Name)// 设置为关键字类型,不进行全文检索 .DateRange(n => n.Time) ) ) );