搜索引擎(Solr配置管理詳解)

學習目標

掌握在生產環境中安裝配置solr
掌握solr的安全配置
掌握solr的集合管理html

應用於生產

在linux系統上安裝solrCloudjava

1.依賴:
    JRE      solr7.3 須要 java1.8
    獨立的zookeeper服務 ,zookeeper安裝請參考:
    http://zookeeper.apache.org/doc/current/zookeeperStarted.htmlnode

2.上傳solr安裝包linux

3.從安裝包中解出安裝腳本apache

tar xzf solr-7.3.0.tgz solr-7.3.0/bin/install_solr_service.sh --strip-components=2

安裝腳本可用於:CentOS, Debian, Red Hat, SUSE and Ubuntu Linux distributionsjson

4.安裝腳本參數說明:api

./install_solr_service.sh -help

-i  指定軟件安裝目錄。默認 /opt
-d 指定數據目錄(solr主目錄):內核存儲目錄 。默認 /var/solr
-u  指定要建立的擁有solr的用戶名,出於安全考慮,不該以root來運行。默認 solr
-s  指定系統服務名。默認 solr
-p  指定端口。默認 8983

5.目錄規劃安全

6.以root身份運行安裝腳本進行安裝服務器

./install_solr_service.sh solr-7.3.0.tgz

等同:app

./install_solr_service.sh solr-7.3.0.tgz -i /opt -d /var/solr -u solr -s solr -p 8983

配置solr服務實例

1.認識solr服務的配置文件

問:如何啓動一個solr服務實例?

1,系統服務腳本:    /etc/init.d/solr   請查看該腳本內容,看系統啓動時是如何啓動solr服務實例的。 可看到使用了以下三個變量:

2.環境參數配置文件(官方叫法:include file)。它將覆蓋 bin/solr啓停控制腳本中的配置參數。咱們經過該文件來配置修改solr服務實例的運行配置。

請查看 /etc/default/solr.in.sh  ,看咱們能夠在該文件中進行哪些配置。

在 /etc/default/solr.in.sh  中可看到它配置了以下參數:

2.在/etc/default/solr.in.sh中配置以下參數

1.調整solr實例的內存,默認solr使用512M的堆內存,生產環境下確定須要調大。

SOLR_JAVA_MEM="-Xms10g -Xmx10g"

2.配置ZK_HOST,讓服務實例以solrCloud模式運行

ZK_HOST=zk1,zk2:2182,zk3:2188

3.設置chroot。Solr默認使用zookeeper的/爲其根目錄,在多應用共用zookeeper的狀況下,爲避免衝突,應該在單獨的子節點(如/solr)下來存儲solr的配置信息。這個節點需事先建立好,而後配置以下:

ZK_HOST=zk1,zk2:2182,zk3:2188/solr
bin/solr zk mkroot /solr -z <ZK_node>:<ZK_PORT>          建立znode的命令

4.設置SOLR服務的主機名,在solrCloud模式下強烈建議配置。不設置則使用的是ip。

SOLR_HOST=solr1.example.com

5.爲solrconfig.xml中用到的動態參數提供值

<autoSoftCommit>
  <maxTime>${solr.autoSoftCommit.maxTime:-1}</maxTime>
</autoSoftCommit>

啓動時給入參數值

bin/solr start -Dsolr.autoSoftCommit.maxTime=10000

在solr.in.sh中配置參數值

SOLR_OPTS="$SOLR_OPTS -Dsolr.autoSoftCommit.maxTime=10000"

 

在一臺機器上運行多個solr服務

生產環境下,不要在一臺機器上運行多個solr服務。若是確要在一臺機器上運行多個solr服務實例,能夠啓動腳本加 –s 選項指定不一樣的solr主目錄。還能夠簡單直接地安裝多個solr系統服務(不一樣的服務名、端口):

./install_solr_service.sh solr-7.3.0.tgz -s solr2 -p 8984

請看solr2裝在什麼位置了。請查看目錄: /etc/init.d   /etc/default

 

瞭解solr主目錄配置文件 solr.xml

solr.xml : solr服務實例配置文件

solr.xml中配置服務實例的參數及內核通用配置參數。

在獨立服務器模式下,主目錄下必需有solr.xml。而在solrCloud模式下,若是zookeeper中放置有solr.xml ,則會從zookeeper中複製到主目錄。

在早期solr版本中,須要在solr.xml中經過<core>元素來配置內核,服務實例才能發現內核。如今則不須要了,服務實例經過查詢主目錄下的子目錄中是否包含core.properties來發現內核。

solr.xml 中的可配置項

The <solr> Element
    The <solrcloud> Element
    The <logging> Element
    The <shardHandlerFactory> Element
    The <metrics> Element

Solr是配置良好的,咱們基本不須要去作什麼配置。但須要了解都有什麼配置參數,能夠來改變solr的運行行爲。

solr.xml配置參數說明

集合管理

回顧

集合是什麼?
分片是什麼?
備份是什麼?
內核是什麼?
集合、分片、備份、內核是什麼關係?每一個備份對應一個物理內核

如何經過bin/solr腳本建立一個集合?

bin/solr create –c collection01 –d _default –shards 2 –replicationFactor 2 –p 8983
bin/solr create –c collection02 –d sample_techproducts_configs –shards 2 –replicationFactor 2 –p 8983

