Skip to content
Advertisement

Why this error coming failed to load elasticsearch nodes in spring boot?

I am trying to use elasticsearch in my webapplication. I am using spring boot 2.0.6. I didn’t add any configuration file elastic search is auto configured by spring boot. I added spring data elastic search properties in application.properties like this

spring-data-elasticsearch-3.0.11

elasticsearch-5.6.12

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.properties.node.master: true
spring.data.elasticsearch.properties.node.data: false
spring.data.elasticsearch.properties.node.name: my-node
spring.data.elasticsearch.properties.node.attr.type: hot
spring.data.elasticsearch.properties.http.enabled: true
spring.data.elasticsearch.repositories.enabled=true  

When i run my application console showing

 o.elasticsearch.plugins.PluginsService   : no modules loaded
 o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
 o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
 o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
 o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
 o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.transport.Netty4Plugin]
 o.s.d.e.c.TransportClientFactoryBean     : Adding transport node : 127.0.0.1:9300

I added one simple example for demo purpose but i am getting this error

.d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{zYXxPcpaQ_6I9GI2yID8cQ}{localhost}{127.0.0.1:9300}]

Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loaders':
Invocation of init method failed; nested exception is NoNodeAvailableException
[None of the configured nodes are available: [{#transport#-1}{zYXxPcpaQ_6I9GI2yID8cQ}{localhost}{127.0.0.1:9300}]]

this is example i used

Users.java

@Document(indexName = "users", type = "users", shards = 1, replicas = 0, refreshInterval = "-1")
public class Users {

private String name;
private Long id;
private String teamName;
private Long salary;

Loaders.java

@Component
public class Loaders {

    @Autowired
    ElasticsearchOperations operations;

    @Autowired
    UsersRepository usersRepository;

    @PostConstruct
    @Transactional
    public void loadAll(){

        operations.putMapping(Users.class);
        System.out.println("Loading Data");
        usersRepository.saveAll(getData());
        System.out.printf("Loading Completed");

    }

    private List<Users> getData() {
        List<Users> userses = new ArrayList<>();
        userses.add(new Users("Ajay",123L, "Accounting", 12000L));
        userses.add(new Users("Jaga",1234L, "Finance", 22000L));
        userses.add(new Users("Thiru",1235L, "Accounting", 12000L));
        return userses;
    }
}

UsersRepository.java

public interface UsersRepository extends ElasticsearchRepository<Users, Long> {
    List<Users> findByName(String text);

    List<Users> findBySalary(Long salary);
}

Why i am getting error? Is there any other property should i use?

Advertisement

Answer

My problem solved after downloading elasticsearch 5.6.12 from here https://www.elastic.co/downloads/elasticsearch#ga-release

and adding config file

@Bean
public Client client() throws UnknownHostException {
    Settings settings = Settings.builder()
             .put("client.transport.sniff", true)
                .put("cluster.name", "elasticsearch").build();
    @SuppressWarnings("resource")
    TransportClient client = new PreBuiltTransportClient(settings)
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
     return client;
}

@Bean
public ElasticsearchOperations elasticsearchTemplate() throws UnknownHostException {
    return new ElasticsearchTemplate(client());
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement