Java 使用代码在运行时获取mysql、jdk、elasticsearch、redis、netty、kafka等组件的版本号

2021-12-06 13:35:13 浏览数 (1)

直接上代码

代码语言:javascript复制
    @FunctionalInterface
    interface VersionGetter {
        String getVersion();
    }

    @Slf4j
    private static class MysqlVersionGetter implements VersionGetter {
        @Override
        public String getVersion() {
            try {
                final DataSource dataSource = SpringUtil.getBean(DataSource.class);
                if (dataSource != null) {
                    final Connection connection = dataSource.getConnection();
                    return connection.getMetaData().getDatabaseProductVersion();
                }
            } catch (Exception e) {
                log.error("获取mysql版本出错", e);
            }
            return null;
        }
    }

    private static class JavaVersionGetter implements VersionGetter {
        @Override
        public String getVersion() {
            return SystemUtil.get(SystemUtil.VERSION);
        }
    }

    @Slf4j
    private static class EsVersionGetter implements VersionGetter {
        @Override
        public String getVersion() {
            try {
                final RestHighLevelClient bean = SpringUtil.getBean(RestHighLevelClient.class);
                final MainResponse info = bean.info(RequestOptions.DEFAULT);
                final MainResponse.Version version = info.getVersion();
                return version.getNumber();
            } catch (IOException e) {
                log.error("获取ES版本出错", e);
            }
            return null;
        }
    }

    @Slf4j
    private static class RedisVersionGetter implements VersionGetter {
        @Override
        public String getVersion() {

            //因我的redis服务 info命令重命名了 所以使用了lua脚本方式
            //如果没有重命名 直接调用template的info api即可
            try {
                final StringRedisTemplate redisTemplate = RedisUtil.getRedisTemplate();
                final String response = redisTemplate.execute(ServerInfoScript.getInstance(), new ArrayList<>());
                if (StrUtil.isNotBlank(response)) {
                    final String[] split = response.split("rn");
                    for (String item : split) {
                        if (StrUtil.startWith(item, "redis_version")) {
                            final String[] versionSplit = item.split(":");
                            if (versionSplit.length == 2) {
                                return versionSplit[1];
                            }
                        }
                    }
                }
            } catch (Exception e) {
                log.error("获取redis版本出错", e);
            }
            return null;
        }
    }

    private static class NettyVersionGetter implements VersionGetter {
        @Override
        public String getVersion() {
            final Map<String, Version> identify = Version.identify();
            //netty组件有很多版本 这里随便取了一个
            final Version version = identify.values().stream().findFirst().orElse(null);
            if (version != null) {
                return version.artifactVersion();
            }
            return null;
        }
    }


    private static class KafkaVersionGetter implements VersionGetter {
        @Override
        public String getVersion() {
            return AppInfoParser.getVersion();
        }
    }

    private static class HotfixVersionGetter implements VersionGetter {
        @Override
        public String getVersion() {
            File file = new File("/data/tip/hotfix/hotfix-version.txt");
            if (file.exists() && file.isFile()) {
                return FileUtil.readUtf8String(file);
            } else {
                return null;
            }
        }
    }

    private static class ServerInfoScript implements RedisScript<String> {

        private static final ServerInfoScript INSTANCE = new ServerInfoScript();

        private ServerInfoScript() {
        }

        public static ServerInfoScript getInstance() {
            return INSTANCE;
        }

        private final String cmd = "return redis.call('redisinfo','server')";

        @Override
        public String getSha1() {
            return SecureUtil.sha1(cmd);
        }

        @Override
        public Class<String> getResultType() {
            return String.class;
        }

        @Override
        public String getScriptAsString() {
            return cmd;
        }
    }

0 人点赞