Spring Boot + Spring Kafka配置公网接入阿里云Kafka
场景说明
今天接到的一个任务是做公网接入阿里云的kafka。不推荐通过公网访问。
由于通过公网问,需要配置为客户端接入配置SASL_SSL的鉴权,设置证书,协议,访问模式的账号密码等参数。
自己的应用采用的是spring boot + spring kafka来接入kafka。
因此,在配置上需要做一些适配和调整。
官方文档
https://help.aliyun.com/knowledge_detail/99958.html
原生的配置,主要有几个点:
- 在控制台下载ssl证书 ---- kafka.client.truststore.jks,放在机器的某个路径上,不要跟着应用打包
- 构建 kafka_client_jaas.conf,它包含着登录模式,登录用账号密码等的信息,账号密码信息在控制台上能找到。将应用启动参数java.security.auth.login.config设置为kafka_client_jaas.conf的绝对路径,可以通过代码或者启动参数设置。
注意 : spring boot配置中可以不需要的这个kafka_client_jaas.conf,它通过读取application.yaml中的配置生成了一个,设置到应用中,具体见下。
- 根证书是有密码,阿里云kafka证书统一的解析密码为KafkaOnsClient。
- 接入协议为SASL_SSL, 鉴权方式为PLAIN。
配置适配
由于采用spring boot + spring kafka来接入kafka,配置的时候需要做一下适配调整。
spring boot读取kafka属性的类为org.springframework.boot.autoconfigure.kafka.KafkaProperties,其中也包含有SSL, JAAS的配置项,这些是我们需要配置的。
当然也有一些额外的配置,需要通过通用的properties给设置进去,让spring读取到,转换成kafka-client的配置。
SSL
主要是设置证书文件的绝对路径和证书解密密码。
这里是分别都为producer, consumer设置ssl的参数
JAAS
主要是配置属性,然后构建一个类似kafka_client_jaas.conf的文件,设置到应用中,最终的实质也仍然是将生成的文件路径设置给java.security.auth.login.config这个应用变量。
spring: kafka: jaas: enabled: true loginModule: org.apache.kafka.common.security.plain.PlainLoginModule controlFlag: REQUIRED options: username: aliyun-kafka-username password: aliyun-kafka-password ...
其中各个属性的值可以对照阿里云官方接入文档中kafka_client_jaas.conf文件内容进行配置
额外参数
通过producer, consumer的properties属性可以添加额外自定义的属性,这里主要设置了
- sasl.mechanism: PLAIN
- security.protocol: SASL_SSL
配置例子
spring: kafka: bootstrap-servers: kafka-server1, kafka-server2, kafka-server3 jaas: enabled: true loginModule: org.apache.kafka.common.security.plain.PlainLoginModule options: username: aliyun-kafka-username password: aliyun-kafka-password consumer: ssl: truststorePassword: KafkaOnsClient truststoreLocation: file:/etc/kafka/kafka.client.truststore.jks properties: sasl.mechanism: PLAIN security.protocol: SASL_SSL group-id: consumer-group max-poll-records: 2 producer: ssl: truststorePassword: KafkaOnsClient truststoreLocation: file:/etc/kafka/kafka.client.truststore.jks retries: 3 acks: 1 compression-type: lz4 buffer-memory: 33554432 batch-size: 51200 properties: send.buffer.bytes: 262144 sasl.mechanism: PLAIN security.protocol: SASL_SSL
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
一个快速找到Spring框架是在哪里找到XML配置文件并解析Beans定义的小技巧
We can define bean configuration in xml and then can get instantiated bean instance with help of all kinds of containers for example ClassPathXmlApplicationContext as displayed below: The content of Beans.xml: <?xml version="1.0" encoding="UTF-8"?> <!-- http://stackoverflow.com/questions/18802982/no-declaration-can-be-found-for-element-contextannotation-config --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns...
- 下一篇
关于异步
一、概述 js的执行环境是‘单线程’的,异步操作是至关重要的。异步任务就是任务需要分阶段完成,各阶段可以插入其他任务,否则就是同步任务。 二、实现 js处理异步任务主要分了3个阶段: 回调函数:异步任务拆分成多个阶段的代码,将这些代码用函数包裹以便在满足条件的时候继续异步任务,该函数被称为回调函数。这样处理主要缺点是:异步任务的逻辑被拆分了,异步任务嵌套异步任务后会形成‘回调地狱’,代码耦合程度太高。 Promise和Generator:Promise是回调函数的语法糖,主要解决‘回调地狱’的问题。Generator的特性类似‘协程’,目前的实现只能说是‘半协程’,只有Generator的实例有执行权。Generator通过yield来将异步任务分阶段,每一个阶段结束都会保留当前的执行上下文,调用next方法执行下一个阶段。缺点就是需要手动执行next,错误处理有些破坏结构。 async:async函数是Generator的语法糖,结合Promise将Generator自动化并返回Promise实例,通过await来分阶段,缺点是会阻塞,错误处理没有优化。 三、总结 现在Generat...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS6,CentOS7官方镜像安装Oracle11G
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- CentOS8编译安装MySQL8.0.19
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Windows10,CentOS7,CentOS8安装Nodejs环境
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7