Springboot集成使用阿里云kafka详细步骤(4)
- UID
- 1066743
|
Springboot集成使用阿里云kafka详细步骤(4)
可能遇到的问题–org.apache.kafka.common.errors.UnsupportedSaslMechanismException: Client SASL mechanism ‘ONS’ not enabled in the server, enabled mechanisms are [PLAIN]
原因
代码中使用的配置与conf中设置的安全机制不一致。
解决方式
PLAIN模式
代码中
props.put(SaslConfigs.SASL_MECHANISM, "PLAIN");
对应conf内容
KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="xxxxxxxxxxxxxxxxxxxxx"
password="xxxxxxxxxxxxxxxxxxxxx";
};
ONS模式
代码中
props.put(SaslConfigs.SASL_MECHANISM, "ONS");
1
对应conf内容
KafkaClient {
com.aliyun.openservices.ons.sasl.client.OnsLoginModule required
AccessKey="XXX"
SecretKey="XXX";
};
可能遇到的问题–nested exception is java.lang.NullPointerException
使用代码为
public KafkaAliyunConfiguration() {
if (StringUtils.isEmpty(confLocation)) {
URL authLocation = KafkaAliyunConfiguration.class.getClassLoader().getResource("kafka_client_jaas.conf");
if (System.getProperty("java.security.auth.login.config") == null) {
System.setProperty("java.security.auth.login.config", authLocation.toExternalForm());
}
System.out.println("kafka配置为:"+authLocation.toExternalForm());
} else {
System.out.println("kafka配置为:"+confLocation);
System.setProperty("java.security.auth.login.config", confLocation);
}
}
13
在进行KafkaAliyunConfiguration初始化时报错空指针。
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'qualityServiceImpl': Unsatisfied dependency expressed through field 'kafkaService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'kafkaServiceImpl': Unsatisfied dependency expressed through field 'kafkaTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'kafkaAliyunConfiguration' defined in URL [jar:file:/jar/report-api-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes!/com/biologic/util/KafkaAliyunConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.biologic.util.KafkaAliyunConfiguration$$EnhancerBySpringCGLIB$$88e40778]: Constructor threw exception; nested exception is java.lang.NullPointerException
1
原因 初始化KafkaAliyunConfiguration时,变量加载的顺序问题导致无法识别到变量。
解决方式
方式一 初始化时不使用注入的变量
如下:
public KafkaAliyunConfiguration() {
//如果用-D 或者其它方式设置过,这里不再设置
if (null == System.getProperty("java.security.auth.login.config")) {
//请注意将 XXX 修改为自己的路径
//这个路径必须是一个文件系统可读的路径,不能被打包到 jar 中
System.setProperty("java.security.auth.login.config", "/jar/kafka_client_jaas.conf");
}
System.out.println("环境变量中已有config文件,kafka配置为:"+System.getProperty("java.security.auth.login.config"));
} |
|
|
|
|
|