Reputation: 41
In my Spring Boot Cassandra build I am getting the following error: s0-admin-1] c.d.o.d.i.c.control.ControlConnection : [s0] Error connecting to Node(endPoint=localhost:1234, hostId=null, hashCode=37hfeouh3), trying next node (ConnectionInitException: [s0|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedChannelException))
Entity type of
@Data
@Builder
@Table
public class Class1 {
@Id
private String id;
private String data;
private Class2 data2;
private Integer data3;
...
}
public class2 Class2 {
@Id
@JasonProperty
private String id;
@Indexed
@JasonProperty
private String data;
@JasonProperty
private String data2;
@JasonProperty
private Integer data3;
...
}
@Configuration
@EnableCassandraRepositories
@ConfigurationProperties(prefix = "DBProperties")
public class ApplicationConfig extends AbstractCassandraConfiguration {
private String DBKEYSPACE;
@Override
protected String getKeyspaceName() {
return DBKEYSPACE;
}
public String[] getEntityBasePackages() {
return new String[] { "com.oreilly.springdata.cassandra" };
}
}
@ConfigurationProperties(prefix = "DBPROPERTIES")
@Slf4j
public class FactoryBeanAppConfig {
private String contactPoints;
private String keySpace;
private Integer port;
private String password;
private String username;
private String dataCenter;
/*
* Factory bean that creates the com.datastax.oss.driver.api.core.CqlSession instance
*/
@Bean
public CqlSessionFactoryBean session() {
//log it we made it.
log.info("I made it to CqlSessionFactoryBean");
CqlSessionFactoryBean session = new CqlSessionFactoryBean();
session.setContactPoints(URLINFO);
log.info("Contact Points: " +URLINFO);
session.setKeyspaceName(DBKEYSPACE);
//session.setPort(OURPORT);
session.setUsername(username);
session.setPassword(password);
session.setLocalDatacenter(LOCALDCENTER INFORMATION);
return session;
}
}
I am unable to find a good example or even a get it to work correctly. Looking at this documentation: https://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#cassandra.core thats the only thing I should have to do to implement example 55
Upvotes: 4
Views: 2900
Reputation: 1137
For the spring boot run your application, it need to load the DB when your server application (the tomcat for example) is starting. So, your schema should be created first. If it is ok, you could change the "localhost" to "127.0.0.1" in your cassandra.yaml file.
Important: "[s0] Error connecting to Node(endPoint=localhost:1234,..." please check the cassandra's port. The correct is 9042.
It will solve your problem. However, others errors can be happen, because the others classes.
Then, you could correct the classes below:
@SpringBootApplication
@EnableCassandraRepositories(basePackages = { "<package's path>" })
@EntityScan(basePackages = { "<package's path>" })
@ComponentScan(basePackages = { "<package's path>" })
public class ApplicationConfig extends SpringBootServletInitializer
{
SpringApplication.run(ApplicationConfig.class, args);
}
Entity:
@Table("<table name>")
public class Class1 {
@PrimaryKeyColumn(name = "<field name id>", type = PrimaryKeyType.PARTITIONED)
private String id;
@Column("<field name data>")
private String data;
private Class2 data2; //I think this declaretion can cause error
@Column("<field name data3>")
private Integer data3;
...
}
This FactoryBeanAppConfig's class is not sound good. I created a class to read the application.properties and inject this class to connect with the db datas like keyspace's name, port, and so one. This link will help you to creat this class: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config . And then, you use this class in your FactoryBeanAppConfig's class to get DBKEYSPACE, OURPORT, ... .
This is a example to helps you to understand what I'm saying: https://www.baeldung.com/spring-data-cassandra-tutorial .
Upvotes: 1