您的位置:首页 > 编程语言 > Java开发

cassndra spring.xml配置

2016-07-07 17:28 381 查看

[java][cassandra][spring]

java操作
import com.datastax.driver.auth.DseAuthProvider;
import com.datastax.driver.core.AuthProvider;
import com.datastax.driver.core.HostDistance;
import com.datastax.driver.core.PoolingOptions;
import com.datastax.driver.core.SocketOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;

/**
 *
 * http://docs.spring.io/spring-data/cassandra/docs/1.3.2.RELEASE/reference/html/#cassandra-connectors.javaconfig 참고
 *
 * 참고
 */
@Configuration
@PropertySource("classpath:cassandra.properties")
@EnableCassandraRepositories(basePackages = { "com.xxx.xxxx.xxxxx." })
public class CassandraConfiguration {

@Autowired
public Environment cassandraMap;

@Bean
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints(cassandraMap.getProperty("cassandra.contactpoints"));
cluster.setPort(Integer.parseInt(cassandraMap.getProperty("cassandra.port")));
cluster.setAuthProvider(getAuthProvider());
cluster.setUsername(cassandraMap.getProperty("cassandra.username"));
cluster.setPassword(cassandraMap.getProperty("cassandra.password"));
cluster.setPoolingOptions(getPoolingOptions());
cluster.setSocketOptions(getSocketOptions());
return cluster;
}

protected PoolingOptions getPoolingOptions() {
PoolingOptions poolingOptions = new PoolingOptions();
// setting for local
poolingOptions.setMinSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, 25);
poolingOptions.setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, 100);
poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, 2);
poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, 8);
// setting for remote
poolingOptions.setMinSimultaneousRequestsPerConnectionThreshold(HostDistance.REMOTE, 25);
poolingOptions.setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.REMOTE, 100);
poolingOptions.setCoreConnectionsPerHost(HostDistance.REMOTE, 1);
poolingOptions.setMaxConnectionsPerHost(HostDistance.REMOTE, 2);
return poolingOptions;
}

protected SocketOptions getSocketOptions() {
SocketOptions socketOptions = new SocketOptions();
socketOptions.setConnectTimeoutMillis(5000);
socketOptions.setKeepAlive(true);
socketOptions.setReadTimeoutMillis(60000);
socketOptions.setReuseAddress(true);
socketOptions.setSoLinger(60);
socketOptions.setTcpNoDelay(true);
socketOptions.setReceiveBufferSize(65536);
socketOptions.setSendBufferSize(65536);
return socketOptions;
}

@Bean
public AuthProvider getAuthProvider() {
return new DseAuthProvider();
}

@Bean
public CassandraMappingContext mappingContext() {
return new BasicCassandraMappingContext();
}

@Bean
public CassandraConverter converter() {
return new MappingCassandraConverter(mappingContext());
}

@Bean
public CassandraSessionFactoryBean session() throws Exception {

CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
session.setCluster(cluster().getObject());
session.setKeyspaceName(cassandraMap.getProperty("cassandra.keyspace"));
session.setConverter(converter());
session.setSchemaAction(SchemaAction.NONE);

return session;
}

@Bean
public CassandraOperations cassandraTemplate() throws Exception {
return new CassandraTemplate(session().getObject());
}

}

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:cassandra="http://www.springframework.org/schema/data/cassandra"
xmlns:cass="http://www.springframework.org/schema/cql"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/cql http://www.springframework.org/schema/cql/spring-cql-1.0.xsd
    http://www.springframework.org/schema/data/cassandra http://www.springframework.org/schema/data/cassandra/spring-cassandra-1.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!-- Loads the properties into the Spring Context and uses them to fill in placeholders in the bean definitions -->

<context:property-placeholder location="classpath:cassandra.properties" />

<!-- authentication -->
<bean id="authProvider" class="com.datastax.driver.auth.DseAuthProvider" />

<!-- REQUIRED: The Cassandra Cluster -->
<cassandra:cluster
contact-points="${cassandra.contactpoints}" port="${cassandra.port}"
auth-info-provider-ref="authProvider" username="${cassandra.username}" password="${cassandra.password}">
<cassandra:local-pooling-options min-simultaneous-requests="25" max-simultaneous-requests="100" core-connections="2" max-connections="8" />
<cassandra:remote-pooling-options min-simultaneous-requests="25" max-simultaneous-requests="100" core-connections="1" max-connections="2" />
<cassandra:socket-options
connect-timeout-millis="5000" keep-alive="true" read-timeout-millis="60000"
reuse-address="true" so-linger="60" tcp-no-delay="true"
receive-buffer-size="65536" send-buffer-size="65536" />
</cassandra:cluster>

<!-- REQUIRED: The Cassandra Session, built from the Cluster, and attaching to a keyspace -->
<cassandra:session keyspace-name="${cassandra.keyspace}" />

<!-- REQUIRED: The Default Cassandra Mapping Context used by CassandraConverter -->
<cassandra:mapping />

<!-- REQUIRED: The Default Cassandra Converter used by CassandraTemplate -->
<cassandra:converter />

<!-- REQUIRED: The Cassandra Template is the building block of all Spring Data Cassandra -->
<cassandra:template id="cassandraTemplate"  />

<!-- OPTIONAL: If you are using Spring Data Cassandra Repositories, add your base packages to scan here -->
<cassandra:repositories base-package="com.xxx.xxx.xxxx.xxxx" cassandra-template-ref="cassandraTemplate" />

</beans>
위의 xml 에서 변수를 사용하기 위해서는,<util:properties id="cassandraConfig" location="classpath:cassandra.properties" /> 을 설정해 두어야 한다.

cassandra.properties

cassandra.contactpoints=xx.xx.xx.xxxcassandra.port=9042cassandra.keyspace=testcassandra.username=testcassandra.password=testtest
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: