0%

Java Client

apache kafka-client

官方文档:https://kafka.apache.org/11/documentation.html#producerapi

官方java doc: https://kafka.apache.org/11/javadoc/overview-summary.html

Kafka Tutorial: Writing a Kafka Producer in Java

Kafka Producer配置解读

Example: Simple Producer Demo

pom.xml

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.1.1</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class KafkaProducerExample {
// ...
private static Producer<Long, String> createProducer() {
Properties props = new Properties();
// ProducerConfig: https://kafka.apache.org/11/javadoc/index.html?org/apache/kafka/clients/producer/ProducerConfig.html
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
BOOTSTRAP_SERVERS);
props.put(ProducerConfig.CLIENT_ID_CONFIG, "KafkaExampleProducer");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
LongSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
StringSerializer.class.getName());

// KafkaProducer: https://kafka.apache.org/11/javadoc/org/apache/kafka/clients/producer/KafkaProducer.html
return new KafkaProducer<>(props);
}

static void runProducer(final int sendMessageCount) throws Exception {
final Producer<Long, String> producer = createProducer();
long time = System.currentTimeMillis();

try {
for (long index = time; index < time + sendMessageCount; index++) {
final ProducerRecord<Long, String> record =
new ProducerRecord<>(TOPIC, index,
"Hello Mom " + index);

RecordMetadata metadata = producer.send(record).get();

long elapsedTime = System.currentTimeMillis() - time;
System.out.printf("sent record(key=%s value=%s) " +
"meta(partition=%d, offset=%d) time=%d\n",
record.key(), record.value(), metadata.partition(),
metadata.offset(), elapsedTime);

}
} finally {
producer.flush();
producer.close();
}
}

Shell

Logic

for

1
2
3
4
5
6
7
8
9
10
11
for file in ./*
do
if test -f $file
then
echo $file is file
fi
if test -d $file
then
echo $file is dictionary
fi
done

Network

DNS

DNS刷新缓存

1
2
3
service nscd restart
service dnsmasq restart
rndc restart

查看某个record 何时才能失效,假设你的默认dns server 不是authoritative server

1
dig +nocmd +noall +answer www.google.com

查看某个record 从authoritative server 请求一个record 时被设置的ttl

1
dig @ns1.google.com +nocmd www.google.com +noall +answer

Proxy

/etc/profile

1
2
export http_proxy=xxx
export https_proxy=xxx

tc

See linux 下使用 tc 模拟网络延迟和丢包

1
2
3
4
5
# 将 eth0 网卡的传输设置为延迟 100 毫秒发送
tc qdisc add dev eth0 root netem delay 100ms
# 删除上面配置
tc qdisc del dev eth0 root netem delay 100ms

Common

Date & Time

1
2
3
# date +%Y%m%d
20190613

Introduction

The new major version of the programmer-friendly testing framework for Java

官网:https://junit.org/junit5/

User Guide: https://junit.org/junit5/docs/current/user-guide/#overview

Getting Started

https://github.com/junit-team/junit5-samples/tree/r5.4.2/junit5-jupiter-starter-maven

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<!-- omit -->

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

Cluster

1
2
kubectl cluster-info
kubectl get nodes