Skip to content

Commit 82f8a9f

Browse files
committed
fixed issue #483 , show slave hosts
1 parent f5bf166 commit 82f8a9f

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

driver/src/main/java/com/alibaba/otter/canal/parse/driver/mysql/socket/BioSocketChannel.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ public SocketAddress getRemoteSocketAddress() {
138138
if (socket != null) {
139139
return socket.getRemoteSocketAddress();
140140
}
141+
142+
return null;
143+
}
144+
145+
public SocketAddress getLocalSocketAddress() {
146+
Socket socket = this.socket;
147+
if (socket != null) {
148+
return socket.getLocalSocketAddress();
149+
}
150+
141151
return null;
142152
}
143153

driver/src/main/java/com/alibaba/otter/canal/parse/driver/mysql/socket/NettySocketChannel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ public SocketAddress getRemoteSocketAddress() {
216216
return channel != null ? channel.remoteAddress() : null;
217217
}
218218

219+
public SocketAddress getLocalSocketAddress() {
220+
return channel != null ? channel.localAddress() : null;
221+
}
222+
219223
public void close() {
220224
if (channel != null) {
221225
channel.close();

driver/src/main/java/com/alibaba/otter/canal/parse/driver/mysql/socket/SocketChannel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public interface SocketChannel {
2121

2222
public SocketAddress getRemoteSocketAddress();
2323

24+
public SocketAddress getLocalSocketAddress();
25+
2426
public void close();
2527
}

parse/src/main/java/com/alibaba/otter/canal/parse/inbound/mysql/MysqlConnection.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.IOException;
66
import java.net.InetSocketAddress;
7+
import java.net.SocketAddress;
78
import java.nio.charset.Charset;
89
import java.util.List;
910
import java.util.concurrent.TimeUnit;
@@ -254,10 +255,18 @@ public void dump(GTIDSet gtidSet, MultiStageCoprocessor coprocessor) throws IOEx
254255

255256
private void sendRegisterSlave() throws IOException {
256257
RegisterSlaveCommandPacket cmd = new RegisterSlaveCommandPacket();
257-
cmd.reportHost = authInfo.getAddress().getAddress().getHostAddress();
258+
SocketAddress socketAddress = connector.getChannel().getLocalSocketAddress();
259+
if (socketAddress == null || !(socketAddress instanceof InetSocketAddress)) {
260+
return;
261+
}
262+
263+
InetSocketAddress address = (InetSocketAddress) socketAddress;
264+
String host = address.getHostString();
265+
int port = address.getPort();
266+
cmd.reportHost = host;
267+
cmd.reportPort = port;
258268
cmd.reportPasswd = authInfo.getPassword();
259269
cmd.reportUser = authInfo.getUsername();
260-
cmd.reportPort = authInfo.getAddress().getPort(); // 暂时先用master节点的port
261270
cmd.serverId = this.slaveId;
262271
byte[] cmdBody = cmd.toBytes();
263272

parse/src/test/java/com/alibaba/otter/canal/parse/MysqlBinlogDumpPerformanceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public class MysqlBinlogDumpPerformanceTest {
2020

2121
public static void main(String args[]) {
2222
final MysqlEventParser controller = new MysqlEventParser();
23-
final EntryPosition startPosition = new EntryPosition("mysql-bin.001699", 120L, 100L);
23+
final EntryPosition startPosition = new EntryPosition("mysql-bin.000007", 89796293L, 100L);
2424
controller.setConnectionCharset(Charset.forName("UTF-8"));
2525
controller.setSlaveId(3344L);
2626
controller.setDetectingEnable(false);
2727
controller.setFilterQueryDml(true);
28-
controller.setMasterInfo(new AuthenticationInfo(new InetSocketAddress("127.0.0.1", 3328), "root", "hello"));
28+
controller.setMasterInfo(new AuthenticationInfo(new InetSocketAddress("100.81.154.142", 3306), "canal", "canal"));
2929
controller.setMasterPosition(startPosition);
3030
controller.setEnableTsdb(false);
3131
controller.setDestination("example");

0 commit comments

Comments
 (0)