Home > database >  Client - Server Java Docker Container Communication
Client - Server Java Docker Container Communication

Time:01-29

I am now learning Docker and I want to create 2 simple client-server java containers in which the client sends an order and the server responds with a confirmation message that the order has been received.

What is the protocol that I should use to send a message from the client to the server?

Client-Server sample code

public class Client {

    private static int counter = 0;
    private final String name;
    private final int id;
    private final Server server;

    public Client(String name, @NotNull Server server) {
            this.name = name;
            this.id=   counter;
            this.server = server;
        }
    
    public void submitOrder(Order order) {
            this.order = order;
    
            if (server!= null) {
                server.receiveOrder(this);
            } else {
                throw new UnsupportedOperationException("Client did not select a preferred server.");
            }
        }
}

public class Server {

    private String serverName;

    public Server(String serverName) {
           this.serverName = serverName;
        }
    
    public void receiveOrder(Client client) {
            sendConfirmationEmailToClient();
        }
}

CodePudding user response:

Client/server communication can be done with different protocols, but the most common using HTTP.

You can also use a WebSocket to have real-time communication.

CodePudding user response:

While you could use TCP or UDP and build your own protocol on top of that I believe you should go with something easy to handle, possibly already existing so you can focus on the docker specialities. Otherwise the learning curve may be too steep.

I suggest you use HTTP and then just need HTTP server and client - presumably in Java as you raised the question here. So how about

For Tomcat you can even use already existing containers as a start.

  •  Tags:  
  • Related