Skip to content

The clientIP attribute of ClientConfig supports setting through Syste… #9262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ public class ClientConfig {
*/
protected String traceTopic;

public ClientConfig() {
String ip = System.getProperty("rocketmq.client.ip");
if (ip != null && !ip.trim().isEmpty()) {
clientIP = ip;
} else {
clientIP = NetworkUtil.getLocalAddress();
}
}

public String buildMQClientId() {
StringBuilder sb = new StringBuilder();
sb.append(this.getClientIP());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.rocketmq.client;

import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.common.utils.NetworkUtil;
import org.apache.rocketmq.remoting.protocol.LanguageCode;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -65,6 +66,17 @@ public void testQueuesWithNamespace() {
assertEquals("lmq%defaultTopic", messageQueues.iterator().next().getTopic());
}

@Test
public void testClientIpOverrideWithSystemProperty() {
String ip = "192.168.8.8";
System.setProperty("rocketmq.client.ip", ip);
ClientConfig clientConfig = new ClientConfig();
assertEquals(ip, clientConfig.getClientIP());
System.clearProperty("rocketmq.client.ip");
clientConfig = new ClientConfig();
assertEquals(NetworkUtil.getLocalAddress(), clientConfig.getClientIP());
}

private ClientConfig createClientConfig() {
ClientConfig result = new ClientConfig();
result.setUnitName("unitName");
Expand Down