温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • 使用Stargate访问K8ssandra的过程中如何利用Springboot整合Cassandra

使用Stargate访问K8ssandra的过程中如何利用Springboot整合Cassandra

发布时间:2021-10-08 09:41:46 来源:亿速云 阅读:131 作者:柒染 栏目:开发技术

使用Stargate访问K8ssandra的过程中如何利用Springboot整合Cassandra,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

    1 简介

    之前我们在文章《K8ssandra入门-详细记录在Linux上部署K8ssandra到Kubernetes》成功地在Ubuntu上安装了K8ssandra,现在我们来看看如何访问Cassandra。

    K8ssandra的组件Stargate提供了多种方式的数据访问,对应端口如下:

    • 8080:GraphQL interface

    • 8081:REST Auth

    • 8082:REST interface

    • 9042:CQL service

    我们使用最常用的9042端口,其它请参考官方文档。

    2 三种方式访问

    先暴露服务,然后找到对应的端口:

    $ kubectl expose deployment k8ssandra-dc1-stargate --type=NodePort --name=stargate-out
    $ kubectl get svc stargate-out

    2.1 cqlsh命令

    安装clqsh命令:

    $ pip install cqlsh

    连接数据库:

    cqlsh -u k8ssandra-superuser -p YMEbXcPCW9xxxxxxx 127.0.0.1 30703

    接着进行数据操作:

    CREATE KEYSPACE pkslow  WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
    
    use pkslow;
     
    CREATE TABLE users (username text primary key, password text, email text);
     
    INSERT INTO users (username, password, email) values ('larry', 'larry123', 'larry@pkslow.com');
    INSERT INTO users (username, password, email) values ('admin', '123456', 'admin@pkslow.com');
    INSERT INTO users (username, password, email) values ('carol', '123456', 'carol@pkslow.com');
    INSERT INTO users (username, password, email) values ('david', '123456', 'david@pkslow.com');

    写入了数据后,我们查询看看:

    使用Stargate访问K8ssandra的过程中如何利用Springboot整合Cassandra

    2.2 用IDEA连接

    配置数据库,选择Cassandra,连接信息如下:

    使用Stargate访问K8ssandra的过程中如何利用Springboot整合Cassandra

    接着就可以查看相关的数据了,如下:

    使用Stargate访问K8ssandra的过程中如何利用Springboot整合Cassandra

    2.3 通过Java程序访问

    引入依赖如下:

    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-cassandra</artifactId>
      <version>3.2.5</version>
    </dependency>

    准备实体类:

    package com.pkslow.springboot.cassandra.entity;
    
    import org.springframework.data.annotation.Id;
    import org.springframework.data.cassandra.core.mapping.Table;
    
    @Table(value = "users")
    public class User {
        @Id
        private String username;
        private String password;
        private String email;
    }

    Reposity类:

    package com.pkslow.springboot.cassandra.repository;
    
    import com.pkslow.springboot.cassandra.entity.User;
    import org.springframework.data.cassandra.repository.CassandraRepository;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface UserRepository extends CassandraRepository<User, String> {
    }

    同时需要在配置类中加上:

    @EnableCassandraRepositories(basePackages = "com.pkslow.springboot.cassandra.repository")

    配置一下数据库连接属性:

    server.port=8080spring.data.cassandra.contact-points=8.134.124.38:30703spring.data.cassandra.username=k8ssandra-superuserspring.data.cassandra.password=YMEbXcPCW9xrfxxxxxspring.data.cassandra.local-datacenter=dc1spring.data.cassandra.keyspace-name=pkslow

    这样就基本可以了。

    启动程序,访问测试如下:

    使用Stargate访问K8ssandra的过程中如何利用Springboot整合Cassandra

    代码请查看:https://github.com/LarryDpk/pkslow-samples

    看完上述内容,你们掌握使用Stargate访问K8ssandra的过程中如何利用Springboot整合Cassandra的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI