springboot学习9之admin监控配置

     项目在服务器上运行,对服务器或容器进行监控是必不可少的。spring-boot-admin提供了一套对容器进行监控的框架。在springboot上面配置非常简单。

     先直接配置个例子跑起来看看效果。

    服务端

pom.xml


		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			de.codecentric
			spring-boot-admin-starter-client
			1.5.0
		
		
			de.codecentric
			spring-boot-admin-server
			1.5.0
		
		
			de.codecentric
			spring-boot-admin-server-ui
			1.5.0
		
	 
application.yml

logging:
  config: classpath:logback.xml
  path: d:/logs
server:
  port: 80
  session-timeout: 60

spring:
    application:
        name: monitor-sba
    boot:
        admin:
            url: http://localhost:${server.port}
            
management:
     security:
        enabled: false

info:
   app:
       name: "@project.name@"  #从pom.xml中获取,pom.xml中需build中加入插件spring-boot-maven-plugin
       description: "@project.description@" 
       version: "@project.version@"
       spring-boot-version: "@project.parent.version@"            
  management.security.enabled设置为false,必须的

启动类Application.java

package com.fei.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer;

import de.codecentric.boot.admin.config.EnableAdminServer;

@SpringBootApplication
@EnableAdminServer  //激活sba
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{

	 @Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
        return application.sources(Application.class);  
    }  

	 
	 public static void main(String[] args) throws Exception {
	        SpringApplication.run(Application.class, args);
	    }

	public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
	//	configurableEmbeddedServletContainer.setPort(9090);
	}
}
@EnableAdminServer不可缺少。服务端配置就这样OK了,还对自己进行了监控。启动,然后浏览器访问http://localhost

springboot学习9之admin监控配置_第1张图片

点击details

springboot学习9之admin监控配置_第2张图片

监控的信息还是能满足一般需求的。就不每个监控页面都贴图了。


    客户端(被监控的服务器)

   pom.xml


	org.springframework.boot
	spring-boot-starter-parent
	1.5.2.RELEASE

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			de.codecentric
			spring-boot-admin-starter-client
			1.5.0
		

	 

application.yml

logging:
  config: classpath:logback.xml
  path: d:/logs
server:
  port: 8081
  session-timeout: 60

spring:
    boot:
        admin:
           client:
              prefer-ip: true  #解决window下无法识别主机名的问题
           url: http://localhost   #服务端的地址

management:
     security:
        enabled: false

info:
   app:
       name: "@project.name@"  #从pom.xml中获取,pom.xml中需build中加入插件spring-boot-maven-plugin
       description: "@project.description@" 
       version: "@project.version@"
       spring-boot-version: "@project.parent.version@"

启动类Application.java

package com.fei.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer;


@SpringBootApplication
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{

	 @Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
        return application.sources(Application.class);  
    }  

	 
	 public static void main(String[] args) throws Exception {
	        SpringApplication.run(Application.class, args);
	    }

	public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
	//	configurableEmbeddedServletContainer.setPort(9090);
	}
}

启动后,监控页面就看到它了

springboot学习9之admin监控配置_第3张图片



源码



你可能感兴趣的:(spring,boot)