docker安装cassandra数据库并整合spring webflux工程

一、docker安装cassandra数据库

1.拉取cassandra数据库镜像

[root@localhost ~]# docker pull cassandra
Using default tag: latest
latest: Pulling from library/cassandra
Digest: sha256:c186d79dca1cebe1ce382aaba201cce08b0f415e850d4b9eb6c1317f02bea293
Status: Image is up to date for cassandra:latest
docker.io/library/cassandra:latest

2.查看挂载网络

[root@localhost ~]# docker network ls
NETWORK ID     NAME            DRIVER    SCOPE
74f885e8ff56   bridge          bridge    local
003e4697a848   cassandra-net   bridge    local
653aafe6b946   host            host      local
ede8314218a4   none 

3.创建持久化文件夹

[root@localhost ~]# mkdir -p /root/cassandra

4.关闭防火墙或放行端口(此处只演示关闭防火墙)

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

5.启动cassandra数据库并进入cassandra容器

[root@localhost ~]# docker run -d --name cassandra --restart always -p 9042:9042 --network cassandra-net -v /root/cassandra:/var/lib/cassandra -e CASSANDRA_USER=cassandra -e CASSANDRA_PASSWORD=cassandra cassandra
836cf7a0983f487a707cf02b77a45eb96880e03e86949bfea02e0e3c69e9b422
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                                          NAMES
836cf7a0983f   cassandra   "docker-entrypoint.s…"   12 seconds ago   Up 10 seconds   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042->9042/tcp, :::9042->9042/tcp   cassandra
[root@localhost ~]# docker exec -it 836cf7a0983f bash

二、启动cassandra数据库并创建所需数据

1.启动cassandra数据库

root@836cf7a0983f:/# cqlsh
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.

2.将cassandra数据库配置文件复制至容器外进行修改(因为容器内没有基本文档编辑器)

cqlsh> exit 
root@836cf7a0983f:/# exit
exit
[root@localhost ~]# docker cp 836cf7a0983f:/opt/cassandra/conf/cassandra.yaml .
Successfully copied 71.2kB to /root/.

3.编辑cassandra.yaml

# 配置 Cassandra 监听的地址,允许远程连接。默认情况下,该项通常是设置为本地地址(127.0.0.1)。您可以将其设置为具体的 IP 地址或者 0.0.0.0(表示接受任何 IP 地址的连接)。
listen_address: <your_ip_address_or_0.0.0.0>

# 监听端口(Listen Port):配置 Cassandra 监听的端口,用于接受客户端和其他节点的连接。默认情况下,Cassandra 使用 9042 端口。
native_transport_port: 9042

# 安全配置(Security Configuration):如果需要启用认证和授权,您还需要配置用户名、密码以及相应的角色权限。
authenticator: PasswordAuthenticator

4.将配置文件复制回容器,并重启容器

[root@localhost ~]# docker cp cassandra.yaml 836cf7a0983f:/opt/cassandra/conf/
Successfully copied 71.2kB to 836cf7a0983f:/opt/cassandra/conf/
[root@localhost ~]# docker restart 836cf7a0983f
836cf7a0983f
[root@localhost ~]# docker exec -it 836cf7a0983f bash

5.使用默认账号启动cassandra,并创建新用户以供外部连接

root@836cf7a0983f:/# cqlsh
Connection error: ('Unable to connect to any servers', {'127.0.0.1:9042': AuthenticationFailed('Remote end requires authentication')})
root@836cf7a0983f:/# cqlsh -u cassandra -p cassandra
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
cassandra@cqlsh> create user test_data with password '123456' nosuperuser;
cassandra@cqlsh> exit
root@836cf7a0983f:/# cqlsh -u test_data -p 123456
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.

6.创建键空间

test_data@cqlsh> CREATE KEYSPACE IF NOT EXISTS store WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : '1' };
test_data@cqlsh> desc keyspaces;

store   system_auth         system_schema  system_views         
system  system_distributed  system_traces  system_virtual_schema

test_data@cqlsh> desc keyspace store

CREATE KEYSPACE store WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}  AND durable_writes = true;

7.插入测试数据

test_data@cqlsh> CREATE TABLE IF NOT EXISTS store.shopping_cart(
   ... userid text PRIMARY KEY,
   ... item_count int,
   ... last_update_timestamp timestamp
   ... );
test_data@cqlsh> insert into store.shopping_cart
   ... (userid,item_count,last_update_timestamp)
   ... values('9876',2,toTimeStamp(now()));
test_data@cqlsh> insert into store.shopping_cart
   ... (userid,item_count,last_update_timestamp)
   ... values('1234',5,toTimeStamp(now()));
test_data@cqlsh> select * from store.shopping_cart;

 userid | item_count | last_update_timestamp
--------+------------+---------------------------------
   1234 |          5 | 2024-03-26 02:02:02.098000+0000
   9876 |          2 | 2024-03-26 02:00:55.666000+0000

(2 rows)

三、整合spring webflux(maven工程)

1.导入依赖

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

2.补充配置文件

server:
  port: 8081
spring:
  data:
    cassandra:
      contact-points: 你的虚拟机地址
      port: 9042
      local-datacenter: datacenter1 # 本地数据中心必须指定
      session-name: JtTistCluster # 创建Cassandra会话对象,建立有效的连接。必须指定
      keyspace-name: store  # 键空间名
      username: 你的账号
      password: 你的密码

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows上安装Docker可以按照以下步骤进行操作: 1. 首先,确保你的Windows版本是Windows 10或者Windows Server 2016以上,并且开启了Hyper-V虚拟化功能。 2. 前往Docker官网(https://www.docker.com/)下载Docker Desktop for Windows安装程序。 3. 运行安装程序,按照提示进行安装安装完成后,Docker会自动启动。 4. 在系统托盘中找到Docker图标,右键点击选择"Settings",进入设置界面。 5. 在设置界面中,可以配置Docker的一些选项,例如镜像加速器、资源限制等。根据需要进行配置。 6. 完成配置后,点击"Apply & Restart"按钮,Docker会重新启动应用配置。 至此,你已经成功在Windows上安装Docker。 接下来,如果你想在Docker安装数据库,可以按照以下步骤进行操作: 1. 打开Docker Desktop应用,确保Docker已经启动。 2. 在命令行或者终端中输入以下命令来搜索并下载数据库Docker镜像: ``` docker search 数据库名称 ``` 3. 选择一个合适的镜像,并使用以下命令来下载该镜像: ``` docker pull 镜像名称 ``` 4. 下载完成后,使用以下命令来创建并运行一个数据库容器: ``` docker run --name 容器名称 -e 环境变量 镜像名称 ``` 其中,容器名称是你给容器起的一个名字,环境变量是数据库的配置信息,镜像名称是你下载的数据库镜像的名称。 5. 容器创建成功后,你可以使用以下命令来管理容器: - 启动容器:`docker start 容器名称` - 停止容器:`docker stop 容器名称` - 进入容器:`docker exec -it 容器名称 bash` 通过以上步骤,你可以在Docker中成功安装和运行数据库

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值