feat(command-pro): 增加一个命令行工具用于直接操作hadoop等组件

基于微服务的命令行适合日常产品化运维操作,但能够直接操作Hadoop等组件,便于开发测试使用,因此增加一个模块用于开发过程中测试使用
This commit is contained in:
v-zhangjc9
2024-05-10 16:22:49 +08:00
parent d46cd5697c
commit 835cc6729b
17 changed files with 586 additions and 162 deletions

View File

@@ -0,0 +1,43 @@
package com.lanyuanxiaoyao.service.command.pro;
import com.lanyuanxiaoyao.service.configuration.SecurityConfig;
import com.lanyuanxiaoyao.service.forest.configuration.SpringCloudDiscoveryInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
/**
* 命令行工具入口
*
* @author ZhangJiacheng
* @date 2022-03-24
*/
@SpringBootApplication
@EnableConfigurationProperties
@ComponentScan(
basePackages = {"com.lanyuanxiaoyao.service"},
excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
SpringCloudDiscoveryInterceptor.class,
SecurityConfig.class
}),
}
)
public class CommandProApplication implements ApplicationRunner {
private static final Logger logger = LoggerFactory.getLogger(CommandProApplication.class);
public static void main(String[] args) {
SpringApplication.run(CommandProApplication.class, args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
// Test
}
}

View File

@@ -0,0 +1,21 @@
package com.lanyuanxiaoyao.service.command.pro.commands;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
/**
* Hudi相关操作
*
* @author lanyuanxiaoyao
* @date 2024-03-19
*/
@ShellComponent("Hudi相关操作")
public class HudiCommand {
private static final Logger logger = LoggerFactory.getLogger(HudiCommand.class);
@ShellMethod("Test")
public void test() {
}
}

View File

@@ -0,0 +1,19 @@
package com.lanyuanxiaoyao.service.command.pro.configuration;
import org.jline.utils.AttributedString;
import org.springframework.context.annotation.Configuration;
import org.springframework.shell.jline.PromptProvider;
/**
* Spring Shell 配置
*
* @author ZhangJiacheng
* @date 2022-04-27
*/
@Configuration
public class ShellConfiguration implements PromptProvider {
@Override
public AttributedString getPrompt() {
return new AttributedString("hudi-pro:->");
}
}

View File

@@ -0,0 +1,57 @@
package com.lanyuanxiaoyao.service.command.pro.configuration;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.dtflys.forest.auth.BasicAuth;
import com.dtflys.forest.http.ForestAddress;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.interceptor.Interceptor;
import com.lanyuanxiaoyao.service.common.Constants;
import java.net.URI;
import java.net.URL;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
/**
* @author lanyuanxiaoyao
* @date 2023-04-24
*/
@Component
public class SpringCloudDiscoveryInterceptor implements Interceptor<Object> {
private static final Logger logger = LoggerFactory.getLogger(SpringCloudDiscoveryInterceptor.class);
private final DiscoveryClient discoveryClient;
public SpringCloudDiscoveryInterceptor(DiscoveryClient discoveryClient) {
logger.info("Services: {}", discoveryClient.getServices());
this.discoveryClient = discoveryClient;
}
@Override
public boolean beforeExecute(ForestRequest request) {
// Load
URL url = URLUtil.url(request.getUrl());
String host = url.getHost();
if (StrUtil.isNotBlank(host)) {
ImmutableList<String> urls = Lists.immutable.ofAll(discoveryClient.getInstances(host))
.collect(instance -> instance.getUri().toString());
if (ObjectUtil.isNotEmpty(urls)) {
String targetUrl = urls.get(RandomUtil.randomInt(urls.size()));
URI uri = URI.create(targetUrl);
request.setAddress(new ForestAddress(uri.getScheme(), uri.getHost(), uri.getPort()));
}
}
// Basic auth
BasicAuth basicAuth = new BasicAuth(Constants.SPRING_SECURITY_USERNAME, Constants.SPRING_SECURITY_PASSWORD_PLAIN);
basicAuth.enhanceAuthorization(request);
return Interceptor.super.beforeExecute(request);
}
}

View File

@@ -0,0 +1,20 @@
spring:
application:
name: service-command-pro
profiles:
include: common,discovery
main:
web-application-type: none
shell:
interactive:
enabled: true
command:
script:
enabled: false
history:
enabled: false
forest:
backend: httpclient
timeout: 120000
log-enabled: false
interceptors: com.lanyuanxiaoyao.service.command.pro.configuration.SpringCloudDiscoveryInterceptor