หน้านี้ยังไม่พร้อมใช้งานในภาษาท้องถิ่นของคุณ เรากำลังพยายามอย่างหนักเพื่อเพิ่มเวอร์ชันภาษาอื่น ๆ เพิ่มเติม ขอบคุณสำหรับการสนับสนุนเสมอมา

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Setting Up the Java Development Environment

Updated on 2023-02-28 GMT+08:00

Based on information in Collecting Connection Information, assume that you have obtained information related to the instance connection and the network environment of the client. This section uses the demo for producing and sending messages as an example to describe the environment configuration of the Kafka client.

Preparations

Procedure

  1. Download the demo package.

    On the ROMA Connect console, choose Message Queue Service > Topic Management. In the upper right corner of the page, choose User Guide > Download Kafka Client Java Demo Package to download the demo.

    The following files are obtained after decompression:

    Table 1 Kafka demo file list

    File Name

    Path

    Description

    MqsConsumer.java

    .\src\main\java\com\mqs\consumer

    API for consuming messages.

    MqsProducer.java

    .\src\main\java\com\mqs\producer

    API for producing messages.

    mqs.sdk.consumer.properties

    .\src\main\resources

    Configuration information of consumption messages.

    mqs.sdk.producer.properties

    .\src\main\resources

    Configuration information of production messages.

    client.truststore.jks

    .\src\main\resources

    SSL certificate, which is used for SASL connection.

    MqsConsumerTest.java

    .\src\test\java\com\mqs\consumer

    Test code of consumption messages.

    MqsProducerTest.java

    .\src\test\java\com\mqs\producer

    Test code of production messages.

    pom.xml

    .\

    Maven configuration file, including the Kafka client reference.

  2. Open IntelliJ IDEA and import the Demo file.

    The demo is a Java project constructed by the Maven. Therefore, you need to configure the JDK environment and the Maven plug-in of the IDEA.
    Figure 1 Choose Import Project.
    Figure 2 Choose Maven.
    Figure 3 Selecting the Java environment

    You can select other options or retain the default settings. Then, click Finish.

    The demo project after the import is as follows:

  3. Configure the path to the Maven.

    Choose File > Settings, find the Maven home directory information item, and select the correct Maven path for Maven home directory and select the settings.xml file required by the Maven.

  4. Modify client configuration information.

    Take a production message as an example. The following information must be configured, among which the information in bold must be modified.

    #The following information in bold is specific to different MQSs and must be modified. Other parameters of the client can also be added.
    #The topic name is in the specific production and consumption code.
    #######################
    #You can obtain the broker information from the console.
    #For example, bootstrap.servers=192.168.0.196:9095,192.168.0.196:9096,192.168.0.196:9094.
    bootstrap.servers=ip1:port1,ip2:port2,ip3:port3
    #Send acknowledgment parameters.
    acks=all
    #Sequence mode of the key.
    key.serializer=org.apache.kafka.common.serialization.StringSerializer
    #Sequence mode of the value.
    value.serializer=org.apache.kafka.common.serialization.StringSerializer
    #Total bytes of memory the producer can use to buffer records waiting to be sent to the server. 
    buffer.memory=33554432
    #Number of retries.
    retries=0
    #######################
    #If SASL authentication is not used, comment out the following parameters:
    #######################
    #Set the jaas username and password on the console.
    sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
        username="username" \
        password="password";
    #SASL authentication mode.
    sasl.mechanism=PLAIN
    #Encryption protocol. Currently, the SASL_SSL protocol is supported.
    security.protocol=SASL_SSL
    #Location of the SSL truststore file. The certificate file is in the \src\main\resources directory of the demo package.
    ssl.truststore.location=E:\\temp\\client.truststore.jks
    #Password of the SSL truststore file.
    ssl.truststore.password=dms@kafka
    ssl.endpoint.identification.algorithm=

  5. Open the Terminal window of the IDEA, and then run the mvn test command to experience the demo.

    By default, the Terminal window is in the lower left corner of the IDEA tool.

    Figure 4 Position of the Terminal window of the IDEA

    The following information is displayed for production messages:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.mqs.producer.MqsProducerTest
    produce msg:The msg is 0
    produce msg:The msg is 1
    produce msg:The msg is 2
    produce msg:The msg is 3
    produce msg:The msg is 4
    produce msg:The msg is 5
    produce msg:The msg is 6
    produce msg:The msg is 7
    produce msg:The msg is 8
    produce msg:The msg is 9
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 138.877 sec
    

    The following information is displayed for consumption messages:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.mqs.consumer.MqsConsumerTest
    the numbers of topic:0
    the numbers of topic:0
    the numbers of topic:6
    ConsumerRecord(topic = topic-0, partition = 2, offset = 0, CreateTime = 1557059377179, serialized key size = -1, serialized value size = 12, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = The msg is 2)
    ConsumerRecord(topic = topic-0, partition = 2, offset = 1, CreateTime = 1557059377195, serialized key size = -1, serialized value size = 12, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = The msg is 5)
    

เราใช้คุกกี้เพื่อปรับปรุงไซต์และประสบการณ์การใช้ของคุณ การเรียกดูเว็บไซต์ของเราต่อแสดงว่าคุณยอมรับนโยบายคุกกี้ของเรา เรียนรู้เพิ่มเติม

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback