Spring Boot 入门

来源:互联网 发布:绝对争锋网络剧在线 编辑:程序博客网 时间:2024/06/05 00:54

http://rensanning.iteye.com/blog/2363526#comments

2)设置端点访问 


1-关闭验证 
默认情况下很多端点是不允许访问的,会返回401:Unauthorized。 

application.properties 
引用
management.security.enabled=false


2-开启HTTP basic认证 

Xml代码  收藏代码
  1. <dependency>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-security</artifactId>  
  4. </dependency>  


application.properties 
引用
security.user.name=admin 
security.user.password=123456 
management.security.enabled=true 
management.security.role=ADMIN


访问URL http://localhost:8080/env 后,就看到需要输入用户名和密码了。 

3-设置ContextPath 

application.properties 
引用
management.contextPath=/manage


那么URL就是:http://localhost:8080/manage/env 

4-设置端口 

application.properties 
引用
management.port=8081


那么URL就是:http://localhost:8081/manage/env 

(2)端点Endpoint 

应用配置类 
  /autoconfig 获取应用的自动化配置报告 
  /beans 获取应用上下文中创建的所有Bean 
  /configprops 获取应用中配置的属性信息报告 
  /env 获取应用所有可用的环境属性报告 
  /mappings 获取应用所有Spring MVC的控制器映射关系报告 
  /info 获取应用自定义的信息 

度量指标类 
  /metrics 返回应用的各类重要度量指标信息 
  /health 返回应用的各类健康指标信息 
  /dump 返回程序运行中的线程信息 
  /trace 返回基本的HTTP跟踪信息 

操作控制类 
  /shutdown 用来远程关闭应用 

(3)端点的开启或禁用 
application.properties 
引用
endpoints.configprops.enabled=false 
endpoints.shutdown.enabled=true


(4)自定义端点 

1-自定义已有端点,比如 /health 端点 
Java代码  收藏代码
  1. @Component  
  2. public class CustomHealth implements HealthIndicator {  
  3.    
  4.     public Health health() {  
  5.         return Health.up().build();  
  6.     }  
  7.    
  8. }  


访问URL:http://localhost:8081/manage/health 

2-创建新的端点 
Java代码  收藏代码
  1. @Component  
  2. public class CustomEndpoint implements Endpoint<List<String>> {  
  3.        
  4.     public String getId() {  
  5.         return "myep";  
  6.     }  
  7.    
  8.     public boolean isEnabled() {  
  9.         return true;  
  10.     }  
  11.    
  12.     public boolean isSensitive() {  
  13.         return true;  
  14.     }  
  15.    
  16.     public List<String> invoke() {  
  17.         List<String> messages = new ArrayList<String>();  
  18.         messages.add("This is message 1");  
  19.         messages.add("This is message 2");  
  20.         return messages;  
  21.     }  
  22. }  


访问URL:http://localhost:8081/manage/myep 

3-列举所有端点 
Java代码  收藏代码
  1. @Component  
  2. public class ListEndpoints extends AbstractEndpoint<List<Endpoint>> {  
  3.     private List<Endpoint> endpoints;  
  4.    
  5.     @Autowired  
  6.     public ListEndpoints(List<Endpoint> endpoints) {  
  7.         super("allep");  
  8.         this.endpoints = endpoints;  
  9.     }  
  10.    
  11.     public List<Endpoint> invoke() {  
  12.         return this.endpoints;  
  13.     }  
  14. }  


访问URL:http://localhost:8081/manage/allep 

(5)自定义端点metrics 
Spring Boot允许开发人员以编码的方式提供更丰富指标信息,通过/metrics端点来访问。counter是以Number类型来展现的指标;gauge是衡量双精度计算的指标。任何位置都可以注入CounterService或GaugeService。 

Java代码  收藏代码
  1. counterService.increment("metricName");  
  2. counterService.decrement("metricName");  
  3. counterService.reset("metricName");  


Java代码  收藏代码
  1. gaugeService.submit("metricName"2.5);  


参考: 
http://www.baeldung.com/spring-boot-actuators 
http://blog.didispace.com/spring-boot-actuator-1/
原创粉丝点击