1、修改端點ID
每個Actuator端點都是有一個特定的ID用來決定端點的路徑。/beans端點的默認ID就是 beans 。端點的路徑是由ID決定的, 那么可以通過修改ID來改變端點的路徑。 要做的就是設(shè)置一個屬性,屬性名是 endpoints.endpoint-id.id 。
如把/beans改為/beansome:
endpoints.beans.id=beansome
這時要是想查看bean的信息時,路徑就由原來的/beans變?yōu)?beansome;
2、啟用和禁用端點
默認情況下,所有端點(除了/shutdown)都是啟用的。
禁用端點所有的端點:
endpoints.enabled=false
禁用某個特定的端點:
endpoints.endpoint-id.enabled=false
注意奧!
禁用后,再次訪問URL時,會出現(xiàn)404錯誤。
3、添加自定義度量信息
3.1 簡單的自定義度量信息:
springBoot自動配置Actuator創(chuàng)建CounterService的實例,并將其注冊為Spring的應(yīng)用程序上下文中的Bean。
CounterService這個接口里定義了三個方法分別用來增加、減少或重置特定名稱的度量值。
代碼如下:
package org.springframework.boot.actuate.metrics;
public interface CounterService {
void increment(String metricName);
void decrement(String metricName);
void reset(String metricName);
}
Actuator的自動配置還會配置一個GaugeService類型的Bean。
代碼如下:
package org.springframework.boot.actuate.metrics;
public interface GaugeService {
void submit(String metricName, double value);
}
自己寫的一個實例:
@Controllerpublic class HelloController {
@Autowired
private CounterService counterService;
@Autowired
private GaugeService gaugeService;
@RequestMapping(value = "/login", method=RequestMethod.GET)
public String login() {
counterService.increment("logintimes:");
gaugeService.submit("loginLastTime:",System.currentTimeMillis());
return "login";
}
}
在運行/ metrics時,出現(xiàn)自定義的度量信息。
3.2 相對復(fù)雜點的自定義度量信息
?主要是寫一個類實現(xiàn)publicMetrics接口,提供自己想要的度量信息。該接口定義了一個metrics()方法,返回一個Metric對象的集合
代碼如下:
@Componentpublic
class CustomMetrics implements PublicMetrics {
private ApplicationContext applicationContext;
@Autowide
public CustomMetrics(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public Collection<Metric<?>> metrics() {
List<Metric<?>> metrics = new ArrayList<Metric<?>>();
metrics.add(new Metric<Long>("spring.startup-date",applicationContext.getStartupDate()));
metrics.add(new Metric<Integer>("spring.bean.definitions",applicationContext.getBeanDefinitionCount()));
metrics.add(new Metric<Integer>("spring.beans",applicationContext.getBeanNamesForType(Object.class).length));
metrics.add(new Metric<Integer>("spring.controllers",applicationContext.getBeanNamesForAnnotation(Controller.class).length));
return metrics;
}
}
運行結(jié)果中可以找到以上自定義的度量信息
"spring.startup-date":1477211367363,
"spring.bean.definitions":317,
"spring.beans":331,
"spring.controllers":3,
4、插入自定義健康指示器
?自定義健康指示器時,需要實現(xiàn)HealthIndicator,重寫health()方法即可。調(diào)用withDetail()方法向健康記錄里添加其他附加信息。有多個附加信息時,可多次調(diào)用withDetail()方法,每次設(shè)置一個健康記錄的附加字段。
示例(關(guān)于一個異常的健康指示器)如下:
@Componentpublic
class CustomHealth implements HealthIndicator{
@Override
public Health health() {
try {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getForObject("http://localhost:8080/index",String.class);
return Health.up().build();
}catch (Exception e){
return Health.down().withDetail("down的原因:",e.getMessage()).build();
}
}
}
?運行“http://localhost:8080/health”
之后就會出現(xiàn)一些列健康指示。有時候有的瀏覽器僅僅出現(xiàn){"status":"DOWN"},為什么會是這樣呢?官網(wǎng)給了我們答案
Shows application health information (when the application is secure, a simple ‘status’ when accessed over an unauthenticated connection or full message details when authenticated)
而且要求Sensitive Default 為flase