public class CustomHttpConnectionManager extends BasicHttpClientConnectionManager { private static Logger logger = LoggerFactory.getLogger(CustomHttpConnectionManager.class); private DnsResolver dnsResolver; private Info info; public CustomHttpConnectionManager(DnsResolver dnsResolver, Info info) { super(RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(), (HttpConnectionFactory)null, (SchemePortResolver)null, dnsResolver); this.dnsResolver = dnsResolver; this.info =info; } public void connect(HttpClientConnection conn, HttpRoute route, int timeout, HttpContext context){ InetAddress address = null; HttpHost host; if (route.getProxyHost() != null) { host = route.getProxyHost(); } else { host = route.getTargetHost(); } address = host.getAddress(); if (address == null) { try { InetAddress[] addresses = this.dnsResolver.resolve(host.getHostName()); if (addresses != null && addresses.length > 0) { } } catch (UnknownHostException var10) { } } try { super.connect(conn, route, timeout, context); } catch (IOException e) { logger.error("执行失败!"); } String srcDestInfo = conn.toString(); logger.info("srcDestInfo:{}",srcDestInfo); if (!srcDestInfo.isEmpty()) { String port = srcDestInfo.substring(srcDestInfo.indexOf(":")+1, srcDestInfo.indexOf("<")); String ip = srcDestInfo.substring(srcDestInfo.indexOf(">") + 1, srcDestInfo.lastIndexOf(":")); String destinationPort = srcDestInfo.substring(srcDestInfo.lastIndexOf(":")+1); info.setDestinationIp(ip); info.setDestinationPort(Integer.parseInt(destinationPort)); info.setSourcePort(Integer.parseInt(port)); logger.info("info:{}",info.toString()); } }
String url = "jdbc:mysql://127.0.0.1:3306/xxxxx";
// jdbc:mysql:// :表示规定好协议,按照jdbc的方式连接到mysql数据库
// 127.0.0.1 :主机,也可以是localhost,表示连接ip地址
// 3306 :表示监听3306端口
// db01 :表示连接到db01数据库
// 将数据库的用户名、密码放在properties对象中
Properties properties = new Properties();
// 其中,“user”、“password”是规定好的,后面的value值根据实际填写即可。
properties.setProperty("user", "root"); // 用户名
properties.setProperty("password", "123456"); // 密码
// 建立连接
ConnectionImpl connection =(ConnectionImpl)DriverManager.getConnection(url, properties) ;
MysqlIO mysqlIO= connection.getIO();
Class aclass=mysqlIO.getClass();
Field[] fields=aclass.getDeclaredFields();
for (Field field:fields){
if (field.getName().equals("mysqlConnection"))
{
field.setAccessible(true);
Socket mySQLConnection=(Socket)field.get(mysqlIO);
System.out.println(mySQLConnection.getLocalPort());
}
System.out.println(field.getName());
}