小编The*_*xme的帖子

Docker - 如何在启动后自动在 REST 容器上发布请求?

我有一个 docker 容器,它是由docker-compose. 该容器在启动时需要通过向其 REST API 发送 HTTP POST 请求进行配置。

基本上我现在所做的就是运行docker-compose up并等待一段时间,以便一切似乎都已开始,特别是给定的 docker 容器。然后我使用命令发送 HTTP POST curl

有没有办法修改,Dockerfile以便启动 Docker 容器,等待其 REST API 启动并发布给定的请求?


我的问题是一个通用问题,但如果您需要更多详细信息,这是我当前的非常简单的问题Dockerfile

FROM 1ambda/kafka-connect:latest
COPY my-project/target/*.jar $KAFKA_HOME/libs/
Run Code Online (Sandbox Code Playgroud)

以及相应的行docker-compose.yml

connect:
    build: kafka-connect
    links:
      - kafka
    ports:
      - "8083:8083"
    environment:
        CONNECT_BOOTSTRAP_SERVERS: kafka:9092
        CONNECT_GROUP_ID: connect-cluster-A
Run Code Online (Sandbox Code Playgroud)

最后,容器启动后我会执行 HTTP POST:

curl -H "Content-Type: application/json" -X POST -d '
{ 
"name":"my-project",
"config" : 
    {
    "name":"my-project",
    "topics":"my-topic",
    "tasks.max":2,
    "connector.class":"the.package.to.my.connector.class"
    }
}
' http://localhost:8083/connectors
Run Code Online (Sandbox Code Playgroud)

post docker dockerfile docker-compose

5
推荐指数
1
解决办法
4785
查看次数

Bson - 如何将JSON转换为List <Document>并将List <Document>转换为JSON?

我正在使用Java Driver 3.0和MongoDB,以便通过Web服务发送JSON.

当我想将Document对象(org.bson.Document)转换为JSON时,我会使用obj.toJson(),当我想将JSON转换为Document对象时,我会使用Document.parse(json).

但是,当我处理文档列表(在JSON中表示如下:)时[{"field1":1, ...}, {"field1":2, ...}],我无法找到一种干净的方式来进行这些转换.

到目前为止,我已经提出了这些"黑客":

  • 从List到JSON:我将文档列表添加为更大文档中名为"list"的字段的值.我将这个大文档转换为JSON,并从获取的String中删除我不需要的内容.

    public String toJson(List<Document> docs){
        Document doc = new Document("list", docs);
        String json = doc.toJson();
        return json.substring(json.indexOf(":")+2, json.length()-1);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 从JSON到List:我通过将此"list"字段添加到JSON,将其转换为Document并仅从Document获取此字段的值来执行相反的操作.

    public static List<Document> toListOfDocuments(String json){
        Document doc = Document.parse("{ \"list\":"+json+"}");
        Object list = doc.get("list");
        if(list instanceof List<?>) {
            return (List<Document>) doc.get("list");
        }
        return null ;
    }
    
    Run Code Online (Sandbox Code Playgroud)

我还尝试使用另一个JSON序列化程序(我使用了Google的序列化程序),但它没有提供与toJson()Document对象中的内置方法相同的结果,特别是对于"_id"字段或时间戳.

这样做有什么干净的方法吗?

java mongodb bson

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

bson ×1

docker ×1

docker-compose ×1

dockerfile ×1

java ×1

mongodb ×1

post ×1