项目升级到jdk17记录

es7 升级到 es8

jdk8-》jdk17

springboot-》springboot3

dockerfile 启动参数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#FROM 10.7.20.51/bcs_dev/bcs-java-base:v1.0
#FROM 10.7.20.51/bcs_dev/openjdk:8-jre-alpine
FROM registry.cn-hangzhou.aliyuncs.com/dragonwell/dragonwell:dragonwell-standard-17.0.4.0.4.8_jdk-17.0.4-ga_x86_64_slim
ADD target/test-0.0.1-SNAPSHOT.jar /app/
ADD run.sh /app/
ADD setenv.sh /app/
RUN chmod u+x /app/run.sh
RUN chmod u+x /app/setenv.sh
WORKDIR /app
EXPOSE 8080
ENTRYPOINT ["/bin/sh", "-c", "set -e && /app/run.sh"]

run.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash

# ================================================
# Simple startup script for flat classpath apps
script_dir=`dirname "$0"`

# Discover JAVA_APP_DIR from the script's location.
if [ x"${JAVA_APP_DIR}" = x ] ; then
  JAVA_APP_DIR=`cd "$script_dir"/.. ; pwd`
  export JAVA_APP_DIR
fi

# Custom configuration used by the startup script
if [ -f "${script_dir}/setenv.sh" ] ; then
    source "${script_dir}/setenv.sh"
fi

# Setup main class
main_class=""
if [ x"${JAVA_MAIN_CLASS}" != x ]; then
  main_class="${JAVA_MAIN_CLASS}"
else
  echo "No JAVA_MAIN_CLASS specified"
  exit 1
fi

# Read in classpath
if [ x"${JAVA_CLASSPATH}" != x ]; then
    classpath="${JAVA_CLASSPATH}"
else
    classpath=""
    while read file; do
        classpath="${classpath}:${JAVA_APP_DIR}/lib/$file"
    done < ${JAVA_APP_DIR}/lib/classpath
fi

# Set debug options if required
if [ x"${JAVA_ENABLE_DEBUG}" != x ] && [ "${JAVA_ENABLE_DEBUG}" != "false" ]; then
    java_debug_args="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=${JAVA_DEBUG_PORT:-5005}"
fi

# Start application
echo "Launching application in folder: $JAVA_APP_DIR"
arg_list="${exec_args} java ${java_debug_args} ${JAVA_HEAP_OPTS} ${JAVA_OPTS} -jar  ${classpath} ${main_class} --spring.profiles.active=dev"
if [ x"${JAVA_MAIN_ARGS}" != x ] ; then
    arg_list="${arg_list} ${JAVA_MAIN_ARGS}"
else
    arg_list="${arg_list} $@"
fi
echo "Running ${arg_list}"
cd ${JAVA_APP_DIR}
exec ${arg_list}

setenv.sh

1
2
3
4
5
6
7
#!/bin/bash
JAVA_CLASSPATH=/app/es-web-0.0.1-SNAPSHOT.jar
JAVA_MAIN_CLASS=com.bcs.noc.es.EsBootApplication
JAVA_HEAP_OPTS="-Xms4096M -Xmx4096M -Xmn100M "
JAVA_OPTS="-verbose:gc  \
            -XX:MaxMetaspaceSize=256M  -XX:+DisableExplicitGC -XX:+UseStringDeduplication \
           -XX:+UseG1GC  -XX:+HeapDumpOnOutOfMemoryError   -XX:-UseContainerSupport --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED"

报错“Caused by: java.net.UnknownHostException: 10.233.181.97:9200: invalid IPv6 address”

问题原因是地址里面了含有了端口,写程序注意这种配置。

报错:“org.elasticsearch.ElasticsearchException: java.util.concurrent.ExecutionException: javax.net.ssl.SSLPeerUnverifiedException: Host name ‘10.233.181.97’ does not match the certificate subject provided by the peer (CN=elastic-es-http.logging.es.local, OU=elastic)”

这种报错原因是因为 veryhost 的原因,解决办法:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
        <!--   解决通过SSL链接es验证证书失败的问题     -->
        <dependency>
            <groupId>io.github.hakky54</groupId>
            <artifactId>sslcontext-kickstart</artifactId>
            <version>7.1.0</version>
        </dependency>
        使用方式为:
                SSLFactory sslFactory = SSLFactory.builder().withUnsafeTrustMaterial().withUnsafeHostnameVerifier().build();
  添加校验。
  .setSSLHostnameVerifier(sslFactory.getHostnameVerifier())
                            .setSSLContext(sslFactory.getSslContext())

报错“Parameter 1 of method reactiveElasticsearchTemplate in org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataConfiguration$ReactiveRestClientConfiguration required a single bean, but 2 were found:”

这种问题解决办法就是,删掉一个 bean 即可。

报错“java.lang.IllegalArgumentException: The number of object passed must be even but was [1] at org.elasticsearch.action.index.IndexRequest.source(IndexRequest.java:498)”

原因是缺少 content。json

修改 indexrequest 的 soure 的参数即可

1
                indexQuery.source(JSON.toJSONString(deviceLogPo), XContentType.JSON);

报错:“org.springframework.data.elasticsearch.client.erhlc.ElasticsearchAggregations cannot be cast to class org.elasticsearch.search.aggregations.Aggregations”

这个原因是因为使用了 ElasticsearchOperation 去查询,这就会造成兼容性问题,对于这个问题的解决方式,还是使用 ElastisearchRestTemplate 去查询参数,能够避免这个问题。

