Springboot 接入 带有SASL/PLAIN 的kafka

2019-03-15 02:24 

yml 配置文件添加

 

  kafka:
    template:
   
    producer:
#kafka ip 端口
      bootstrap-servers: xx.xx.65.8:8123
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      properties: 
        sasl.mechanism: PLAIN
        security.protocol: SASL_PLAINTEXT      
    consumer: 
      bootstrap-servers: xx.xx.65.8:8123
      group-id: group-1
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      properties: 
        sasl.mechanism: PLAIN
        security.protocol: SASL_PLAINTEXT  
	 //初始化系统属性
    static {
    	System.setProperty("java.security.auth.login.config", "D:/kafka_client_jaas.conf");
    }

POM 文件配置

	


       <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>


        <dependency>
		    <groupId>org.springframework.kafka</groupId>
		    <artifactId>spring-kafka</artifactId>
		    <version>2.1.2.RELEASE</version>
		</dependency>

	<dependencyManagement>
	    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
	</dependencyManagement>

发送消息

/**
  * 使用kafka模板方法.
 * */
    @Autowired
    private KafkaTemplate<String, Object> template;

D:/kafka_client_jaas.conf

KafkaClient {
        org.apache.kafka.common.security.plain.PlainLoginModule required
        username="mooc"
        password="moocpswd";
};

 

发表评论

您必须 登录 才能发表留言!