spring-boot入门工程之j360-boot:(欢迎star、fork)java
https://github.com/xuminwlt/j360-bootgit
spring-boot官方地址github
http://projects.spring.io/spring-boot/web
【j360-boot】Spring-boot系列一(多是最好的quick start)redis
【j360-boot】Spring-boot系列二(困难模式,比简单复杂那么一点点)
spring
【j360-boot】Spring-boot系列三(崩溃模式,不是你崩就是电脑崩)docker
【j360-boot】Spring-boot系列四(运维福利,监控和管理生产环境)
shell
【j360-boot】Spring-boot系列五(docker、docker、docker)浏览器
Spring Boot包含不少其余的特性,它们能够帮你监控和管理发布到生产环境的应用。你能够选择使用HTTP端点,JMX或远程shell(SSH或Telnet)来管理和监控应用。审计Auditing),健康(health)和数据采集(metrics gathering)会自动应用到你的应用。安全
spring-boot-actuator模块提供了Spring Boot全部的production-ready特性。启用该特性的最简单方式就是添加对spring-bootstarter-actuator ‘Starter POM’的依赖。
定义:执行器是一个制造业术语,指的是用于移动或控制东西的一个机械装置。一个很小的改变就能让执行器产生大量的运动。
基于Maven的项目想要添加执行器只需添加下面的'starter'依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
对于Gradle,使用下面的声明:
dependencies { compile("org.springframework.boot:spring-boot-starter-actuator") }
执行器端点容许你监控应用及与应用进行交互。Spring Boot包含不少内置的端点,你也能够添加本身的。例如,health端点提供了应用的基本健康信息。
端点暴露的方式取决于你采用的技术类型。大部分应用选择HTTP监控,端点的ID映射到一个URL。例如,默认状况下,health端点将被映射到/health。
下面的端点都是可用的:
ID 描述 敏感(Sensitive) autoconfig 显示一个auto-configuration的报告,该报告展现全部auto-configuration候选者及它们被应用或未被应用的缘由 true beans 显示一个应用中全部Spring Beans的完整列表 true configprops 显示一个全部@ConfigurationProperties的整理列表 true dump 执行一个线程转储 true env 暴露来自Spring ConfigurableEnvironment的属性 true health 展现应用的健康信息(当使用一个未认证链接访问时显示一个简单的'status',使用认证链接访问则显示所有信息详情) false info 显示任意的应用信息 false metrics 展现当前应用的'指标'信息 true mappings 显示一个全部@RequestMapping路径的整理列表 true shutdown 容许应用以优雅的方式关闭(默认状况下不启用) true trace 显示trace信息(默认为最新的一些HTTP请求) true 注:根据一个端点暴露的方式,sensitive参数可能会被用作一个安全提示。例如,在使用HTTP访问sensitive端点时须要提供用户名/密码(若是没有启用web安全,可能会简化为禁止访问该端点)。
启用该执行器的工程 ->浏览器输入:
http://localhost:8080/autoconfig
使用Spring属性能够自定义端点。你能够设置端点是否开启(enabled),是否敏感(sensitive),甚至它的id。例如,下面的application.properties改变了敏感性和beans端点的id,也启用了shutdown。
endpoints.beans.id=springbeans endpoints.beans.sensitive=false endpoints.shutdown.enabled=true
注:前缀 endpoints + . + name 被用来惟一的标识被配置的端点。
默认状况下,除了shutdown外的全部端点都是启用的。若是但愿指定选择端点的启用,你可使用endpoints.enabled属性。
例如,下面的配置禁用了除info外的全部端点:
endpoints.enabled=false endpoints.info.enabled=true
若是你正在开发一个Spring MVC应用,Spring Boot执行器自动将全部启用的端点经过HTTP暴露出去。默认约定使用端点的id做为URL路径,例如,health暴露为/health。
若是你的项目中添加的有Spring Security,全部经过HTTP暴露的敏感端点都会受到保护。默认状况下会使用基本认证(basic authentication,用户名为user,密码为应用启动时在控制台打印的密码)。
你可使用Spring属性改变用户名,密码和访问端点须要的安全角色。例如,你可能会在application.properties中添加下列配置:
security.user.name=admin security.user.password=secret management.security.role=SUPERUSER
注:若是你不使用Spring Security,那你的HTTP端点就被公开暴露,你应该慎重考虑启用哪些端点。
有时候将全部的管理端口划分到一个路径下是有用的。例如,你的应用可能已经将 /info 做为他用。你能够用 management.contextPath 属性为管理端口设置一个前缀:
management.context-path=/manage
上面的application.properties示例将把端口从 /{id} 改成 /manage/{id} (好比,/manage/info)。
对于基于云的部署,使用默认的HTTP端口暴露管理端点(endpoints)是明智的选择。然而,若是你的应用是在本身的数据中心运行,那你可能倾向于使用一个不一样的HTTP端口来暴露端点。
management.port 属性能够用来改变HTTP端口:
management.port=8081
因为你的管理端口常常被防火墙保护,不对外暴露也就不须要保护管理端点,即便你的主要应用是安全的。在这种状况下,classpath下会存在Spring Security库,你能够设置下面的属性来禁用安全管理策略(management security):
management.security.enabled=false
(若是classpath下不存在Spring Security,那也就不须要显示的以这种方式来禁用安全管理策略,它甚至可能会破坏应用程序。)
你能够经过设置 management.address 属性来定义管理端点可使用的地址。这在你只想监听内部或面向生产环境的网络,或
只监听来自localhost的链接时很是有用。
下面的application.properties示例不容许远程管理链接:
management.port=8081 management.address=127.0.0.1
若是不想经过HTTP暴露端点,你能够将管理端口设置为-1: management.port=-1
Java管理扩展(JMX)提供了一种标准的监控和管理应用的机制。默认状况下,Spring Boot在 org.springframework.boot 域下将管理端点暴露为JMX MBeans。
Spring Boot支持集成一个称为'CRaSH'的Java shell。你能够在CRaSH中使用ssh或telnet命令链接到运行的应用。为了启用远程shell支持,你只需添加 spring-boot-starter-remote-shell 的依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-remote-shell</artifactId> </dependency>
Spring Boot执行器包括一个支持'gauge'和'counter'级别的度量指标服务。'gauge'记录一个单一值;'counter'记录一个增量(增长或减小)。同时,Spring Boot提供一个PublicMetrics接口,你能够实现它,从而暴露以上两种机制不能记录的指标。
能够看到基本的 memory , heap , class loading , processor 和 thread pool 信息,连同一些HTTP指标。在该实例中, root ('/'), /metrics URLs分别返回20次,3次 HTTP 200 响应。同时能够看到 root URL返回了4次 HTTP401 (unauthorized)响应。双asterix(star-star)来自于被Spring MVC /** 匹配到的一个请求(一般为一个静态资源)。
gauge 级别展现了一个请求的最后响应时间。因此, root 的最后请求被响应耗时2毫秒, /metrics 耗时3毫秒。
Spring Boot暴露如下系统指标:
系统内存总量(mem),单位:Kb
空闲内存数量(mem.free),单位:Kb
处理器数量(processors)
系统正常运行时间(uptime),单位:毫秒
应用上下文(就是一个应用实例)正常运行时间(instance.uptime),单位:毫秒
系统平均负载(systemload.average)
堆信息(heap,heap.committed,heap.init,heap.used),单位:Kb
线程信息(threads,thread.peak,thead.daemon)
类加载信息(classes,classes.loaded,classes.unloaded)
垃圾收集信息(gc.xxx.count, gc.xxx.time)
最大链接数(datasource.xxx.max)
最小链接数(datasource.xxx.min)
活动链接数(datasource.xxx.active)
链接池的使用状况(datasource.xxx.usage)
全部的数据源指标共用 datasoure. 前缀。该前缀对每一个数据源都很是合适:
若是是主数据源(惟一可用的数据源或存在的数据源中被@Primary标记的)前缀为datasource.primary
若是数据源bean名称以dataSource结尾,那前缀就是bean的名称去掉dataSource的部分(例如,batchDataSource的前缀是datasource.batch)
其余状况使用bean的名称做为前缀
若是你使用Tomcat做为内嵌的servlet容器,session指标将被自动暴露出去。 httpsessions.active 和 httpsessions.max 提供了活动的和最大的session数量。
对于全部的HTTP请求Spring Boot自动启用追踪。你能够查看 trace 端点,并获取最近一些请求的基本信息:
# ---------------------------------------- # ACTUATOR PROPERTIES # ---------------------------------------- # MANAGEMENT HTTP SERVER (ManagementServerProperties) management.port= # defaults to 'server.port' management.address= # bind to a specific NIC management.context-path= # default to '/' management.add-application-context-header= # default to true management.security.enabled=true # enable security management.security.role=ADMIN # role required to access the management endpoint management.security.sessions=stateless # session creating policy to use (always, never, if_required, stateless) # PID FILE (ApplicationPidFileWriter) spring.pidfile= # Location of the PID file to write # ENDPOINTS (AbstractEndpoint subclasses) endpoints.autoconfig.id=autoconfig endpoints.autoconfig.sensitive=true endpoints.autoconfig.enabled=true endpoints.beans.id=beans endpoints.beans.sensitive=true endpoints.beans.enabled=true endpoints.configprops.id=configprops endpoints.configprops.sensitive=true endpoints.configprops.enabled=true endpoints.configprops.keys-to-sanitize=password,secret,key # suffix or regex endpoints.dump.id=dump endpoints.dump.sensitive=true endpoints.dump.enabled=true endpoints.env.id=env endpoints.env.sensitive=true endpoints.env.enabled=true endpoints.env.keys-to-sanitize=password,secret,key # suffix or regex endpoints.health.id=health endpoints.health.sensitive=true endpoints.health.enabled=true endpoints.health.mapping.*= # mapping of health statuses to HttpStatus codes endpoints.health.time-to-live=1000 endpoints.info.id=info endpoints.info.sensitive=false endpoints.info.enabled=true endpoints.mappings.enabled=true endpoints.mappings.id=mappings endpoints.mappings.sensitive=true endpoints.metrics.id=metrics endpoints.metrics.sensitive=true endpoints.metrics.enabled=true endpoints.shutdown.id=shutdown endpoints.shutdown.sensitive=true endpoints.shutdown.enabled=false endpoints.trace.id=trace endpoints.trace.sensitive=true Spring Boot参考指南 附录A. 常见应用属性 393 endpoints.trace.enabled=true # HEALTH INDICATORS (previously health.*) management.health.db.enabled=true management.health.elasticsearch.enabled=true management.health.elasticsearch.response-timeout=100 # the time, in milliseconds, to wait for a response from the cluster management.health.diskspace.enabled=true management.health.diskspace.path=. management.health.diskspace.threshold=10485760 management.health.mongo.enabled=true management.health.rabbit.enabled=true management.health.redis.enabled=true management.health.solr.enabled=true management.health.status.order=DOWN, OUT_OF_SERVICE, UNKNOWN, UP # MVC ONLY ENDPOINTS endpoints.jolokia.path=jolokia endpoints.jolokia.sensitive=true endpoints.jolokia.enabled=true # when using Jolokia # JMX ENDPOINT (EndpointMBeanExportProperties) endpoints.jmx.enabled=true endpoints.jmx.domain= # the JMX domain, defaults to 'org.springboot' endpoints.jmx.unique-names=false endpoints.jmx.static-names= # JOLOKIA (JolokiaProperties) jolokia.config.*= # See Jolokia manual # REMOTE SHELL shell.auth=simple # jaas, key, simple, spring shell.command-refresh-interval=-1 shell.command-path-patterns= # classpath*:/commands/**, classpath*:/crash/commands/** shell.config-path-patterns= # classpath*:/crash/* shell.disabled-commands=jpa*,jdbc*,jndi* # comma-separated list of commands to disable shell.disabled-plugins=false # don't expose plugins shell.ssh.enabled= # ssh settings ... shell.ssh.key-path= shell.ssh.port= shell.telnet.enabled= # telnet settings ... shell.telnet.port= shell.auth.jaas.domain= # authentication settings ... shell.auth.key.path= shell.auth.simple.user.name= shell.auth.simple.user.password= shell.auth.spring.roles= # SENDGRID (SendGridAutoConfiguration) spring.sendgrid.username= # SendGrid account username spring.sendgrid.password= # SendGrid account password spring.sendgrid.proxy.host= # SendGrid proxy host spring.sendgrid.proxy.port= # SendGrid proxy port # GIT INFO spring.git.properties= # resource ref to generated git info properties file