mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
当MQ配置为Kafka时,支持配置Kerberos认证 (#1895)
This commit is contained in:
@@ -31,6 +31,10 @@ public class MQProperties {
|
||||
private String accessChannel = null;
|
||||
private String customizedTraceTopic = null;
|
||||
private String namespace = "";
|
||||
private boolean kerberosEnable = false; //kafka集群是否启动Kerberos认证
|
||||
private String kerberosKrb5FilePath = ""; //启动Kerberos认证时配置为krb5.conf文件的路径
|
||||
private String kerberosJaasFilePath = ""; //启动Kerberos认证时配置为jaas.conf文件的路径
|
||||
|
||||
public static class CanalDestination {
|
||||
|
||||
private String canalDestination;
|
||||
@@ -257,6 +261,30 @@ public class MQProperties {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
public boolean isKerberosEnable() {
|
||||
return kerberosEnable;
|
||||
}
|
||||
|
||||
public void setKerberosEnable(boolean kerberosEnable) {
|
||||
this.kerberosEnable = kerberosEnable;
|
||||
}
|
||||
|
||||
public String getKerberosKrb5FilePath() {
|
||||
return kerberosKrb5FilePath;
|
||||
}
|
||||
|
||||
public void setKerberosKrb5FilePath(String kerberosKrb5FilePath) {
|
||||
this.kerberosKrb5FilePath = kerberosKrb5FilePath;
|
||||
}
|
||||
|
||||
public String getKerberosJaasFilePath() {
|
||||
return kerberosJaasFilePath;
|
||||
}
|
||||
|
||||
public void setKerberosJaasFilePath(String kerberosJaasFilePath) {
|
||||
this.kerberosJaasFilePath = kerberosJaasFilePath;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return "MQProperties{" +
|
||||
"servers='" + servers + '\'' +
|
||||
@@ -280,6 +308,9 @@ public class MQProperties {
|
||||
", accessChannel='" + accessChannel + '\'' +
|
||||
", customizedTraceTopic='" + customizedTraceTopic + '\'' +
|
||||
", namespace='" + namespace + '\'' +
|
||||
", kerberosEnable='" + kerberosEnable + '\'' +
|
||||
", kerberosKrb5FilePath='" + kerberosKrb5FilePath + '\'' +
|
||||
", kerberosJaasFilePath='" + kerberosJaasFilePath + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,5 @@
|
||||
package com.alibaba.otter.canal.kafka;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||
import org.apache.kafka.clients.producer.Producer;
|
||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||
import org.apache.kafka.common.serialization.StringSerializer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.alibaba.otter.canal.common.MQMessageUtils;
|
||||
@@ -21,6 +7,20 @@ import com.alibaba.otter.canal.common.MQProperties;
|
||||
import com.alibaba.otter.canal.protocol.FlatMessage;
|
||||
import com.alibaba.otter.canal.protocol.Message;
|
||||
import com.alibaba.otter.canal.spi.CanalMQProducer;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||
import org.apache.kafka.clients.producer.Producer;
|
||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||
import org.apache.kafka.common.serialization.StringSerializer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* kafka producer 主操作类
|
||||
@@ -59,6 +59,26 @@ public class CanalKafkaProducer implements CanalMQProducer {
|
||||
} else {
|
||||
properties.put("retries", kafkaProperties.getRetries());
|
||||
}
|
||||
|
||||
if (kafkaProperties.isKerberosEnable()){
|
||||
File krb5File = new File(kafkaProperties.getKerberosKrb5FilePath());
|
||||
File jaasFile = new File(kafkaProperties.getKerberosJaasFilePath());
|
||||
if(krb5File.exists() && jaasFile.exists()){
|
||||
//配置kerberos认证,需要使用绝对路径
|
||||
System.setProperty("java.security.krb5.conf",
|
||||
krb5File.getAbsolutePath());
|
||||
System.setProperty("java.security.auth.login.config",
|
||||
jaasFile.getAbsolutePath());
|
||||
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
|
||||
properties.put("security.protocol", "SASL_PLAINTEXT");
|
||||
properties.put("sasl.kerberos.service.name", "kafka");
|
||||
}else{
|
||||
String errorMsg = "ERROR # The kafka kerberos configuration file does not exist! please check it";
|
||||
logger.error(errorMsg);
|
||||
throw new RuntimeException(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
if (!kafkaProperties.getFlatMessage()) {
|
||||
properties.put("value.serializer", MessageSerializer.class.getName());
|
||||
producer = new KafkaProducer<String, Message>(properties);
|
||||
|
||||
Reference in New Issue
Block a user