我正在使用JDK 8和Spring在Cassandra 3.4上进行一次POC,并且我在集群模式下运行它,我有两台主机,我希望如果一台主机出现故障,那么另一台主机应该工作。 但它没有发生,可能是我在定义选项时犯了一些错误。任何人都可以检查一次吗?
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PoolingOptions;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
import com.datastax.driver.core.policies.TokenAwarePolicy;
public class CassandraConnectionManager {
int concurrency = 50;
int maxRequestsPerConnection = 128;
int maxConnections = 10;
int coreConnections = 5;
int port = 7001;
String hosts[];
private Cluster cluster;
private Session session;
public CassandraConnectionManager(String hosts, String port, int maxConn, int coreConn) {
this.maxConnections = maxConn;
this.coreConnections = coreConn;
this.port = Integer.parseInt(port);
this.hosts = hosts.split(",");
this.CassandraConnect();
}
public CassandraConnectionManager(String hosts) {
this.maxConnections = concurrency / maxRequestsPerConnection + 1;
this.hosts = hosts.split(",");
this.CassandraConnect();
}
private void CassandraConnect() {
PoolingOptions pools = new PoolingOptions();
/*pools.setCoreConnectionsPerHost(HostDistance.LOCAL, coreConnections);
pools.setMaxConnectionsPerHost(HostDistance.LOCAL, maxConnections);
pools.setCoreConnectionsPerHost(HostDistance.REMOTE, coreConnections);
pools.setMaxConnectionsPerHost(HostDistance.REMOTE, maxConnections);
pools.setMaxRequestsPerConnection(HostDistance.LOCAL, concurrency);*/
Cluster.Builder builder = new Cluster.Builder();
builder.withPort(port);
for (String host : hosts) {
builder.addContactPoints(host);
}
//builder.addContactPoints(hosts);
//builder.withPoolingOptions(pools);
//builder.withSocketOptions(new SocketOptions().setTcpNoDelay(true));
//builder.withRetryPolicy(DefaultRetryPolicy.INSTANCE);
builder.withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy.Builder().build()));
//builder.withReconnectionPolicy(new ConstantReconnectionPolicy(100L));
this.cluster = builder.build();
this.session = cluster.connect();
System.out.println("Connected");
}
public Session getSession() {
return this.session;
}
/** Close cluster. */
public void close() {
cluster.close();
}
}
public class CassandraDAO {
private CassandraConnectionManager casConnManager;
private Session casSession;
private PreparedStatement insertIntoP1events;
final static Logger logger = Logger.getLogger(CassandraDAO.class);
public CassandraDAO(CassandraConnectionManager casConnManager){
this.casConnManager = casConnManager;
this.casSession = casConnManager.getSession();
}
public void insert(Person book) {
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO ").append("showcase.login_event")
.append("(person_id, event_time, event_code,ip_address) ")
.append("VALUES ('").append(book.getId()).append("', '")
.append(book.getEvent_time()).append("', ")
.append(book.getEV()).append(",'192.168.50.50');");
String query = sb.toString();
casSession.execute(query);
}
}
public class TEST {
private static ClassPathXmlApplicationContext ctx ;
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/resources/applicationContext.xml");
CassandraDAO casDAO = (CassandraDAO) context.getBean("casDao");
for(int i=0;i<1000000;i++){
Person p = new Person(i, "Ev 0", 24,"2017-12-12T00:00:00Z");
try{
casDAO.insert(p);
System.out.println("Inserting .."+i);
}catch(Exception e){
e.printStackTrace();
}
Thread.sleep(1000);
}
System.out.println("Exiting ..");
}
}