我最近在网站上遇到了这段代码
const List = ({ items }) => (
<ul className="list">
{items.map(item => <ListItem item={item} />)}
</ul>
);
Run Code Online (Sandbox Code Playgroud)
为什么他们用花括号包裹物品,它是道具
我编写了一个非常简单的 TCP 服务器,它读取连接并以 HELLO WORLD 进行响应。
import (
"fmt"
"log"
"net"
)
func handleRequest(conn net.Conn) {
buff := make([]byte, 10)
_, err := conn.Read(buff)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(buff))
conn.Write([]byte("HELLO WORLD"))
conn.Close()
}
func main() {
ln, err := net.Listen("tcp", ":8080")
fmt.Println("Listening on Port 8080")
if err != nil {
log.Fatal(err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
}
go handleRequest(conn)
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码有什么问题?当我运行 curl http://localhost:8080 时,我得到这个输出
curl: (56) Recv …Run Code Online (Sandbox Code Playgroud) 所以,我是 Kafka 的新手,我已经阅读了一段时间。我在 confluent 上找到了这些信息。
https://docs.confluent.io/current/streams/architecture.html
所以我从这里了解到,假设我有一个名为 plain_text 的主题,我只是将一堆记录作为纯文本发送,而我只有一个具有单个主题和单个分区的代理。我现在启动 2 个消费者实例 ConsumerA 和 ConsumerB。由于我的分区计数小于消费者计数,因此只有一个消费者应该主动消费消息,而另一个消费者则处于空闲状态。如果我错了,请纠正我。
我使用 kafka-console-* 脚本进行了测试
bin/zookeeper-server-start.sh config/zookeeper.properties
Run Code Online (Sandbox Code Playgroud)
bin/kafka-server-start.sh config/server.properties
Run Code Online (Sandbox Code Playgroud)
bin/kafka-topics.sh --create \
--bootstrap-server localhost:9092 \
--replication-factor 1 \
--partitions 1 \
--topic plain_text
Run Code Online (Sandbox Code Playgroud)
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic plain_text
Run Code Online (Sandbox Code Playgroud)
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 \
--topic plain_text \
--formatter kafka.tools.DefaultMessageFormatter \
--property print.key=true \
--property print.value=true \
--property group.id=test_group \
Run Code Online (Sandbox Code Playgroud)
因此,两个消费者中的一个应该拥有该单个分区(如果我错了,请再次纠正我),但是我在生产者控制台上生成的任何内容在两个消费者控制台上都是可见的。为什么两个消费者都在使用来自单个分区的消息。我是否遗漏了什么或对 kafka-console-* 脚本执行了不同的规则。
apache-kafka kafka-consumer-api kafka-producer-api kafka-partition
我已经进行了一段时间的 Go 之旅,我刚刚注意到这行代码:-
today := time.Now().Weekday()
switch time.Saturday {
case today + 0:
fmt.Println("Today.")
case today + 1:
fmt.Println("Tomorrow.")
case today + 2:
fmt.Println("In two days.")
default:
fmt.Println("Too far away.")
}
Run Code Online (Sandbox Code Playgroud)
如果 Go 不支持运算符重载,我如何使用“+”运算符来增加一天?
我有 2 个从单个通道读取的 go-routines。4 秒后,我取消上下文并终止选择循环。在终止循环之前,我在通道上调用 close,因为有 2 个 go-routines close 被调用两次并导致恐慌,因为其中一个 go-routines 已经关闭了通道。目前我正在使用恢复来从恐慌中恢复过来,有没有更好的方法来做到这一点?
package main
import (
"context"
"fmt"
"sync"
"time"
)
func numberGen(ctx context.Context, numChan chan int) {
num := 0
doneCh := ctx.Done()
defer func() {
if r := recover(); r != nil {
fmt.Println("recovered from ", r)
}
}()
for {
select {
case <-doneCh:
fmt.Println("done generating...")
close(numChan)
return
default:
num++
numChan <- num
}
}
}
func main() {
ctx, cancelFn := context.WithCancel(context.Background())
numChan := make(chan …Run Code Online (Sandbox Code Playgroud)