Setting Up the Java Development Environment
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
- Maven
Download Apache Maven 3.0.3 or later from the Maven official website and install it.
- JDK
Download Java Development Kit 1.8.111 or later from the Oracle official website and install it.
After the installation, configure Java environment variables.
- Download IntelliJ IDEA 2018.3.5 or later from the IntelliJ IDEA official website and install it.
Procedure
- 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.
- 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:
- 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.
- 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=
- 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
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot