如何访问 Weblogic 集群中管理服务器的 JNDI 树?



我有一个包含管理服务器、服务器 1 和服务器 2 的集群 应用程序将部署到服务器 1 和 2 的群集。

如果我在本地单个服务器中部署应用程序,则以下代码工作正常

InitialContext ctx = new InitialContext(); (MBeanServer) ctx.lookup("java:comp/env/jmx/domainRuntime");

但一旦部署到群集,它就会失败(命名异常(

查看JNDI树,我看到jmx/domainRuntime仅在管理服务器中可用。

所以基本上这就是我问题的原因,如果应用程序在服务器 1 或 2 中,如何访问管理服务器中的该资源。

提前谢谢。

根据https://docs.oracle.com/middleware/1213/wls/WJNDI/wls_jndi.htm#i473354 您应该采取以下方法:

示例 2-7 在 WebLogic 群集中使用命名服务

Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://acmeCluster:7001");
try {
Context ctx = new InitialContext(ht);
// Do the client's work
}
catch (NamingException ne) {
// A failure occurred
}
finally {
try {ctx.close();}
catch (Exception e) {
// a failure occurred
}
}

你还应该参考:如何在WebLogic上查找JNDI资源?

以及其中包含的最高答案,即

Hashtable<String, String> h = new Hashtable<String, String>(7);
h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, pURL); //For example "t3://127.0.0.1:7001"
h.put(Context.SECURITY_PRINCIPAL, pUsername);
h.put(Context.SECURITY_CREDENTIALS, pPassword);
InitialContext context = new InitialContext(h).........

只需更改代码中的代码块,这将要求您导入包 只需这样做,这将起作用:

Environment env = new Environment();
env.setProviderUrl("localhost");
env.setSecurityPrincipal("username");
env.setSecurityCredentials("password");``
if(ctx == null) {
try {
ctx = env.getInitialContext();
} catch (NamingException e) {
System.out.println("SAML2IdentityAsserterProviderImpl: Exception " );
e.printStackTrace();
}
}

最新更新