如何經過 bin/solr腳本刪除一個集合?

bin/solr delete –c collectonname –p port

思考

集合的分片數能夠修改嗎?集合的備份數能夠修改嗎?

集合管理API

詳細請瀏覽:https://lucene.apache.org/solr/guide/7_3/collections-api.html

建立集合

http://localhost:8983/solr/admin/collections?action=CREATE&name=newCollection&numShards=2&replicationFactor=2&wt=xml

經常使用參數說明:

name:指定集合名
numShards:初始分片數
replicationFactor:分片備份因子
collection.configName:參照的配置集名稱(zookeeper的/configs下的子)
maxShardsPerNode:每一個節點上容許的最大分片數,默認1
autoAddReplicas:true/false,在共享文件系統中可自動增長備份,默認false

拆分分片

/admin/collections?action=SPLITSHARD&collection=name&shard=shardID

刪除備份

http://localhost:8983/solr/admin/collections?action=DELETEREPLICA&collection=test2&shard=shard2&replica=core_node3&wt=xml

添加備份

http://localhost:8983/solr/admin/collections?action=ADDREPLICA&collection=test2&shard=shard2&node=192.167.1.2:8983_solr&wt=xml

內核的主要配置文件

core.properties

內核屬性文件,是slor服務實例在solr主目錄中自動發現內核的標識文件。發現的規則:遍歷主目錄下的子目錄樹來查找core.properties,直到在一支上找到core.properties文件。

它是一個屬性文件,能在裏面定義什麼屬性呢?

core.properties 中能夠配置的屬性

最簡單的core.properties

name=my_core_name

 

安全配置

Solr安全涉及的方面

Authentication or authorization  認證鑑權
Enabling SSL        使用 https 協議
If using SolrCloud, ZooKeeper Access Control     zookeeper的訪問控制

Solr提供了多種認證鑑權插件的實現:

Kerberos Authentication Plugin
Basic Authentication Plugin
Rule-Based Authorization Plugin
Custom authentication or authorization plugin

以 Basi Authentication 爲例來了解如何應用。

如何啓用安全?

Solr要求以在solr主目錄下加入security.json 文件來啓用安全控制,在solrCloud模式下,如在zookeeper中加入/security.json 。在security.json中定義認證、鑑權安全插件,如:

{
"authentication":{ 
   "blockUnknown": true, 
   "class":"solr.BasicAuthPlugin",
   "credentials":{"solr":"IV0EHq1OnNrj6gvRCwvFwTrZ1+z1oBbnQdiVC3otuq0= Ndd7LKvVBAaZIF0QAVi1ekCfAJXr1GGfLtRUXhgrF8c="} 
},
"authorization":{
   "class":"solr.RuleBasedAuthorizationPlugin",
   "permissions":[{"name":"security-edit",
      "role":"admin"}], 
   "user-role":{"solr":"admin"} 
}}

如何往zookeeper中加入security.json?

方式一:直接經過zk的客戶端來添加節點
方式二:用 solr提供的操做腳本

>server/scripts/cloud-scripts/zkcli.sh -zkhost localhost:2181 -cmd put /security.json '{"authentication": {"class": "org.apache.solr.security.KerberosPlugin"}}'

方式三:用 bin/solr zk 來操做

bin/solr zk cp file:path_to_local_security.json zk:/security.json -z localhost:9983

Basic Authentication Plugin

基於用戶名密碼的認證插件,官網參考:

https://lucene.apache.org/solr/guide/7_3/basic-authentication-plugin.htm

Rule-Based Authorization Plugin

基於用戶、角色、權限規則的鑑權插件,官網參考:

https://lucene.apache.org/solr/guide/7_3/rule-based-authorization-plugin.html

用戶管理

API

v1: http://localhost:8983/solr/admin/authentication
v2: http://localhost:8983/api/cluster/security/authentication

新增或修改密碼

curl --user solr:SolrRocks http://localhost:8983/api/cluster/security/authentication -H 'Content-type:application/json' -d '{"set-user": {"tom":"tom", "harry":"harry"}}'

刪除用戶

curl --user solr:SolrRocks http://localhost:8983/api/cluster/security/authentication -H 'Content-type:application/json' -d '{"delete-user": ["tom", "harry"]}'

在SolrJ中使用基本認證

在每一個請求中都須要帶上用戶名密碼

SolrRequest req ;//create a new request object
req.setBasicAuthCredentials(userName, password);
solrClient.request(req);

查詢示例

QueryRequest req = new QueryRequest(new SolrQuery("*:*"));
req.setBasicAuthCredentials(userName, password);
QueryResponse rsp = req.process(solrClient);

使用 bin/solr 腳本也須要安全認證了,在solr.in,sh環境參數文件中加入

SOLR_AUTH_TYPE="basic"
SOLR_AUTHENTICATION_OPTS="-Dbasicauth=solr:SolrRocks"

啓用SSL

請參考:

https://lucene.apache.org/solr/guide/7_3/enabling-ssl.html

啓用zookeeper ACL 

請參考:

https://lucene.apache.org/solr/guide/7_3/zookeeper-access-control.html
相關文章
相關標籤/搜索