基于Spring框架和Linux Shell的实用实例
以下是基于Spring框架和Linux Shell的实用实例,涵盖开发、部署、运维等场景:
环境检查与依赖管理
`shell
检查Java和Spring Boot版本
java -version spring version
列出项目Maven依赖
mvn dependency:tree > dependencies.txt
检查Spring应用运行端口
netstat -tulnp | grep java `
项目构建与打包
`shell
使用Maven清理并打包Spring Boot项目
mvn clean package
构建Docker镜像(需Dockerfile)
docker build -t spring-app .
Gradle构建Spring项目
gradle build `
应用部署与启动
`shell
后台启动Spring Boot Jar
nohup java -jar app.jar > app.log 2>&1 &
带特定Profile启动
java -jar app.jar --spring.profiles.active=prod
Kubernetes部署Spring应用
kubectl apply -f spring-deployment.yaml `
日志监控
`shell
实时查看Spring Boot日志
tail -f /var/log/spring-app.log
按错误级别过滤日志
grep "ERROR" application.log
日志文件按日期分割
logrotate -f /etc/logrotate.d/spring-app `
数据库操作
`shell
导出Spring应用使用的MySQL数据
mysqldump -u user -p dbname > spring_db_backup.sql
使用Flyway迁移数据库
mvn flyway:migrate
检查Redis连接(Spring Data Redis)
redis-cli ping `
性能监控
`shell
查看Spring应用进程资源占用
top -p $(pgrep -f app.jar)
生成线程转储
jstack -l $(pgrep -f app.jar) > thread_dump.txt
监控HTTP请求响应时间
watch -n 1 "curl -o /dev/null -s -w '%{time_total}s\n' http://localhost:8080/actuator/health" `
配置管理
`shell
热更新Spring配置(需Actuator)
curl -X POST http://localhost:8080/actuator/refresh
加密配置中的敏感信息
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="secret" password=mykey algorithm=PBEWithMD5AndDES
检查生效的配置项
curl http://localhost:8080/actuator/configprops | jq `
测试验证
`shell
运行Spring单元测试
mvn test
发送测试HTTP请求
curl -X GET "http://localhost:8080/api/users" -H "Authorization: Bearer token"
压力测试Spring端点
ab -n 1000 -c 10 http://localhost:8080/api/resource `
安全相关
`shell
生成SSL证书(Spring HTTPS)
keytool -genkey -alias tomcat -keyalg RSA -keystore keystore.p12 -validity 3650
检查暴露的Actuator端点
curl -s http://localhost:8080/actuator | jq '_links'
验证CSRF保护
curl -X POST -H "X-XSRF-TOKEN: value" http://localhost:8080/api `
维护与排错
`shell
优雅关闭Spring应用
kill -15 $(pgrep -f app.jar)
检查Bean加载情况
curl http://localhost:8080/actuator/beans | jq
分析堆内存
jmap -heap $(pgrep -f app.jar) `
这些示例覆盖了Spring应用生命周期中的常见操作,可根据实际需求组合使用或调整参数。注意部分命令需要根据具体环境修改路径、端口等参数。
基于Spring框架监控AI/人工智能系统的实用
以下是基于Spring框架监控AI/人工智能系统的实用场景和代码示例,涵盖性能、日志、健康检查、分布式追踪等多个维度:
监控AI模型性能指标
使用Spring Boot Actuator暴露模型推理延迟和吞吐量:
@RestController
public class ModelMonitorController {
@Autowired
private MeterRegistry meterRegistry;
@PostMapping("/predict")
public Prediction predict(@RequestBody InputData data) {
Timer.Sample sample = Timer.start(meterRegistry);
Prediction result = aiModel.predict(data);
sample.stop(meterRegistry.timer("model.latency", "model", "nlp"));
return result;
}
}
Prometheus配置采集自定义指标:
# application.yml
management:
endpoints:
web:
exposure:
include: health,prometheus,metrics
metrics:
export:
prometheus:
enabled: true
分布式追踪AI流水线
集成Sleuth+Zipkin追踪跨服务AI调用:
@FeignClient(name = "cv-service")
public interface CVModelClient {
@PostMapping("/detect")
@SleuthSpan
DetectionResult detect(@RequestBody ImageInput input);
}
日志中自动注入TraceID:
2023-07-20 14:00:00 [INFO ] [cv-service,5c8526df3f47e214,5c8526df3f47e214] Model inference completed in 45ms
模型健康检查端点
自定义健康检查指示器:
@Component
public class ModelHealthIndicator implements HealthIndicator {
@Override
public Health health() {
boolean isHealthy = checkModelStatus();
return isHealthy ?
Health.up().withDetail("version", "2.1.0").build() :
Health.down().withDetail("error", "GPU memory overflow").build();
}
}
异常监控与告警
AOP拦截AI异常并触发告警:
@Aspect
@