Skip to content

Commit 39164ce

Browse files
authored
Support custom advertised address in pulsar standalone (#187)
1 parent 99f7e21 commit 39164ce

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

pulsar-broker/src/main/java/com/yahoo/pulsar/PulsarStandaloneStarter.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.apache.commons.lang3.StringUtils.isBlank;
1919

2020
import java.io.FileInputStream;
21+
import java.net.URI;
2122
import java.net.URL;
2223

2324
import org.slf4j.Logger;
@@ -29,6 +30,7 @@
2930
import com.google.common.collect.Sets;
3031
import com.yahoo.pulsar.broker.PulsarService;
3132
import com.yahoo.pulsar.broker.ServiceConfiguration;
33+
import com.yahoo.pulsar.broker.ServiceConfigurationUtils;
3234
import com.yahoo.pulsar.client.admin.PulsarAdmin;
3335
import com.yahoo.pulsar.client.admin.PulsarAdminException;
3436
import com.yahoo.pulsar.common.configuration.PulsarConfigurationLoader;
@@ -70,6 +72,9 @@ public class PulsarStandaloneStarter {
7072
@Parameter(names = { "--only-broker" }, description = "Only start Pulsar broker service (no ZK, BK)")
7173
private boolean onlyBroker = false;
7274

75+
@Parameter(names = { "-a", "--advertised-address" }, description = "Standalone broker advertised address")
76+
private String advertisedAddress = null;
77+
7378
@Parameter(names = { "-h", "--help" }, description = "Show this help message")
7479
private boolean help = false;
7580

@@ -103,6 +108,16 @@ public PulsarStandaloneStarter(String[] args) throws Exception {
103108
config.setGlobalZookeeperServers("127.0.0.1:" + zkPort);
104109
config.setWebSocketServiceEnabled(true);
105110

111+
if (advertisedAddress != null) {
112+
// Use advertised address from command line
113+
config.setAdvertisedAddress(advertisedAddress);
114+
} else if (isBlank(config.getAdvertisedAddress())) {
115+
// Use advertised address as local hostname
116+
config.setAdvertisedAddress(ServiceConfigurationUtils.unsafeLocalhostResolve());
117+
} else {
118+
// Use advertised address from config file
119+
}
120+
106121
Runtime.getRuntime().addShutdownHook(new Thread() {
107122
public void run() {
108123
try {
@@ -143,15 +158,22 @@ void start() throws Exception {
143158
broker.start();
144159

145160
// Create a sample namespace
146-
URL url = new URL("http://127.0.0.1:" + config.getWebServicePort());
147-
admin = new PulsarAdmin(url, config.getBrokerClientAuthenticationPlugin(),
161+
URL webServiceUrl = new URL(
162+
String.format("http://%s:%d", config.getAdvertisedAddress(), config.getWebServicePort()));
163+
String brokerServiceUrl = String.format("pulsar://%s:%d", config.getAdvertisedAddress(),
164+
config.getBrokerServicePort());
165+
admin = new PulsarAdmin(webServiceUrl, config.getBrokerClientAuthenticationPlugin(),
148166
config.getBrokerClientAuthenticationParameters());
149167
String property = "sample";
150168
String cluster = config.getClusterName();
151169
String namespace = property + "/" + cluster + "/ns1";
152170
try {
171+
ClusterData clusterData = new ClusterData(webServiceUrl.toString(), null /* serviceUrlTls */,
172+
brokerServiceUrl, null /* brokerServiceUrlTls */);
153173
if (!admin.clusters().getClusters().contains(cluster)) {
154-
admin.clusters().createCluster(cluster, new ClusterData(url.toString()));
174+
admin.clusters().createCluster(cluster, clusterData);
175+
} else {
176+
admin.clusters().updateCluster(cluster, clusterData);
155177
}
156178

157179
if (!admin.properties().getProperties().contains(property)) {

0 commit comments

Comments
 (0)