使用jks证书文件连接kafka问题

其实程序很简单

import java.util.Arrays;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
public class Consumer {
 public static void main(String[] args) {
 Properties props = new Properties();
 props.put("bootstrap.servers",
 "****:9092");
 props.put("group.id", "groupId");
 props.put("enable.auto.commit", "false");
 props.put("auto.offset.reset", "earliest");
 props.put("auto.commit.interval.ms", "1000");
 props.put("session.timeout.ms", "30000");
 props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
 props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
 props.put("security.protocol", "SASL_SSL");
 props.put("ssl.endpoint.identification.algorithm", "");
 props.put("ssl.truststore.location", "D:/Kafka/******.jks");
 props.put("ssl.truststore.password", "WSO2_sp440");
 props.put("sasl.mechanism", "SCRAM-SHA-512");
 props.put("sasl.jaas.config",
 "org.apache.kafka.common.security.scram.ScramLoginModule required username='account_name' password='pwd';");
@SuppressWarnings("resource")
 KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
 consumer.subscribe(Arrays.asList("topic_name"));
 while (true) {
 ConsumerRecords<String, String> records = consumer.poll(100);
 for (ConsumerRecord<String, String> record : records) {
 System.out.printf("partition= %d, offset = %d, key = %s, value = %s\n", record.partition(),
 record.offset(), record.key(), record.value());
 //consumer.commitSync();
 }
 }
 }
}

但是一直报错
java.lang.IllegalArgumentException: JAAS config entry not terminated by semi-colon

查了很多材料都没有问题,

后来才注意到

 props.put("sasl.jaas.config",
 "org.apache.kafka.common.security.scram.ScramLoginModule required username='account_name' password='pwd';");


注意观察最后面的是有一个分号的,这个很重要,如果不加就会上面的报错

 

 

另外,

ssl.truststore.location里面配置的文件,如果是可以通过

ResourceUtils.getFile("classpath:aa.jks").getPath()

代替的,但要在pom文件里设置可以打包到class文件里

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
		<!-- 添加资源 -->
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<!-- src/main/resources下的指定资源放行 -->
				<includes>
					<include>**/*.properties</include>
					<include>**/*.yml</include>
					<include>**/*.xml</include>
					<include>**/*.jks</include>
				</includes>
				<filtering>false</filtering>
			</resource>
		</resources>
	</build>

完结撒花 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值