gpt4 book ai didi

java - Spring Data - 无法执行CommandLineRunner

转载 作者:太空宇宙 更新时间:2023-11-04 10:07:55 24 4
gpt4 key购买 nike

我对 Spring Data + Spring Boot 非常陌生。我已经下载了 Spring Data 项目并尝试在 Eclipse 中进行配置。

在执行项目时出现以下错误。

java.lang.IllegalStateException: Failed to execute CommandLineRunner

请找到我的Application.java文件

@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
private ElasticsearchOperations es;

@Autowired
private BookService bookService;

public static void main(String args[]) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... args) throws Exception {

printElasticSearchInfo();

bookService.save(new Book("1001", "Elasticsearch Basics", "Rambabu Posa", "23-FEB-2017"));
bookService.save(new Book("1002", "Apache Lucene Basics", "Rambabu Posa", "13-MAR-2017"));
bookService.save(new Book("1003", "Apache Solr Basics", "Rambabu Posa", "21-MAR-2017"));

//fuzzey search
Page<Book> books = bookService.findByAuthor("Rambabu", new PageRequest(0, 10));

//List<Book> books = bookService.findByTitle("Elasticsearch Basics");

books.forEach(x -> System.out.println(x));


}

//useful for debug
private void printElasticSearchInfo() {

System.out.println("--ElasticSearch-->");
Client client = es.getClient();
Map<String, String> asMap = client.settings().getAsMap();

asMap.forEach((k, v) -> {
System.out.println(k + " = " + v);
});
System.out.println("<--ElasticSearch--");
}

}

**更新 1 **

--ElasticSearch-->
client.type = transport
cluster.name = mkyong-cluster
name = Hyde
network.server = false
node.client = true
transport.ping_schedule = 5s
<--ElasticSearch--
2018-10-09 13:30:56.797 INFO 11476 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-10-09 13:30:56.842 ERROR 11476 --- [ main] o.s.boot.SpringApplication : Application startup failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at com.mkyong.Application.main(Application.java:26) [classes/:na]
Caused by: org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{127.0.0.1:9300}]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:326) ~[elasticsearch-2.4.4.jar:2.4.4]
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:223) ~[elasticsearch-2.4.4.jar:2.4.4]
at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55) ~[elasticsearch-2.4.4.jar:2.4.4]
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:295) ~[elasticsearch-2.4.4.jar:2.4.4]
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:359) ~[elasticsearch-2.4.4.jar:2.4.4]
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:86) ~[elasticsearch-2.4.4.jar:2.4.4]
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:56) ~[elasticsearch-2.4.4.jar:2.4.4]
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.index(ElasticsearchTemplate.java:536) ~[spring-data-elasticsearch-2.1.0.RELEASE.jar:na]
at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.save(AbstractElasticsearchRepository.java:142) ~[spring-data-elasticsearch-2.1.0.RELEASE.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_131]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.0.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at com.sun.proxy.$Proxy45.save(Unknown Source) ~[na:na]
at com.mkyong.book.service.BookServiceImpl.save(BookServiceImpl.java:23) ~[classes/:na]
at com.mkyong.Application.run(Application.java:34) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
... 6 common frames omitted

请找到我的 application.properties 文件

elasticsearch.clustername = mkyong-cluster
elasticsearch.host = localhost
elasticsearch.port = 9300

更新2

2018-10-09 14:34:01.155  INFO 12400 --- [           main] org.elasticsearch.client.transport       : [Terminatrix] failed to get node info for {#transport#-1}{127.0.0.1}{127.0.0.1:9200}, disconnecting...

org.elasticsearch.transport.ReceiveTimeoutTransportException: [][127.0.0.1:9200][cluster:monitor/nodes/liveness] request_id [0] timed out after [5037ms]
at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:698) ~[elasticsearch-2.4.4.jar:2.4.4]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[na:1.8.0_131]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[na:1.8.0_131]
at java.lang.Thread.run(Unknown Source) ~[na:1.8.0_131]

2018-10-09 14:34:01.455 ERROR 12400 --- [ main] .d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{127.0.0.1:9200}]
2018-10-09 14:34:01.789 INFO 12400 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup

另外,我已经安装了ElasticSearch 6.2.3(这是最新版本)。但在 stacktrace 中它显示 elasticsearch-2.4.4.jar:2.4.4。我该如何重新配置​​它?

最佳答案

据我所知 Spring-data-elastic-search 不支持 ES 6.2。正如您在堆栈跟踪中看到的,您的 ES 版本是 2.4。

如果要使用6.2,则需要使用HignLevelRestClient(基于Java的ES API)

这是 ES 6.2 的配置

@Configuration
public class ElasticSearchConfig extends AbstractFactoryBean {

private static final Logger LOG = LoggerFactory.getLogger(ElasticSearchConfig.class);

@Value("${spring.data.elasticsearch.cluster-nodes}")
private String clusterNodes;
@Value("${spring.data.elasticsearch.cluster-name}")
private String clusterName;
private RestHighLevelClient restHighLevelClient;

@Override
public void destroy() {
try {
if (restHighLevelClient != null) {
restHighLevelClient.close();
}
} catch (final Exception e) {
LOG.error("Error closing ElasticSearch client: ", e);
}
}

@Override
public Class<RestHighLevelClient> getObjectType() {
return RestHighLevelClient.class;
}

@Override
public boolean isSingleton() {
return false;
}

@Override
public RestHighLevelClient createInstance() {
return buildClient();
}

private RestHighLevelClient buildClient() {
try {
restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
} catch (Exception e) {
LOG.error(e.getMessage());
}
return restHighLevelClient;
}

}

这是你的 Controller :

@RestController
@RequestMapping("/books")
public class BookController {

@Autowired
private BookRepository bookRepository;


@GetMapping("/{id}")
public Map<String, Object> getBookById(@PathVariable String id){
return bookRepository.getBookById(id);
}

这是您的存储库:

@Repository
public class BookRepository {

private final String INDEX = "bookdata";
private final String TYPE = "books";
private RestHighLevelClient restHighLevelClient;
private ObjectMapper objectMapper;

public BookRepository( ObjectMapper objectMapper, RestHighLevelClient restHighLevelClient) {
this.objectMapper = objectMapper;
this.restHighLevelClient = restHighLevelClient;
}

public Map<String, Object> getBookById(String id){
GetRequest getRequest = new GetRequest(INDEX, TYPE, id);
GetResponse getResponse = null;
try {
getResponse = restHighLevelClient.get(getRequest);
} catch (java.io.IOException e){
e.getLocalizedMessage();
}
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
return sourceAsMap;
}

这是您的主类(class):

@SpringBootApplication
public class ElasticsearchImplApplication {

public static void main(String[] args) {
SpringApplication.run(ElasticsearchImplApplication.class, args);
}
}

这是您的配置文件:

spring.data.elasticsearch.cluster-name=your-cluster
spring.data.elasticsearch.cluster-nodes=elasticsearch

我希望这会有所帮助。

关于java - Spring Data - 无法执行CommandLineRunner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52715881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com