spring-boot入门工程之j360-boot:(欢迎star、fork)
https://github.com/xuminwlt/j360-boot
spring-boot官方地址
http://projects.spring.io/spring-boot/
【j360-boot】Spring-boot系列一(可能是最好的quick start)
【j360-boot】Spring-boot系列二(困难模式,比简单复杂那么一点点)
【j360-boot】Spring-boot系列三(崩溃模式,不是你崩就是电脑崩)
【j360-boot】Spring-boot系列四(运维福利,监控和管理生产环境)
【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
http://localhost:8080/health
使用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