spring cloud stream kafka 配置 SSL,pod 中读不到 jks 文件:
异常栈
# file / classpath 方式都不可用
# file:/xxx/kafka.client.truststore.jks
# classpath:static/kafka.client.truststore.jks
"message":"Modification time of key store could not be obtained: classpath:static/kafka.client.truststore.jks"
"stack_trace":"java.nio.file.NoSuchFileException: classpath:static/kafka.client.truststore.jks...."
解决方案
- 通过 Dockerfile 将文件 copy 到指定目录
FROM openjdk:8-jdk-alpine
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 将 jks 文件 copy 到 pod 指定位置
RUN mkdir /keys
COPY kafka.client.truststore.jks /keys/kafka.client.truststore.jks
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
- application.yaml 配置
spring:
cloud:
stream:
default-binder: ssl-kafka
bindings:
pubMapUpChannel:
binder: ssl-kafka
destination: dest-topic
binders:
ssl-kafka:
type: kafka
environment:
spring:
cloud:
stream:
kafka:
binder:
brokers: xxxx:9093
configuration:
security:
protocol: SASL_SSL
sasl:
mechanism: PLAIN
jaas:
config: org.apache.kafka.common.security.plain.PlainLoginModule required username="username" password="password";
ssl:
truststore:
# 服务器上需要通过dockerfile复制到指定位置,再通过绝对路径读
location: /keys/kafka.client.truststore.jks
password: password
endpoint:
identification:
algorithm: ''