Cassandra:开放让外界连接(位于Linux中Cassandra让外界Windows的java连接)

1.声明

当前内容主要用于本人学习之用,内容主要包括使用SpringBoot连接当前的Linux中的Cassandra

2.Cassandra开放端口让外界访问

主要修改两个部分即可:(找到conf文件夹中的cassandra.yaml文件)

在这里插入图片描述

listen_address:当前的Linux的地址
broadcast_address:为当前Linux的地址

注意需要关闭重启Cassandra,即可!

3.使用SpringBoot连接Cassandra

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.18.RELEASE</version>

	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!-- <spring.framework.version>5.2.13.RELEASE</spring.framework.version> -->
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-cassandra</artifactId>

		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>com.codahale.metrics</groupId>
			<artifactId>metrics-core</artifactId>
			<version>3.0.2</version>
		</dependency>
		<!--<dependency>
            <groupId>com.github.jnr</groupId>
            <artifactId>jnr-x86asm</artifactId>
            <version>1.0.2</version>
        </dependency>-->
        <!-- 32位电脑 -->
		<!-- <dependency>
			<groupId>com.github.jnr</groupId>
			<artifactId>jnr-ffi</artifactId>
			<version>2.1.9</version>
		</dependency> -->

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

上面中的com.github.jnr一点作用都没有!

java连接操作

public class CassandraConnectionTest {
    
    

	public static void main(String[] args) {
    
    

		Cluster cluster = Cluster.builder().addContactPoints("192.168.1.100").build();
		Session session = cluster.connect();
		if(session!=null) {
    
    
			System.out.println("连接当前的Cassandra数据库成功!");
		}else {
    
    
			System.out.println("连接数据库失败!");
		}
		session.close();
		cluster.close();
	}
}

此时连接成功!

在这里插入图片描述
此时虽然有报错但是不要紧:
在这里插入图片描述

这个连接打开操作还是成功的

猜你喜欢

转载自blog.csdn.net/weixin_45492007/article/details/114141524