1. 程式人生 > 實用技巧 >Elasticsearch7.9.0 指定JDK11.0.8 啟動

Elasticsearch7.9.0 指定JDK11.0.8 啟動

1. jdk8版本啟動elasticsearch 7報錯解決辦法

報錯資訊:
future versions of Elasticsearch will require Java 11; your Java version from [/opt/jdk1.8.0_211/jre] does not meet this requirement


解決辦法(講解):
由於Elasticsearch依賴於jdk,es和jdk有著對應的依賴關係。
具體可見:
https://www.elastic.co/cn/support/matrix
https://www.elastic.co/guide/en/elasticsearch/reference/7.2/setup.html
這裡是說Elasticsearch該版本內建了JDK,而內建的JDK是當前推薦的JDK版本。當然如果你本地配置了JAVA_HOME那麼ES就是優先使用配置的JDK啟動ES。(言外之意,你不安裝JDK一樣可以啟動,我試了可以的。)
ES推薦使用LTS版本的JDK(這裡只是推薦,JDK8就不支援),如果你使用了一些不支援的JDK版本,ES會拒絕啟動。
那麼哪些版本的JDK支援LTS呢?https://www.oracle.com/technetwork/java/java-se-support-roadmap.html

根據啟動資訊我們看到Elasticsearch7.2推薦使用JDK11,並且從剛才的截圖得知可以下載openjdk 
11.

2.安裝jdk11

jdk下載地址:https://www.oracle.com/java/technologies/javase-jdk11-downloads.html

[root@centos7 ~]# ls
jdk-11.0.8_linux-x64_bin.tar.gz

解壓:
[root@centos7 ~]## tar -xzvf jdk-11.0.8_linux-x64_bin.tar.gz /use/local/

3.不改變原有jdk8環境,只elasticsearch 使用jdk11方法

由於我們日常的程式碼開發都是使用的JDK1.8,所以這裡不會把JAVA_HOME配置成JDK11,我們只需更改Elasticsearch的啟動檔案,使它指向我們下載的JDK11.

修改配置檔案
[root@centos7 bin]# pwd
/home/wx/elasticsearch-7.9.0/bin [root@centos7 bin]# vi elasticsearch 新增以下幾行內容 #配置自己的jdk11 export JAVA_HOME=/usr/local/jdk-11.0.8/ export PATH=$JAVA_HOME/bin:$PATH #新增jdk判斷 if [ -x "$JAVA_HOME/bin/java" ]; then JAVA="/usr/local/jdk-11.0.8//bin/java" else JAVA=`which java` fi

4.elasticsearch檔案全部內容

[root@centos7 bin]# cat elasticsearch
#!/bin/bash

# CONTROLLING STARTUP:
#
# This script relies on a few environment variables to determine startup
# behavior, those variables are:
#
#   ES_PATH_CONF -- Path to config directory
#   ES_JAVA_OPTS -- External Java Opts on top of the defaults set
#
# Optionally, exact memory values can be set using the `ES_JAVA_OPTS`. Example
# values are "512m", and "10g".
#
#   ES_JAVA_OPTS="-Xms8g -Xmx8g" ./bin/elasticsearch

#配置自己的jdk11
export JAVA_HOME=/usr/local/jdk-11.0.8
export PATH=$JAVA_HOME/bin:$PATH

#新增jdk判斷
if [ -x "$JAVA_HOME/bin/java" ]; then
        JAVA="/usr/local/jdk-11.0.8/bin/java"
else
        JAVA=`which java`
fi
source "`dirname "$0"`"/elasticsearch-env
CHECK_KEYSTORE=true
DAEMONIZE=false
for option in "$@"; do
  case "$option" in
    -h|--help|-V|--version)
      CHECK_KEYSTORE=false
      ;;
    -d|--daemonize)
      DAEMONIZE=true
      ;;
  esac
done
if [ -z "$ES_TMPDIR" ]; then
  ES_TMPDIR=`"$JAVA" "$XSHARE" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.TempDirectory`
fi
# get keystore password before setting java options to avoid
# conflicting GC configurations for the keystore tools
unset KEYSTORE_PASSWORD
KEYSTORE_PASSWORD=
if [[ $CHECK_KEYSTORE = true ]] \
    && bin/elasticsearch-keystore has-passwd --silent
then
  if ! read -s -r -p "Elasticsearch keystore password: " KEYSTORE_PASSWORD ; then
    echo "Failed to read keystore password on console" 1>&2
    exit 1
  fi
