简体   繁体   English

Java 的 UNIX 套接字实现?

[英]UNIX socket implementation for Java?

I realize that since UNIX sockets are platform-specific, there has to be some non-Java code involved.我意识到由于 UNIX 套接字是特定于平台的,因此必须涉及一些非 Java 代码。 Specifically, we're interested in using JDBC to connect to a MySQL instance which only has UNIX domain sockets enabled.具体来说,我们对使用 JDBC 连接到仅启用了 UNIX 域套接字的 MySQL 实例感兴趣。

It doesn't look like this is supported, but from what I've read it should be at least possible to write a SocketFactory for JDBC based on UNIX sockets if we can find a decent implementation of UNIX sockets for Java.看起来这不受支持,但从我读到的内容来看,如果我们能找到适用于 Java 的 UNIX 套接字的体面实现,那么至少应该可以为基于 UNIX 套接字的 JDBC 编写 SocketFactory。

Has anyone tried this?有没有人试过这个? Does anyone know of such an implementation?有谁知道这样的实现?

Checkout the JUDS library.查看 JUDS 库。 It is a Java Unix Domain Socket library...它是一个 Java Unix Domain Socket 库...

https://github.com/mcfunley/juds https://github.com/mcfunley/juds

You could use junixsocket: https://github.com/kohlschutter/junixsocket您可以使用 junixsocket: https : //github.com/kohlschutter/junixsocket

It already provides code for connecting to MySQL from Java (Connector/J) via Unix sockets.它已经提供了通过 Unix 套接字从 Java (Connector/J) 连接到 MySQL 的代码。

One big advantage compared to other implementations is that junixsocket uses the standard Java Socket API.与其他实现相比的一大优势是 junixsocket 使用标准的 Java Socket API。

The MariaDB JDBC driver now supports this and is compatible with the MySQL JDBC driver. MariaDB JDBC 驱动程序现在支持这一点并且与 MySQL JDBC 驱动程序兼容。

Use a JDBC url like:使用 JDBC url,如:

jdbc:mariadb://localhost:3306/revmgt?localSocket=/var/run/mysqld/mysqld.sock

Worth noting that this library require including the JNA library as it uses JNA to access native unix domain sockets.值得注意的是,这个库需要包含 JNA 库,因为它使用 JNA 访问本机 unix 域套接字。 It works pretty well in my testing.它在我的测试中运行良好。 I saw speed improvements on CPU bound java processes from the offload to native code.我看到了 CPU 绑定 Java 进程从卸载到本机代码的速度改进。

As the original kohlschutter/junixsocket , mentioned in another answer seems to be dead, you can check out its forks.由于在另一个答案中提到的原始kohlschutter/junixsocket似乎已死,您可以查看它的叉子。

Especially fiken/junixsocket looks promising.尤其是fiken/junixsocket看起来很有希望。 Its author has added support for connection to PostgreSQL using unix socket via pgjdbc , for example.例如,它的作者添加了对使用 unix socket 通过pgjdbc连接到 PostgreSQL 的支持。

Check out the JNA library.查看 JNA 库。 It's a halfway house between pure Java and JNI native code它是纯 Java 和 JNI 本机代码之间的中途之家

https://github.com/twall/jna/ https://github.com/twall/jna/

JNR 项目(它是项目 panama的松散基础)有一个unix socket实现。

As of Java 16, Unix domain sockets are supported natively by java through SocketChannel and ServerSocketChannel API.从 Java 16 开始,Java 通过SocketChannelServerSocketChannel API 原生支持 Unix 域套接字。

You can find more information about it in JEP380 proposal and implementation example here .你可以找到关于它的更多信息JEP380建议和实施例子在这里

Some searching on the internet has uncovered the following useful-looking library:在互联网上的一些搜索发现了以下有用的库:

http://www.nfrese.net/software/gnu_net_local/overview.html http://www.nfrese.net/software/gnu_net_local/overview.html

Wayback Link 回溯链接

Writing a socket factory should be easy enough.编写套接字工厂应该很容易。 Once you've done so, you can pass it to your driver THUSLY .( Wayback Link ).一旦你这样做,你可以将它传递给你的驱动正是如此。( 韦巴克链接)。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.mysql.management.driverlaunched.ServerLauncherSocketFactory;

public class ConnectorMXJTestExample {
    public static void main(String[] args) throws Exception {
        String hostColonPort = "localhost:3336";

        String driver = com.mysql.jdbc.Driver.class.getName();
        String url = "jdbc:mysql://" + hostColonPort + "/" + "?"
                + "socketFactory="
                + ServerLauncherSocketFactory.class.getName();
        String userName = "root";
        String password = "";

        Class.forName(driver);
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, userName, password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT VERSION()");
            rs.next();
            String version = rs.getString(1);
            rs.close();
            stmt.close();

            System.out.println("------------------------");
            System.out.println(version);
            System.out.println("------------------------");
        } finally {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            ServerLauncherSocketFactory.shutdown(hostColonPort);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM