《Spring Data官方文档》5.3. Connecting to Cassandra with Spring至5.5. Introduction to CassandraTemplate...

    xiaoxiao2024-05-15  128

    5.3. 连接到Spring Cassandra

    5.3.1. 外部化连接属性

    你需要连接到Cassandra来创建配置文件信息。接触点是键空间的所必需的最小字段,但是为了清楚起见,最好能添加端口。

    我们称这些为cassandra.properties

    cassandra.contactpoints=10.1.55.80,10.1.55.81 cassandra.port=9042 cassandra.keyspace=showcase

    下面两个例子中我们将使用spring把这些属性加载到Spring上下文.

    5.3.2. XML 配置

    一个Cassandra基础配置中的XML配置元素如下所示。 这些元素都使用默认bean名称来保持配置代码干净和可读。

    这个例子说明配置Spring连接Cassandra很方便,而且还有一些其他选项可以用。Spring Data Cassandra配置中还提供了可供DataStax Java驱动程序使用的一切选项。 这包括但不限于身份验证,负载平衡策略,重试策略和池选项。所有Spring Data Cassandra方法名称和XML元素都采用驱动程序上的配置选项同样(或差不都)的命名,因此应该直接的映射任意已有的驱动程序配置。

    <?xml version='1.0'?> <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: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" /> <!-- REQUIRED: The Cassandra Cluster --> <cassandra:cluster contact-points="${cassandra.contactpoints}" port="${cassandra.port}" /> <!-- 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="org.spring.cassandra.example.repo" /> </beans>

    5.3.3. Java 配置

    下面的类展示了AnnotationConfigApplicationContext(aka JavaConfig)中的最基本的Cassandra配置。

    package org.spring.cassandra.example.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; @Configuration @PropertySource(value = { "classpath:cassandra.properties" }) @EnableCassandraRepositories(basePackages = { "org.spring.cassandra.example.repo" }) public class CassandraConfig { private static final Logger LOG = LoggerFactory.getLogger(CassandraConfig.class); @Autowired private Environment env; @Bean public CassandraClusterFactoryBean cluster() { CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean(); cluster.setContactPoints(env.getProperty("cassandra.contactpoints")); cluster.setPort(Integer.parseInt(env.getProperty("cassandra.port"))); return cluster; } @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(env.getProperty("cassandra.keyspace")); session.setConverter(converter()); session.setSchemaAction(SchemaAction.NONE); return session; } @Bean public CassandraOperations cassandraTemplate() throws Exception { return new CassandraTemplate(session().getObject()); } }

    5.4. 常规评审配置

    评审支持在当前版本中不可用。

    5.5. CassandraTemplate简介

    5.5.1. 实例化CassandraTemplate

    虽然我们在上面展示一个你可以直接实例化它的例子,但是’CassandraTemplate’应该始终被配置为一个Spring Bean。为了这个存在的Spring模块的目的,在此我们假定我们使用Spring容器。

    ‘CassandraTemplate’是’CassandraOperations’的实现类。 你应该指定你的’CassandraTemplate’实现它的接口定义,’CassandraOperations’。

    有两个简单的方式来获取’CassandraTemplate’,取决于你如何加载你的Spring应用程序上下文。

    自动装配
    @Autowired private CassandraOperations cassandraOperations;

    像所有Spring Autowiring,这里假设在’ApplicationContext’中只有一个类型为’CassandraOperations’的bean。 如果您有多个“CassandraTemplate”bean(如果您在同一个项目中使用多个键空间,则会是这种情况),可以添加 [email protected]/* */`注解到你想要自动装配的bean。

    @Autowired @Qualifier("myTemplateBeanId") private CassandraOperations cassandraOperations;
    使用ApplicationContext来查找Bean

    你也可以从’ApplicationContext’中查找’CassandraTemplate’bean。

    CassandraOperations cassandraOperations = applicationContext.getBean("cassandraTemplate", CassandraOperations.class);

    转载自 并发编程网 - ifeve.com

    相关资源:敏捷开发V1.0.pptx
    最新回复(0)