There's a couple of ways you can deal with this.
First, you can just add all the hosts and ports to the URL, e.g.:
(tcp://master1:61616,tcp://slave1:61616,tcp://master2:61616,tcp://slave2:61616)
When a core client connects to a master it will automatically be informed of any backup, but you can't guarantee that one of the predefined masters will actually be up so it's safest to just list all the brokers in the URL.
Second, you can use discovery from the client, e.g.:
udp://231.7.7.7:9876
Here the client uses the same UDP multicast address and port that the brokers use to form the cluster. The client will listen on this multicast address & port to discover which brokers are up and then connect to one of them.
Of course for this to work UDP multicast packets must be able to reach the client from the broker and that typically means they have to be on the same network subnet as UDP multicast is typically not broadcast beyond this.