在Tomcat的Servlet中使用Elasticsearh Java API时,得到"org.elasticsearch.client.transport.NoNodeAvailableException:没有一个配置的节点可用">
我正在尝试使用 cxf 创建一个简单的 REST API。
它将做以下工作 -
-
将从 Elasticsearch 集群(使用 https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html(读取一些记录。
-
返回结果作为 Json。
当我加热网址="http://localhost:8080/aee-services/sequence/?txID=5f64f331-63c9-485d-9018-6872b1cf5f91">
我得到了这个
/sequence : txId = 5f64f331-63c9-485d-9018-6872b1cf5f91
no modules loaded
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty3Plugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
19:09:09.147 [http-nio-8080-exec-4] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Invoking handleFault on interceptor org.apache.cxf.interceptor.ServiceInvokerInterceptor@1464d94b
19:09:09.147 [http-nio-8080-exec-4] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Invoking handleFault on interceptor org.apache.cxf.interceptor.OneWayProcessorInterceptor@168896e2
19:09:09.148 [http-nio-8080-exec-4] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Invoking handleFault on interceptor org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@2494bbf9
19:09:09.148 [http-nio-8080-exec-4] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Invoking handleFault on interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4e848932
19:09:09.164 [http-nio-8080-exec-4] WARN o.a.cxf.phase.PhaseInterceptorChain - Application {http://services.aee.onap.amdocs.com/}RestServer has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: None of the configured nodes are available: [{#transport#-1}{tIRObbGrQ3KLmv0v4OL4ag}{10.247.83.8}{10.247.83.8:9300}]
at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:163) ~[cxf-core-3.0.3.jar:3.0.3]
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:129) ~[cxf-core-3.0.3.jar:3.0.3]
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:200) ~[cxf-rt-frontend-jaxrs-3.0.3.jar:3.0.3]
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:99) ~[cxf-rt-frontend-jaxrs-3.0.3.jar:3.0.3]
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59) ~[cxf-core-3.0.3.jar:3.0.3]
at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96) ~[cxf-core-3.0.3.jar:3.0.3]
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307) ~[cxf-core-3.0.3.jar:3.0.3]
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) [cxf-core-3.0.3.jar:3.0.3]
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:251) [cxf-rt-transports-http-3.0.3.jar:3.0.3]
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223) [cxf-rt-transports-http-3.0.3.jar:3.0.3]
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:197) [cxf-rt-transports-http-3.0.3.jar:3.0.3]
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:149) [cxf-rt-transports-http-3.0.3.jar:3.0.3]
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:171) [cxf-rt-transports-http-3.0.3.jar:3.0.3]
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:290) [cxf-rt-transports-http-3.0.3.jar:3.0.3]
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:214) [cxf-rt-transports-http-3.0.3.jar:3.0.3]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) [servlet-api.jar:na]
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:265) [cxf-rt-transports-http-3.0.3.jar:3.0.3]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [catalina.jar:9.0.0.M22]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.0.M22]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) [tomcat-websocket.jar:9.0.0.M22]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.0.M22]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.0.M22]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [catalina.jar:9.0.0.M22]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:9.0.0.M22]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475) [catalina.jar:9.0.0.M22]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [catalina.jar:9.0.0.M22]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) [catalina.jar:9.0.0.M22]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:625) [catalina.jar:9.0.0.M22]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [catalina.jar:9.0.0.M22]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [catalina.jar:9.0.0.M22]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498) [tomcat-coyote.jar:9.0.0.M22]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:9.0.0.M22]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796) [tomcat-coyote.jar:9.0.0.M22]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1372) [tomcat-coyote.jar:9.0.0.M22]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:9.0.0.M22]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_121]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_121]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:9.0.0.M22]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_121]
Caused by: org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{tIRObbGrQ3KLmv0v4OL4ag}{10.247.83.8}{10.247.83.8:9300}]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347) ~[elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245) ~[elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59) ~[elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:363) ~[elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:408) ~[elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80) ~[elasticsearch-5.5.2.jar:5.5.2]
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54) ~[elasticsearch-5.5.2.jar:5.5.2]
at org.openecomp.sequence.elastic.data.ElasticDataSourceRetriveRecords.getLogRecords(ElasticDataSourceRetriveRecords.java:43) ~[classes/:na]
at com.amdocs.onap.aee.services.RestServer.getSequenceById(RestServer.java:35) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_121]
at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:181) ~[cxf-core-3.0.3.jar:3.0.3]
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:97) ~[cxf-core-3.0.3.jar:3.0.3]
... 37 common frames omitted
19:09:09.170 [http-nio-8080-exec-4] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.interceptor.MessageSenderInterceptor@54b8545c to phase prepare-send
19:09:09.170 [http-nio-8080-exec-4] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.jaxrs.interceptor.JAXRSDefaultFaultOutInterceptor@28d9d97 to phase marshal
19:09:09.170 [http-nio-8080-exec-4] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Chain org.apache.cxf.phase.PhaseInterceptorChain@48c00d4 was created. Current flow:
prepare-send [MessageSenderInterceptor]
marshal [JAXRSDefaultFaultOutInterceptor]
我 网络.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>onap-aee-services</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/aeeserver-beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
-豆子.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<jaxrs:server id="sequence" address="/">
<jaxrs:serviceBeans>
<bean class="com.aee.services.RestServer" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
</jaxrs:providers>
</jaxrs:server>
</beans>
和 REST 请求处理程序类
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.openecomp.sequence.SequenceGenerator;
import org.openecomp.sequence.elastic.data.ElasticDataSourceRetriveRecords;
import org.openecomp.sequence.elastic.data.LogRecord;
import org.openecomp.sequence.impl.SequenceGeneratorImpl;
import org.openecomp.sequence.json.SequenceResponse;
public class RestServer {
@GET
@Path("/sequence")
@Produces(MediaType.APPLICATION_JSON)
public SequenceResponse getSequenceById(@QueryParam("txID") final String txId) {
System.out.println("nnnn/sequence : txId = " + txId + "nnnn");
org.elasticsearch.common.settings.Settings settings = Settings.builder().put("client.transport.sniff", true)
.put("cluster.name", "onap-cluster").build();
TransportClient client = null;
try {
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.247.83.8"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.247.83.8"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
// on shutdown
String[] mdcs = { "PartnerName", "Component", "loglevel", "Timestamp", "Thread", "LogType", "BeginTimestamp",
"EndTimestamp" };
String[] mdcsexclude = { "message", "@timestamp", "@version", "beat" };
SearchResponse response1 = client.prepareSearch("onaplogs*").addSort("Timestamp", SortOrder.ASC).setFrom(0)
.setSize(100).setQuery(QueryBuilders.matchQuery("RequestId", txId))
.setFetchSource(mdcs, mdcsexclude).execute().actionGet();
SearchHits hits = response1.getHits();
List<LogRecord> logs = new ArrayList<LogRecord>();
System.out.println(
"totalHits" + response1.getHits().getTotalHits() + "hitlenght" + response1.getHits().getHits().length);
SequenceResponse sequenceResponse = null;
return sequenceResponse;
}
}
pom.xm
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.amdocs.onap.aee.services</groupId>
<artifactId>onap-aee-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<properties>
<jackson.version>1.8.6</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.5.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
请纠正我错的地方
我们可以在 Servlen 容器中使用 Elasticsearch Java API 吗?
请注意,当我使用 J4Unit 测试用例进行测试时,它工作正常。
请确保您在传输客户端(onap-cluster(中提供的集群名称与实际集群名称匹配。
还有两次添加相同 ip (10.247.83.8( 的任何原因?