美文网首页
SpringBoot通过配置文件初始化Nebula数据库连接

SpringBoot通过配置文件初始化Nebula数据库连接

作者: AC编程 | 来源:发表于2021-09-28 09:31 被阅读0次

    一、配置属性类

    @Configuration
    @ConfigurationProperties(prefix = "nebula")
    @ConditionalOnProperty("nebula.address")
    @Data
    public class NebulaProperties {
    
        private String address;
    
        private String userName;
    
        private String passworld;
    
        private String spaceName;
    
        private int minConnsSize;
    
        private int maxConnSize;
    
        private int timeout;
    
        private int idleTime;
    }
    

    二、初始化数据库连接

    @Slf4j
    @Configuration
    @EnableConfigurationProperties({NebulaProperties.class})
    @RequiredArgsConstructor
    public class NebulaConfig {
    
        private final NebulaProperties nebulaProperties;
    
        private NebulaPool pool;
        private Session session;
        private String spaceName;
    
        // 服务一启动会执行该方法
        @PostConstruct
        public void init() {
            pool = new NebulaPool();
            session = null;
            spaceName = nebulaProperties.getSpaceName();
            log.info("执行init方法,spaceName="+spaceName);
        }
    
        @PreDestroy
        public void destroy()    {
            if (session != null) {
                session.release();
            }
            pool.close();
        }
    
        @Bean
        @ConditionalOnProperty("nebula.address")
        public Session session() {
            try {
                NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig();
                nebulaPoolConfig.setMaxConnSize(nebulaProperties.getMaxConnSize());
                pool.init(assemblyAddress(nebulaProperties.getAddress()), nebulaPoolConfig);
                session = pool.getSession(nebulaProperties.getUserName(), nebulaProperties.getPassworld(), false);
    
                //创建数据库
                dbCreate();
            } catch (Exception e) {
                log.info("address:{},username:{},password:{}",nebulaProperties.getAddress(),nebulaProperties.getUserName(),nebulaProperties.getPassworld());
                log.error("session异常:"+e.getMessage());
                e.printStackTrace();
            }
    
            return session;
        }
    
        /**
         * 使用SPACE
         * @return
         */
        public String useSpace(){
            return "USE "+spaceName+";";
        }
    
        public ResultSet execute(String ngql){
            log.info("执行execute方法,ngql="+ngql);
            ResultSet resp = null;
            try {
                resp = this.session.execute(ngql);
                if (!resp.isSucceeded()) {
                    log.error(String.format("Execute: `%s', failed: %s", ngql, resp.getErrorMessage()));
                    System.out.println(resp.getErrorMessage());
                }
            }catch (Exception e){
                e.printStackTrace();
                System.out.println(e.getMessage());
                log.error("execute异常");
                log.error(e.getMessage());
            }
            return resp;
        }
    
        private List<HostAddress> assemblyAddress(String address) {
            String[] split = address.split(",");
            List<HostAddress> addresses = new ArrayList<>(split.length);
            for (int i=0; i< split.length; i++ ) {
                String[] host = split[i].split(":");
                addresses.add(new HostAddress(host[0],Integer.valueOf(host[1])));
            }
            return addresses;
        }
    
        /**
         * 创建数据库
         */
        private void dbCreate(){
            if(StringUtils.isEmpty(spaceName)){
                log.error("配置文件没有配置SPACE_NAME,退出程序");
                System.out.println("配置文件没有配置SPACE_NAME,退出程序");
                System.exit(1);
            }
    
            String sql = packageCreateSql();
    
            //执行SQL
            this.execute(sql);
        }
    
        /**
         * 组装创建SQL
         * @return
         */
        private String packageCreateSql(){
            StringBuilder builder = new StringBuilder();
    
            String createSpaceSql = MessageFormat.format(RelationConstant.CREATE_SPACE, spaceName);
            builder.append(createSpaceSql);
            builder.append(useSpace());
    
            String createMemberTagSql = RelationConstant.CREATE_MEMBER_TAG;
            builder.append(createMemberTagSql);
    
            return builder.toString();
        }
    
    }
    

    相关文章

      网友评论

          本文标题:SpringBoot通过配置文件初始化Nebula数据库连接

          本文链接:https://www.haomeiwen.com/subject/bjfknltx.html