如何在android应用程序中创建kafka producer?

nukf8bse  于 2021-06-05  发布在  Kafka
关注(0)|答案(2)|浏览(479)

我对apachekafka还很陌生,我正尝试在androidstudio上使用它,以便使用一个简单的kafka消费者和生产者示例中的代码向位于我的pc上的服务器生成数据
gradle代码:

apply plugin: 'com.android.application'

android {
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }
    compileSdkVersion 28
    buildToolsVersion "28.0.3"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 14
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.support:multidex:1.0.1'
    implementation 'io.socket:socket.io-client:0.2.1'
    implementation 'org.apache.kafka:kafka-clients:0.10.0.0'
    implementation 'org.apache.kafka:kafka-streams:0.10.0.0'

}

这是我的主要活动:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.os.StrictMode;
import android.widget.Toast;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.LongSerializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, LongSerializer.class.getName());
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
        for (int i = 0; i < 1000; i++) {
            ProducerRecord<String, String> data;
            if (i % 2 == 0) {
                data = new ProducerRecord<String, String>("even", 0, Integer.toString(i), String.format("%d is even", i));
            } else {
                data = new ProducerRecord<String, String>("odd", 0, Integer.toString(i), String.format("%d is odd", i));
            }
            producer.send(data);
            try {
                Thread.sleep(1L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        producer.close();

        }

    }

错误如下:
基本上,这些行清楚地显示了错误:

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
        at org.apache.kafka.common.utils.AppInfoParser.unregisterAppInfo(AppInfoParser.java:65)
        at org.apache.kafka.clients.producer.KafkaProducer.close(KafkaProducer.java:699)
        at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:333)
        at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:188)

它一直在瞬间崩溃,无法识别问题。我试图将multidex设置为true或false,但出现了相同的错误。我还尝试使用maven依赖项,但错误仍然是一样的。
我错过了什么?你能帮忙吗

slmsl1lt

slmsl1lt1#

我发现我们不能用安卓系统创造Kafka。最好为kafka创建一个公开rest端点的应用程序,稍后可以在android应用程序中调用。你可以在任何语言或框架中创建它,我在springboot中已经做过了,它工作得很好。
https://github.com/vpkeerthan/kafka/tree/master/kafkarestservice
为kafka服务公开的使用restful端点的android应用程序https://github.com/vpkeerthan/android-tutorial/tree/master/kafka-rest-android

jc3wubiy

jc3wubiy2#

你不会的。这是不安全的。你可以向一个webservice发送一个请求,它会向kafka发送数据。其他任何东西都需要你的Kafka服务器公开应用程序中的身份验证信息,然后基本上是开放给任何人。

相关问题