react写一个冒泡排序
时间: 2023-10-11 21:04:32 浏览: 141
您可以使用以下docker-compose文件来创建一个运行go、kafka和elk的docker环境:
```
version: '3.3'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.9.2
ports:
- "9200:9200"
- "9300:9300"
environment:
- discovery.type=single-node
kibana:
image: docker.elastic.co/kibana/kibana:7.9.2
ports:
- "5601:5601"
environment:
ELASTICSEARCH_URL: http://elasticsearch:9200
go-app:
build: .
ports:
- "8080:8080"
environment:
KAFKA_HOST: kafka:9092
ELASTICSEARCH_HOST: elasticsearch:9200
```
请将上面的docker-compose文件保存为`docker-compose.yml`,然后在该文件所在的目录中运行以下命令以启动docker环境:
```
docker-compose up
```
这将启动以下服务:
- Zookeeper:Kafka所需的分布式协调器。
- Kafka:消息队列,用于在Go应用程序和ELK之间传递消息。
- Elasticsearch:用于存储和索引日志数据的搜索引擎。
- Kibana:用于可视化和查询Elasticsearch中存储的数据。
- Go应用程序:用于生成和发送日志消息到Kafka,然后将其存储在Elasticsearch中。
请注意,上面的docker-compose文件中包括一个名为`go-app`的服务,该服务需要使用Dockerfile构建Go应用程序的镜像。在该文件所在的目录中创建一个名为`Dockerfile`的文件,并将以下内容复制到其中:
```
FROM golang:1.15-alpine
# Set the working directory
WORKDIR /app
# Copy the source code
COPY . .
# Build the binary
RUN go build -o main .
# Run the binary
CMD ["./main"]
```
接下来,您需要在`docker-compose.yml`文件所在的目录中创建一个名为`main.go`的Go应用程序文件,并将以下内容复制到其中:
```
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/Shopify/sarama"
"github.com/elastic/go-elasticsearch/v8"
)
type Log struct {
Message string `json:"message"`
}
func main() {
// Connect to Kafka
kafkaConfig := sarama.NewConfig()
kafkaConfig.Producer.Return.Successes = true
kafkaBroker := fmt.Sprintf("%s:%d", getEnv("KAFKA_HOST", "localhost"), getEnvAsInt("KAFKA_PORT", 9092))
producer, err := sarama.NewSyncProducer([]string{kafkaBroker}, kafkaConfig)
if err != nil {
log.Fatalf("Failed to connect to Kafka broker: %s", err)
}
defer producer.Close()
// Connect to Elasticsearch
elasticsearchConfig := elasticsearch.Config{
Addresses: []string{
fmt.Sprintf("http://%s:%d", getEnv("ELASTICSEARCH_HOST", "localhost"), getEnvAsInt("ELASTICSEARCH_PORT", 9200)),
},
}
elasticsearchClient, err := elasticsearch.NewClient(elasticsearchConfig)
if err != nil {
log.Fatalf("Failed to connect to Elasticsearch: %s", err)
}
// Send logs to Kafka and Elasticsearch
for i := 1; i <= 10; i++ {
log := Log{Message: fmt.Sprintf("Log message %d", i)}
logBytes, _ := json.Marshal(log)
// Send log to Kafka
kafkaMessage := &sarama.ProducerMessage{
Topic: "logs",
Value: sarama.StringEncoder(logBytes),
}
_, _, err := producer.SendMessage(kafkaMessage)
if err != nil {
log.Fatalf("Failed to send message to Kafka: %s", err)
}
// Send log to Elasticsearch
elasticsearchIndex := "logs"
elasticsearchDocument := string(logBytes)
elasticsearchResponse, err := elasticsearchClient.Index(elasticsearchIndex, nil, elasticsearch.DocumentSource{Body: []byte(elasticsearchDocument)})
if err != nil {
log.Fatalf("Failed to send message to Elasticsearch: %s", err)
}
defer elasticsearchResponse.Body.Close()
}
}
func getEnv(key, defaultValue string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultValue
}
func getEnvAsInt(key string, defaultValue int) int {
if value, exists := os.LookupEnv(key); exists {
if intValue, err := strconv.Atoi(value); err == nil {
return intValue
}
}
return defaultValue
}
```
该应用程序将生成10条日志消息,并将每条消息发送到Kafka和Elasticsearch。请注意,该应用程序需要`sarama`和`go-elasticsearch`依赖项。您可以通过在`main.go`文件所在的目录中运行以下命令来安装这些依赖项:
```
go get github.com/Shopify/sarama
go get github.com/elastic/go-elasticsearch/v8
```
完成上述步骤后,您可以使用`docker-compose up`命令来启动docker环境,并使用以下URL访问Kibana界面:
```
http://localhost:5601
```
在Kibana界面中,您可以创建一个名为`logs-*`的索引模式,以查看从Go应用程序发送到Elasticsearch的日志数据。
阅读全文