当前位置:   article > 正文

spring-boot-admin源码分析及单机监控spring-boot-monitor的实现(二)_springboot admin源码

springboot admin源码

SpringBootMonitor


1.spring-boot-admin配置分析

spring-boot-admin使用AdminServerCoreConfiguration进行属性配置,@EnableConfigurationProperties

(AdminServerProperties.class) 指出注入 AdminServerProperties类,AdminServerProperties类中注解

@ConfigurationProperties("spring.boot.admin"),将读取application.yml配置文件中以spring.boot.admin

开头的配置,并与属性一一对应。同时,AdminServerCoreConfiguration也注入了一系列bean,总的来说,

AdminServerCoreConfiguration就是注入配置及业务处理bean的管理。


spring-boot-admin使用AdminServerWebConfiguration进行web相关配置管理。AdminServerWebConfiguration

继承自WebMvcConfigurerAdapter,实现了ApplicationContextAware。其中最主要的就是重写了addResourceHandlers

和addViewControllers方法。在addResourceHandlers方法中,它将所有访问contextPath + / 的路径映射到

classpath:/META-INF/spring-boot-admin-server-ui/下;将contextPath + all-modules.css/js 映射到

classpath*:/META-INF/spring-boot-admin-server-ui/*/module.css/js 上,它根据自己的逻辑去实现了

这些资源的整合访问。在addViewControllers方法中,将contextPath + / 的访问forward到index.html。


2.spring-boot-monitor配置实现。


我们提供MonitorConfiguration、MonitorProperties、MonitorWebConfiguration分别实现对应的功能。

MonitorConfiguration:
  1. package com.cff.boot.monitor.config;
  2. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. @EnableConfigurationProperties(MonitorProperties.class)
  6. public class MonitorConfiguration {
  7. private final MonitorProperties monitorProperties;
  8. public MonitorConfiguration(MonitorProperties monitorProperties) {
  9. this.monitorProperties = monitorProperties;
  10. }
  11. }

MonitorProperties:
  1. package com.cff.boot.monitor.config;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. @ConfigurationProperties("spring.boot.monitor")
  4. public class MonitorProperties {
  5. private String contextPath = "/sbim";
  6. private String username = "";
  7. private String password = "";
  8. public String getContextPath() {
  9. return contextPath;
  10. }
  11. public String getUsername() {
  12. return username;
  13. }
  14. public void setUsername(String username) {
  15. this.username = username;
  16. }
  17. public String getPassword() {
  18. return password;
  19. }
  20. public void setPassword(String password) {
  21. this.password = password;
  22. }
  23. }

MonitorWebConfiguration:
  1. package com.cff.boot.monitor.config;
  2. import java.util.List;
  3. import java.util.Map;
  4. import org.springframework.beans.BeansException;
  5. import org.springframework.boot.autoconfigure.AutoConfigureAfter;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  7. import org.springframework.boot.autoconfigure.web.ServerProperties;
  8. import org.springframework.context.ApplicationContext;
  9. import org.springframework.context.ApplicationContextAware;
  10. import org.springframework.context.ApplicationEventPublisher;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.core.io.support.ResourcePatternResolver;
  14. import org.springframework.http.converter.HttpMessageConverter;
  15. import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
  16. import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  17. import org.springframework.util.StringUtils;
  18. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  19. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  20. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  21. import com.cff.boot.monitor.model.AppInfo;
  22. import com.cff.boot.monitor.resolver.UrlFilteringResourceResolver;
  23. import com.cff.boot.monitor.store.SimpleAppInfoStore;
  24. import com.cff.boot.monitor.web.RestApiController;
  25. import com.fasterxml.jackson.databind.ObjectMapper;
  26. @Configuration
  27. @AutoConfigureAfter({MonitorConfiguration.class})
  28. public class MonitorWebConfiguration extends WebMvcConfigurerAdapter
  29. implements ApplicationContextAware{
  30. private final ApplicationEventPublisher publisher;
  31. private final ServerProperties server;
  32. private final ResourcePatternResolver resourcePatternResolver;
  33. private final MonitorProperties monitorProperties;
  34. private ApplicationContext applicationContext;
  35. public MonitorWebConfiguration(ApplicationEventPublisher publisher, ServerProperties server,
  36. ResourcePatternResolver resourcePatternResolver,
  37. MonitorProperties monitorProperties) {
  38. this.publisher = publisher;
  39. this.server = server;
  40. this.resourcePatternResolver = resourcePatternResolver;
  41. this.monitorProperties = monitorProperties;
  42. }
  43. @Override
  44. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  45. this.applicationContext = applicationContext;
  46. }
  47. @Override
  48. public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
  49. if (!hasConverter(converters, MappingJackson2HttpMessageConverter.class)) {
  50. ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
  51. .applicationContext(this.applicationContext).build();
  52. converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
  53. }
  54. }
  55. private boolean hasConverter(List<HttpMessageConverter<?>> converters,
  56. Class<? extends HttpMessageConverter<?>> clazz) {
  57. for (HttpMessageConverter<?> converter : converters) {
  58. if (clazz.isInstance(converter)) {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. @Override
  65. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  66. System.out.println("路径:" + monitorProperties.getContextPath());
  67. registry.addResourceHandler(monitorProperties.getContextPath() + "/**")
  68. .addResourceLocations("classpath:/META-INF/spring-boot-monitor-ui/")
  69. .resourceChain(true)
  70. .addResolver(new UrlFilteringResourceResolver(".min"));
  71. }
  72. @Override
  73. public void addViewControllers(ViewControllerRegistry registry) {
  74. String contextPath = monitorProperties.getContextPath();
  75. if (StringUtils.hasText(contextPath)) {
  76. registry.addRedirectViewController(contextPath, server.getPath(contextPath) + "/");
  77. }
  78. registry.addViewController(contextPath + "/").setViewName("forward:login.html");
  79. }
  80. @Bean
  81. @ConditionalOnMissingBean
  82. public SimpleAppInfoStore appInfoStore() {
  83. SimpleAppInfoStore simpleAppInfoStore = new SimpleAppInfoStore();
  84. String appId = applicationContext.getId();
  85. String appName = appId.substring(0, appId.indexOf(":"));
  86. String appInfo = appId;
  87. String appStatus = "UP";
  88. AppInfo app = new AppInfo(appName,"",appInfo,appStatus);
  89. simpleAppInfoStore.addApp(app);
  90. return simpleAppInfoStore;
  91. }
  92. @Bean
  93. @ConditionalOnMissingBean
  94. public RestApiController restApiController(SimpleAppInfoStore simpleAppInfoStore) {
  95. return new RestApiController(monitorProperties,simpleAppInfoStore);
  96. }
  97. }

addViewControllor是必须的,不然使用模版引擎会导致自定义的内容失效。

拷贝一个ResourceResolver改个名,也懒得去了解它是干嘛的了。UrlFilteringResourceResolver:

  1. /*
  2. * Copyright 2014 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.cff.boot.monitor.resolver;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import javax.servlet.http.HttpServletRequest;
  21. import org.springframework.core.io.Resource;
  22. import org.springframework.util.StringUtils;
  23. import org.springframework.web.servlet.resource.AbstractResourceResolver;
  24. import org.springframework.web.servlet.resource.ResourceResolver;
  25. import org.springframework.web.servlet.resource.ResourceResolverChain;
  26. /**
  27. * {@link ResourceResolver} which is looking for minified version of resources.
  28. *
  29. * @author Johannes Edmeier
  30. */
  31. public class UrlFilteringResourceResolver extends AbstractResourceResolver {
  32. private final String extensionPrefix;
  33. public UrlFilteringResourceResolver(String extensionPrefix) {
  34. this.extensionPrefix = extensionPrefix;
  35. }
  36. @Override
  37. protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
  38. List<? extends Resource> locations, ResourceResolverChain chain) {
  39. List<Resource> newLocations = new ArrayList<>(locations.size());
  40. for (Resource location : locations) {
  41. Resource minified = findMinified(location);
  42. newLocations.add(minified != null ? minified : location);
  43. }
  44. return chain.resolveResource(request, requestPath, newLocations);
  45. }
  46. private Resource findMinified(Resource resource) {
  47. try {
  48. String basename = StringUtils.stripFilenameExtension(resource.getFilename());
  49. String extension = StringUtils.getFilenameExtension(resource.getFilename());
  50. Resource minified = resource
  51. .createRelative(basename + extensionPrefix + '.' + extension);
  52. if (minified.exists()) {
  53. if (logger.isDebugEnabled()) {
  54. logger.debug("Found minified file for '" + resource.getFilename() + "': '"
  55. + minified.getFilename() + "'");
  56. }
  57. return minified;
  58. }
  59. } catch (IOException ex) {
  60. logger.trace("No minified resource for [" + resource.getFilename() + "]", ex);
  61. }
  62. return null;
  63. }
  64. @Override
  65. protected String resolveUrlPathInternal(String resourceUrlPath,
  66. List<? extends Resource> locations, ResourceResolverChain chain) {
  67. return chain.resolveUrlPath(resourceUrlPath, locations);
  68. }
  69. }




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/125586
推荐阅读
相关标签
  

闽ICP备14008679号