如下图所示

image-20230621141210973

报错:

“java.lang.RuntimeException: Unable to parse response body for Response{requestLine=POST /_bulk?timeout=1m HTTP/1.1, host=https://10.233.203.242:9200, response=HTTP/1.1 200 OK}”

这是因为使用 es7,17api 去连接 es8 报错了,解决办法开启兼容模式即可

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Problem Description
When sending a bulk index request to Elasticsearch 8.0.0 with the compatibility headers set for a client version 7, the bulk response is missing the _type property in the index answer. This causes the client failing to parse the response.

Unable to parse response body for Response{requestLine=POST /_bulk?timeout=1m HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}] with root cause

java.lang.NullPointerException: null
	at java.base/java.util.Objects.requireNonNull(Objects.java:208) ~[na:na]
	at org.elasticsearch.action.DocWriteResponse.<init>(DocWriteResponse.java:116) ~[elasticsearch-7.15.2.jar:7.15.2]
	at org.elasticsearch.action.index.IndexResponse.<init>(IndexResponse.java:43) ~[elasticsearch-7.15.2.jar:7.15.2]
	at org.elasticsearch.action.index.IndexResponse.<init>(IndexResponse.java:28) ~[elasticsearch-7.15.2.jar:7.15.2]
	at org.elasticsearch.action.index.IndexResponse$Builder.build(IndexResponse.java:96) ~[elasticsearch-7.15.2.jar:7.15.2]
	at org.elasticsearch.action.index.IndexResponse$Builder.build(IndexResponse.java:93) ~[elasticsearch-7.15.2.jar:7.15.2]
	at org.elasticsearch.action.bulk.BulkItemResponse.fromXContent(BulkItemResponse.java:148) ~[elasticsearch-7.15.2.jar:7.15.2]
	at org.elasticsearch.action.bulk.BulkResponse.fromXContent(BulkResponse.java:177) ~[elasticsearch-7.15.2.jar:7.15.2]
	at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:2011) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
	at org.elasticsearch.client.RestHighLevelClient.lambda$performRequestAndParseEntity$8(RestHighLevelClient.java:1673) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
	at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1749) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1702) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
	at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1672) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
	at org.elasticsearch.client.RestHighLevelClient.bulk(RestHighLevelClient.java:577) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
Steps to Reproduce
send a bulk index request with the RestHighLevelClient 7.15.2 to the cluster

POST http://localhost:9200/_bulk?timeout=1m HTTP/1.1
Accept: application/vnd.elasticsearch+json;compatible-with=7
Content-Type: application/vnd.elasticsearch+json;compatible-with=7
Authorization: Basic *****
Content-Length: 121
Host: localhost:9200
Proxy-Connection: Keep-Alive
User-Agent: elasticsearch-java/7.15.2-SNAPSHOT (Java/17.0.2)
X-Elastic-Client-Meta: es=7.15.2-SNAPSHOT,jv=17,t=7.15.2-SNAPSHOT,hc=4.1.5
{"index":{"_index":"entities","_id":"1"}}
{"id":"1","text":"text-1"}
This will return

HTTP/1.1 200 OK
X-elastic-product: Elasticsearch
content-type: application/vnd.elasticsearch+json;compatible-with=7
content-length: 202
{"took":13,"errors":false,"items":[{"index":{"_index":"entities","_id":"1","_version":5,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":40,"_primary_term":3,"status":200}}]}
When sending the same request to Elasticsearch 7.17.0 the following body is returned>

{"took":456,"errors":false,"items":[{"index":{"_index":"entities","_type":"_doc","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":10,"_primary_term":2,"status":200}}]}
Here the _type field is returned which is missing in the response from 8.0.0 .

Btw, when not using bulk, but a normal index for a single document, then the _type is returned, it 's misisng in the bulk response.

image-20230621152739942

“TerminalServiceImpl.java:157”

解决:java.util.ConcurrentModificationException: null:异常

原因是在遍历 list 的过程中,如果修改了元素,会导致 list 中索引与对应的值不同,因此抛出异常;

报错解释:“ConcurrentModificationException 是基于java 集合中的快速失败(fail-fast)机制产生的,在使用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了增删改,就会抛出该异常。”

如何选择集合的实现类

  1. 先判断存储的类型(一组对象或一组键值对)
  2. 一组对象:Collection 接口 允许重复:List 增删多:LinkedList(底层维护了一个双向链表) 改查多:ArrayList(底层维护 Object 类型的可变数组) 不允许重复:Set 无序:HashSet(底层是 HashMap,维护了一个哈希表,即数组+链表+红黑树) 排序:TreeSet 插入和取顺序一致:LinkedHashSet(维护数组+双向链表)

3.一组键值对:Map 键无序:HashMap(底层是:哈希表 jdk7:数组+链表 jdk8:数组+链表+红黑树) 键排序:TreeMap 键插入和取出顺序一样:LinkedHashMap 读取文件:Properties

报错:“

1
2
3
4
5
6
Exception in thread "main" java.lang.IllegalArgumentException: The number of object passed must be even but was [1]
at  org.elasticsearch.action.index.IndexRequest.source(IndexRequest.java:451)
at elastic.elasti.App.lambda$0(App.java:55)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at elastic.elasti.App.indexExampleData(App.java:53)
at elastic.elasti.App.main(App.java:45)

解决办法,addsource 添加 XContentType.JSON

Licensed under CC BY-NC-SA 4.0
最后更新于 Jan 06, 2025 05:52 UTC
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计
Caret Up