fi
# The JVM options parser produces the final JVM options to start Elasticsearch.
# It does this by incorporating JVM options in the following way:
#   - first, system JVM options are applied (these are hardcoded options in the
#     parser)
#   - second, JVM options are read from jvm.options and jvm.options.d/*.options
#   - third, JVM options from ES_JAVA_OPTS are applied
#   - fourth, ergonomic JVM options are applied
ES_JAVA_OPTS=`export ES_TMPDIR; "$JAVA" "$XSHARE" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.JvmOptionsParser "$ES_PATH_CONF"`

# manual parsing to find out, if process should be detached
if [[ $DAEMONIZE = false ]]; then
  exec \
    "$JAVA" \
    "$XSHARE" \
    $ES_JAVA_OPTS \
    -Des.path.home="$ES_HOME" \
    -Des.path.conf="$ES_PATH_CONF" \
    -Des.distribution.flavor="$ES_DISTRIBUTION_FLAVOR" \
    -Des.distribution.type="$ES_DISTRIBUTION_TYPE" \
    -Des.bundled_jdk="$ES_BUNDLED_JDK" \
    -cp "$ES_CLASSPATH" \
    org.elasticsearch.bootstrap.Elasticsearch \
    "$@" <<<"$KEYSTORE_PASSWORD"
else
  exec \
    "$JAVA" \
    "$XSHARE" \
    $ES_JAVA_OPTS \
    -Des.path.home="$ES_HOME" \
    -Des.path.conf="$ES_PATH_CONF" \
    -Des.distribution.flavor="$ES_DISTRIBUTION_FLAVOR" \
    -Des.distribution.type="$ES_DISTRIBUTION_TYPE" \
    -Des.bundled_jdk="$ES_BUNDLED_JDK" \
    -cp "$ES_CLASSPATH" \
    org.elasticsearch.bootstrap.Elasticsearch \
    "$@" \
    <<<"$KEYSTORE_PASSWORD" &
  retval=$?
  pid=$!
  [ $retval -eq 0 ] || exit $retval
  if [ ! -z "$ES_STARTUP_SLEEP_TIME" ]; then
    sleep $ES_STARTUP_SLEEP_TIME
  fi
  if ! ps -p $pid > /dev/null ; then
    exit 1
  fi
  exit 0
fi

5.啟動es提醒cms 垃圾收集器在 jdk9 就開始被標註為 @deprecated解決辦法

警告資訊:
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.


解決辦法(詳解):
看下JDK11支援的垃圾回收器:https://docs.oracle.com/en/java/javase/11/gctuning/garbage-first-garbage-collector.html#GUID-CE6F94B6-71AF-45D5-829E-DEADD9BA929D
修改jvm.options
將 : -XX:+UseConcMarkSweepGC
改為:-XX:+UseG1GC 即可

6./home/wx/elasticsearch-7.9.0/config/jvm.options詳細配置

## JVM configuration

################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms2g
-Xmx2g

################################################################
## Expert settings
################################################################
##
## All settings below this section are considered
## expert settings. Don't tamper with them unless
## you understand what you are doing
##
################################################################

## GC configuration
## 原有的註釋了,添加了G1回收器 #
8-13:-XX:+UseConcMarkSweepGC #8-13:-XX:CMSInitiatingOccupancyFraction=75 #8-13:-XX:+UseCMSInitiatingOccupancyOnly -XX:+UseG1GC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly ## G1GC Configuration # NOTE: G1 GC is only supported on JDK version 10 or later # to use G1GC, uncomment the next two lines and update the version on the # following three lines to your version of the JDK # 10-13:-XX:-UseConcMarkSweepGC # 10-13:-XX:-UseCMSInitiatingOccupancyOnly 14-:-XX:+UseG1GC 14-:-XX:G1ReservePercent=25 14-:-XX:InitiatingHeapOccupancyPercent=30 ## JVM temporary directory -Djava.io.tmpdir=${ES_TMPDIR} ## heap dumps # generate a heap dump when an allocation from the Java heap fails # heap dumps are created in the working directory of the JVM -XX:+HeapDumpOnOutOfMemoryError # specify an alternative path for heap dumps; ensure the directory exists and # has sufficient space -XX:HeapDumpPath=data # specify an alternative path for JVM fatal error logs -XX:ErrorFile=logs/hs_err_pid%p.log ## JDK 8 GC logging 8:-XX:+PrintGCDetails 8:-XX:+PrintGCDateStamps 8:-XX:+PrintTenuringDistribution 8:-XX:+PrintGCApplicationStoppedTime 8:-Xloggc:logs/gc.log 8:-XX:+UseGCLogFileRotation 8:-XX:NumberOfGCLogFiles=32 8:-XX:GCLogFileSize=64m # JDK 9+ GC logging 9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m 9-:-Djava.locale.providers=COMPAT

7.普通使用者啟動即可