springCloud监控中心-springbootAdmin
简介
Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。
主要功能
熔断器信息,服务http请求链接信息,服务配置项信息(@ConfigurationProperties),系统状态信息(内存、线程、运行时间等),服务注册状态,系统配置变量信息,服务运行状态管理, 服务注册bean信息,日志级别管理,服务启用的组件信息,服务线程堆栈信息,下载hprof文件,健康检测,返回服务当前状态[UP || DOWN]。
Spring Boot Admin 是由服务端和客户端组成
springbootAdmin 和Eureka结合使用,所有服务注册到Eureka中,springbootAdmin连接到Eureka拉取各个服务信息。
springbootAdmin server 搭建
首先搭建Eureka-server博客地址:
项目结构下图:
父级工程pom.xml文件:
<?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">
<modelVersion>4.0.0</modelVersion>
<!--spring-boot-starter-parent的jar包和版本定义在父级工程pom文件里-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.wl</groupId>
<artifactId>spring-cloud-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-cloud-demo</name>
<!--子级工程引入父级工程-->
<modules>
<module>eureka-server-demo</module>
<module>spring-boot-admin-demo</module>
</modules>
<!--所依赖的jar包版本统一管理-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<motan.version>0.3.0</motan.version>
<spring.cloud.version>2.1.1.RELEASE</spring.cloud.version>
<spring.admin.version>2.1.4</spring.admin.version>
<spring.boot.start.version>2.1.4.RELEASE</spring.boot.start.version>
<maven.jar.plugin>2.6</maven.jar.plugin>
<java.version>1.8</java.version>
<gson.version>2.8.5</gson.version>
</properties>
<build>
</build>
</project>
spring-boot-admin-demo的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>com.wl</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-admin-demo</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring.admin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.start.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring.boot.start.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring.boot.start.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven.jar.plugin}</version>
<configuration>
<archive>
<!-- 添加index则不从mainfest中读取classpath,而是从Index.list中读取 -->
<!-- <index>true</index> -->
<manifest>
<!-- 作为程序入口来执行的class -->
<mainClass>com.wl.admin.SpringbootAdminServerApplication</mainClass>
<!-- 是否将依赖的classpath一起打包 -->
<addClasspath>true</addClasspath>
<!-- 依赖的classpath的前缀,也就是打包后生成的MANIFEST.MF文件里,引入的jar文件都会加上前缀。lib/,比如fastjson-1.2.7.jar,在mainfest文件里就会是lib/fastjson-1.2.7.jar -->
<classpathPrefix>lib/</classpathPrefix>
<!--<classpathLayoutType>custom</classpathLayoutType> <customClasspathLayout>
lib/$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension} </customClasspathLayout> -->
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<!-- 打包排除的文件 -->
<excludes>
<exclude>config/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven.jar.plugin}</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.zczy.generator.EntityGenerator</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
SpringbootAdminServerApplication类:
package com.wl.admin;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
/**
* spring-admin spring-boot监控启动类
* @author wl
* @description TODO
* @date 2019年4月30日 上午11:09:34
*/
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@EnableDiscoveryClient
@SpringBootApplication
public class SpringbootAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminServerApplication.class, args);
}
/**
* 不开启权限认证
*/
@Profile("insecure")
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()//
.and().csrf().disable();
}
}
/**
* 开启权限认证
*/
@Profile("secure")
@Configuration
public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
http.authorizeRequests()
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and()
.csrf().disable();
}
}
}
application.properties 配置:
#程序名称
spring.application.name=spring-boot-admin-demo
#端口号
server.port = 8099
#配置是否开启权限认证标识
spring.profiles.active=secure
spring.profiles.include=secure
#转化为json格式
spring.jackson.serialization.indent_output=true
#代表打开所有的监控点
management.endpoints.web.exposure.include=*
# 代表启用单独的url地址来监控 Spring Boot 应用,
management.endpoints.web.base-path=/
#开启任何访问
management.endpoint.health.show-details=always
#开启日志
management.endpoints.logfile.enabled=true
#是否开启安全验证 true:false
sping.management.security.enabled=false
#登录账号
spring.security.user.name=xxxxx
#登录密码
spring.security.user.password=12345
spring.security.basic.enabled=false
spring.endpoints.health.sensitive=true
#网页访问地址
spring.boot.admin.url=http://localhost:8099
#spring.boot.admin.turbine.location=spring-admin
#spring.boot.admin.turbine.clusters=default
#spring.boot.admin.turbine.enabled=true
#spring.boot.admin.routes.endpoints=env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream,turbine.stream
#turbine.appConfig=spring-client
#turbine.combineHostPort=true
#turbine.clusterNameExpression=new String("default")
#spring.boot.admin.turbine.url=/turbine.stream
#eureka.instance.leaseRenewalIntervalInSeconds= 10
#eureka.instance.health-check-url-path=/actuator/health
#eureka.client.registryFetchIntervalSeconds=5
#注册到eureka地址
eureka.client.serviceUrl.defaultZone=http://192.168.7.66:9020/eureka/
#turbine地址
spring.boot.admin.turbine.url: http://localhost:8099/turbine.stream
spring.boot.admin.turbine.clusters: PROVIDER-SERVICE,RIBBON-SERVICE,FEIGN-SERVICE
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.health-check-url-path=/actuator/health
eureka.client.registry-fetch-interval-seconds=5
编译程序:
###启动程序,访问http://localhost:8099
客户端接入
客户端example-service-demo结构:
example-service-demo的pom.xml:
<?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>com.wl</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>example-service-demo</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- hystrix和hystrix监控相关依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.swagger2.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.swagger2.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<!-- 添加index则不从mainfest中读取classpath,而是从Index.list中读取 -->
<!-- <index>true</index> -->
<manifest>
<!-- 作为程序入口来执行的class -->
<mainClass>com.wl.demoGoodsServiceApplication</mainClass>
<!-- 是否将依赖的classpath一起打包 -->
<addClasspath>true</addClasspath>
<!-- 依赖的classpath的前缀,也就是打包后生成的MANIFEST.MF文件里,引入的jar文件都会加上前缀。lib/,比如fastjson-1.2.7.jar,在mainfest文件里就会是lib/fastjson-1.2.7.jar -->
<classpathPrefix>lib/</classpathPrefix>
<!--<classpathLayoutType>custom</classpathLayoutType> <customClasspathLayout>
lib/$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension} </customClasspathLayout> -->
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<!-- 打包排除的文件 -->
<excludes>
<exclude>config/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.zczy.generator.EntityGenerator</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
application.properties的配置:
##\u5de5\u7a0b\u540d\u79f0
spring.application.name=example-service-demo
server.port=8088
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
eureka.client.serviceUrl.defaultZone= http://127.0.0.1:9020/eureka/
management.endpoint.health.show-details = always
management.endpoints.web.exposure.include = *
##tomcat\u6027\u80fd\u8c03\u4f18
##\u961f\u5217\u6570
server.tomcat.accept-count=500
#\u6700\u5927\u7ebf\u7a0b\u6570
server.tomcat.max-threads=500
#\u6700\u5927\u8fde\u63a5\u6570
server.tomcat.max-connections=10000
启动类demoGoodsServiceApplication:
package com.wl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class demoGoodsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(demoGoodsServiceApplication.class, args);
}
}
启动程序
查看Eureka 注册中心可以看到注册到注册中心的服务
访问springbootAdmin:http://localhost:8099
输入账号密码进入主页面
查看example-service-demo 服务:
使用swagger-ui 进行接口访问
后续会写swagger的使用博客查看监控品台界面可以看到请求和响应信息:
查看实时日志:
git上源码位置:
git源码地址:https://github.com/wenlin1/spring-cloud-demo