来自具有JNDI的其他IP服务器的spring.datasource.jndi-name [英] spring.datasource.jndi-name from different ip server with JNDI

查看:100
本文介绍了来自具有JNDI的其他IP服务器的spring.datasource.jndi-name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一种服务,该服务使用本地JNDI(服务所在的服务器和JNDI)中的数据源.

I am developed a service that use the DataSource from a local JNDI (Server where to live the service and the JNDI).

在Spring引导中,此信息的声明如下:

In Spring boot this information is declared like this:

spring:数据源:
jndi名称:java:/comp/env/jdbc/MyLocalDB

spring: datasource:
jndi-name: java:/comp/env/jdbc/MyLocalDB

我想知道是否可以做这样的事情:

I want to know if is possible to do something like this:

spring:数据源:
jndi名称:java:192.168.0.1:8080/comp/env/jdbc/MyLocalDB

spring: datasource:
jndi-name: java:192.168.0.1:8080/comp/env/jdbc/MyLocalDB

谢谢

推荐答案

JNDI是用于访问目录的API.应用程序服务器包括用于维护应用程序,数据源和EJB Bean信息的目录.JNDI可用于访问其他类型的目录.例如,它可用于访问Active Directoy和LDAP目录服务器上的用户信息.

JNDI is an API to access directories. Application servers include directories to maintain information of the applications, the datasources and the EJB beans. JNDI can be used to access other types of directories. For instance, it can be used to access user information on Active Directoy and LDAP directory servers.

您可以创建访问这些JNDI目录的客户端程序,并获取诸如配置数据和RMI远程引用之类的数据来调用EJB bean.

You can create client programs that access these JNDI directories and obtain data such as configuration data and RMI remote references to invoke EJB beans.

访问远程JNDI目录

如果要创建客户端程序,则可以定义 InitialContext 描述您的程序将使用的JNDI目录.您必须使用配置定义 Hashtable ,定义 Context.PROVIDER_URL 并创建 InitialContext .

If you are creating a client program, you can define an InitialContext describing the JNDI directory your program will use. You must define a Hashtable with the configuration, defining a Context.PROVIDER_URL and create the InitialContext.

例如,您可以创建一个程序,该程序在LDAP目录.

For instance, you can create a program that access information in an LDAP directory.

// Set up the environment for creating the initial context
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

// Create the initial context
DirContext ctx = new InitialDirContext(env);

// ... do something useful with ctx

根据您使用的库,可以定义一个描述JNDI远程主机的配置文件.例如,对于 jndi.properties 文件.

Depending on the library you use, you can define a configuration file describing the JNDI remote host. For instance, for the JBoss EJB Client library, you can define a jboss-ejb-client.propertiesfile. Glassfish uses a jndi.properties file.

访问远程资源

资源是程序需要独立于程序代码位置访问的数据(例如图像,音频或文本).您可以使用JNDI API或 @Resource 批注以访问信息.

A resource is data (e.g an image, audio, or text) that a program needs to access independently of the location of the program code. You can use the JNDI API or the @Resource annotation to access the information.

例如,您可以使用API​​在LDAP目录中查询用户数据.

For instance, you can use the API to ask for data of users in an LDAP directory.

try {

    // Create the initial directory context
    DirContext ctx = new InitialDirContext(env);

    // Ask for all attributes of the object 
    Attributes attrs = ctx.getAttributes("cn=Ted Geisel, ou=People");

    // Find the surname attribute ("sn") and print it
    System.out.println("sn: " + attrs.get("sn").get());

} catch (NamingException e) {
    System.err.println("Problem getting attribute:" + e);
}

访问远程EJB

您可以

You can access EJBs with remote interfaces located in other VMs. On the one hand, you must create the EJBs using CORBA-style remote interfaces and register the EJBs in the java: namespace in the server. On the other hand, the applications can use a corbaname URL to access these beans.

在Websphere/Liberty中,如果您定义绑定到以下对象的EJB:

In Websphere/Liberty, if you define an EJB bound to:

java:global/ExampleApp/ExampleModule/ExampleBean!com.ibm.example.ExampleRemoteInterface
java:app/ExampleModule/ExampleBean!com.ibm.example.ExampleRemoteInterface    
java:module/ExampleBean!com.ibm.example.ExampleRemoteInterface

您可以使用RMI-IIOP远程访问EJB(以调用方法)

You can access remotely the EJB using RMI-IIOP (to invoke methods)

corbaname::test.ibm.com:2809#ejb/global/ExampleApp/ExampleModule/ExampleHomeBean!com.ibm.example.ExampleEJBHome
corbaname:rir:#ejb/global/ExampleApp/ExampleModule/ExampleHomeBean!com.ibm.example.ExampleEJBHome

这些远程EJB的详细配置可能因一台应用程序服务器而异.如果您使用Glassfish,则可以查看文档.Oracle博客中有一个示例.

Detailed configuration of these remote EJBs may vary from one application server to another. If you use Glassfish, you can check the documentation. There is an example in the Oracle blogs.

如果配置集群,则还有许多其他

If you configure a cluster, there are many other options to access the remote resources using the names of the nodes in the cluster.

访问远程数据源

服务器中定义的数据源无法远程访问.如果要将数据源的配置保留在JNDI目录中,则可以解决方案在本地定义

Datasources defined in a server cannot be accessed remotely. If you want to keep the configuration of the datasources in the JNDI directory, you can solutions define a custom class that implements the DataSource interface. A client can obtain the datasource configuration from the server and create a local database connection using that connection.

这篇关于来自具有JNDI的其他IP服务器的spring.datasource.jndi-name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