在Spring Boot Admin中显示日志文件,可以通过以下步骤实现:
@EnableAdminServer
注解启用Spring Boot Admin,并配置AdminServerProperties
的log-file
属性为日志文件的路径和名称:import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration;
import org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
import org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler;
import org.springframework.jmx.export.assembler.MBeanInfoAssembler;
import org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler;
import org.springframework.jmx.export.naming.MetadataNamingStrategy;
import org.springframework.jmx.export.naming.ObjectNamingStrategy;
import org.springframework.jmx.export.naming.SelfNaming;
import org.springframework.jmx.support.ConnectorServerFactoryBean;
import org.springframework.jmx.support.MBeanServerFactoryBean;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.util.StringUtils;import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@Configuration
@EnableConfigurationProperties({ JmxProperties.class, SpringApplicationAdminJmxProperties.class })
@ConditionalOnClass({ MBeanServer.class, AnnotationMBeanExporter.class })
@ConditionalOnProperty(prefix = "spring.application.admin", name = "enabled", matchIfMissing = true)
@Import(JmxAutoConfiguration.class)
public class SpringBootAdminConfig {
@Bean
@Primary
@ConditionalOnMissingBean(MBeanServer.class)
public MBeanServer mBeanServer() {
MBeanServerFactoryBean factory = new MBeanServerFactoryBean();
factory.setLocateExistingServerIfPossible(true);
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
@ConditionalOnMissingBean
public ObjectNamingStrategy objectNamingStrategy() {
MetadataNamingStrategy strategy = new MetadataNamingStrategy(mBeanServer());
strategy.setDefaultDomain("org.springframework.boot");
return strategy;
}
@Bean
@ConditionalOnMissingBean
public MBeanInfoAssembler mBeanInfoAssembler() {
return new SimpleReflectiveMBeanInfoAssembler();
}
@Bean
@ConditionalOnMissingBean
public AnnotationMBeanExporter mbeanExporter(ObjectNamingStrategy namingStrategy,
MBeanInfoAssembler assembler) {
AnnotationMBeanExporter exporter = new AnnotationMBeanExporter();
exporter.setNamingStrategy(namingStrategy);
exporter.setAssembler(assembler);
exporter.setRegistrationPolicy(RegistrationPolicy.IGNORE_EXISTING);
return exporter;
}
@Bean
@ConditionalOnMissingBean
public ConnectorServerFactoryBean connectorServerFactoryBean(JmxProperties properties) {
ConnectorServerFactoryBean factoryBean = new ConnectorServerFactoryBean();
factoryBean.setServiceUrl(properties.getConnector());
return factoryBean;
}
@Bean
@ConditionalOnMissingBean
public SpringApplicationAdminJmxAutoConfiguration springApplicationAdminJmxAutoConfiguration(
JmxProperties jmxProperties, SpringApplicationAdminJmxProperties adminJmxProperties,
ResourceLoader resourceLoader, Environment environment) throws IOException {
SpringApplicationAdminJmxAutoConfiguration configuration = new SpringApplicationAdminJmxAutoConfiguration(
jmxProperties, adminJmxProperties, resourceLoader, environment);
configuration.setLoggingSystem(LoggingSystem.get(environment.getClassLoader()));
return configuration;
}
@Bean
@Order(Ordered.LOWEST_PRECEDENCE)
public SelfNaming selfNaming() {
return new SelfNaming() {
@Override
public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
Map<String, String> properties = new HashMap<>();
properties.put("type", "AdminApplication");
properties.put("name", "Spring Boot Admin");
properties.put("instance", getInstanceId());
properties.put("context", getContextPath());
properties.put("host", getHostName());
properties.put("port", getPort());
return ObjectNameManager.getInstance(properties);
}
private String getInstanceId() {
return System.getProperty("spring.application.admin.instance-id", "unknown");
}
private String getContextPath() {
return System.getProperty("server.servlet.context-path", "/");
}
private String getHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException ex) {
return "unknown";
}
}
private String getPort() {
return System.getProperty("server.port", "unknown");
}
};
}
private static final class ObjectNameManager {
private static final String DOMAIN = "org.springframework.boot";
private static final String KEY_PROPERTY = "key";
private static final String DEFAULT_KEY = "default";
private static ObjectName getInstance(Map<String, String> properties) throws MalformedObjectNameException {
Properties props = new Properties();
props.putAll(properties);
String domain = props.getProperty("domain", DOMAIN);
String key = props.getProperty(KEY_PROPERTY, DEFAULT_KEY);
props.remove("domain");
props.remove(KEY_PROPERTY);
String name = String.format("%s:type=%s,%s", domain, key,
StringUtils.collectionToDelimitedString(props.entrySet(), ","));
return ObjectName.getInstance(name);
}
}
}
@EnableAdminServer
注解,启用Spring Boot Admin服务器功能:import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import de.codecentric.boot.admin.server.config.EnableAdminServer;@SpringBootApplication
@EnableAdminServer
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
http://localhost:8080
),可以在应用的详情页面中查看和下载日志文件。注意:以上步骤是基于Spring Boot Admin 2.5.1版本的配置,具体步骤可能会因版本不同而有所变化。另外,以上配置只适用于显示单个应用的日志文件,如果需要显示多个应用的日志文件,可以参考Spring Boot Admin的文档进行配置。
领取专属 10元无门槛券
手把手带您无忧上云