其实网上有很多资料都说得很详细,但有时按照他们的例子去做了,就会出现各种各种的问题,归其原因,是因为版本的问题,所以我来贴一下完整的示例。
先来看看pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 继承了父类的依赖管理,控制兼容版本,无需自己控制 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.zgz.spring.boot</groupId>
<artifactId>admin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>1.5.7</version>
<exclusions>
<!-- 排除依赖 因为该版本比较旧,出现过NoSuchMethod的异常-->
<exclusion>
<groupId>com.netflix.servo</groupId>
<artifactId>servo-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 使用jmx 需要引入此类-->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
<version>1.5.0</version>
<scope>compile</scope>
</dependency>
<!-- 引入较新的版本 -->
<dependency>
<groupId>com.netflix.servo</groupId>
<artifactId>servo-core</artifactId>
<version>0.12.11</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
<goal>build-info</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.zgz.application.SpringBootAdminApplication</mainClass>
<addResources>false</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
再来看看application启动类:
package com.zgz.application; import de.codecentric.boot.admin.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableAutoConfiguration @EnableDiscoveryClient @EnableAdminServer public class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); } @Configuration public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().permitAll() //<1> .and().csrf().disable(); } } } // end::application-eureka[]
接着是application.yml:
spring: application: name: zgz-spring-boot-admin info: version: @project.version@ # tag::configuration-eureka[] eureka: #<1> instance: leaseRenewalIntervalInSeconds: 10 hostname: localhost client: registryFetchIntervalSeconds: 5 serviceUrl: defaultZone: http://localhost:8761/eureka/ management: endpoints: web: exposure: include: "*" #<2> # end::configuration-eureka[] endpoint: health: show-details: ALWAYS security: enabled: false endpoints: logfile: sensitive: false
最后是logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <!-- 要是需要管理日志级别,需要在文件中加入 此配置 --> <jmxConfigurator/> </configuration>
注:如果需要在Spring Boot Admin 中能输出其他监控项目的日志,则需在监控项目的application.yml指定日志文件:
logging:
file: D:JavaWebLogsSpringBootDemolog_info.log
以上的配置会将监控项目的Log,Logging,JMX模块管理起来: