spring-boot-admin原始碼分析及單機監控spring-boot-monitor的實現(二)
阿新 • • 發佈:2018-11-27
SpringBootMonitor
spring-boot-admin原始碼分析及單機監控spring-boot-monitor的實現(一)
spring-boot-admin原始碼分析及單機監控spring-boot-monitor的實現(二)
spring-boot-admin原始碼分析及單機監控spring-boot-monitor的實現(三)1.spring-boot-admin配置分析
(AdminServerProperties.class) 指出注入 AdminServerProperties類,AdminServerProperties類中註解spring-boot-admin使用AdminServerCoreConfiguration進行屬性配置,@EnableConfigurationProperties
@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/下;將
classpath*:/META-INF/spring-boot-admin-server-ui/*/module.css/js 上,它根據自己的邏輯去實現了
這些資源的整合訪問。在addViewControllers方法中,將contextPath + / 的訪問forward到index.html。
2.spring-boot-monitor配置實現。
我們提供MonitorConfiguration、MonitorProperties、MonitorWebConfiguration分別實現對應的功能。
MonitorConfiguration:
package com.cff.boot.monitor.config;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(MonitorProperties.class)
public class MonitorConfiguration {
private final MonitorProperties monitorProperties;
public MonitorConfiguration(MonitorProperties monitorProperties) {
this.monitorProperties = monitorProperties;
}
}
MonitorProperties:
package com.cff.boot.monitor.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("spring.boot.monitor")
public class MonitorProperties {
private String contextPath = "/sbim";
private String username = "";
private String password = "";
public String getContextPath() {
return contextPath;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
MonitorWebConfiguration:
package com.cff.boot.monitor.config;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.cff.boot.monitor.model.AppInfo;
import com.cff.boot.monitor.resolver.UrlFilteringResourceResolver;
import com.cff.boot.monitor.store.SimpleAppInfoStore;
import com.cff.boot.monitor.web.RestApiController;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
@AutoConfigureAfter({MonitorConfiguration.class})
public class MonitorWebConfiguration extends WebMvcConfigurerAdapter
implements ApplicationContextAware{
private final ApplicationEventPublisher publisher;
private final ServerProperties server;
private final ResourcePatternResolver resourcePatternResolver;
private final MonitorProperties monitorProperties;
private ApplicationContext applicationContext;
public MonitorWebConfiguration(ApplicationEventPublisher publisher, ServerProperties server,
ResourcePatternResolver resourcePatternResolver,
MonitorProperties monitorProperties) {
this.publisher = publisher;
this.server = server;
this.resourcePatternResolver = resourcePatternResolver;
this.monitorProperties = monitorProperties;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
if (!hasConverter(converters, MappingJackson2HttpMessageConverter.class)) {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
.applicationContext(this.applicationContext).build();
converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
}
}
private boolean hasConverter(List<HttpMessageConverter<?>> converters,
Class<? extends HttpMessageConverter<?>> clazz) {
for (HttpMessageConverter<?> converter : converters) {
if (clazz.isInstance(converter)) {
return true;
}
}
return false;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("路徑:" + monitorProperties.getContextPath());
registry.addResourceHandler(monitorProperties.getContextPath() + "/**")
.addResourceLocations("classpath:/META-INF/spring-boot-monitor-ui/")
.resourceChain(true)
.addResolver(new UrlFilteringResourceResolver(".min"));
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
String contextPath = monitorProperties.getContextPath();
if (StringUtils.hasText(contextPath)) {
registry.addRedirectViewController(contextPath, server.getPath(contextPath) + "/");
}
registry.addViewController(contextPath + "/").setViewName("forward:login.html");
}
@Bean
@ConditionalOnMissingBean
public SimpleAppInfoStore appInfoStore() {
SimpleAppInfoStore simpleAppInfoStore = new SimpleAppInfoStore();
String appId = applicationContext.getId();
String appName = appId.substring(0, appId.indexOf(":"));
String appInfo = appId;
String appStatus = "UP";
AppInfo app = new AppInfo(appName,"",appInfo,appStatus);
simpleAppInfoStore.addApp(app);
return simpleAppInfoStore;
}
@Bean
@ConditionalOnMissingBean
public RestApiController restApiController(SimpleAppInfoStore simpleAppInfoStore) {
return new RestApiController(monitorProperties,simpleAppInfoStore);
}
}
addViewControllor是必須的,不然使用模版引擎會導致自定義的內容失效。
拷貝一個ResourceResolver改個名,也懶得去了解它是幹嘛的了。UrlFilteringResourceResolver:
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cff.boot.monitor.resolver;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.resource.AbstractResourceResolver;
import org.springframework.web.servlet.resource.ResourceResolver;
import org.springframework.web.servlet.resource.ResourceResolverChain;
/**
* {@link ResourceResolver} which is looking for minified version of resources.
*
* @author Johannes Edmeier
*/
public class UrlFilteringResourceResolver extends AbstractResourceResolver {
private final String extensionPrefix;
public UrlFilteringResourceResolver(String extensionPrefix) {
this.extensionPrefix = extensionPrefix;
}
@Override
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
List<? extends Resource> locations, ResourceResolverChain chain) {
List<Resource> newLocations = new ArrayList<>(locations.size());
for (Resource location : locations) {
Resource minified = findMinified(location);
newLocations.add(minified != null ? minified : location);
}
return chain.resolveResource(request, requestPath, newLocations);
}
private Resource findMinified(Resource resource) {
try {
String basename = StringUtils.stripFilenameExtension(resource.getFilename());
String extension = StringUtils.getFilenameExtension(resource.getFilename());
Resource minified = resource
.createRelative(basename + extensionPrefix + '.' + extension);
if (minified.exists()) {
if (logger.isDebugEnabled()) {
logger.debug("Found minified file for '" + resource.getFilename() + "': '"
+ minified.getFilename() + "'");
}
return minified;
}
} catch (IOException ex) {
logger.trace("No minified resource for [" + resource.getFilename() + "]", ex);
}
return null;
}
@Override
protected String resolveUrlPathInternal(String resourceUrlPath,
List<? extends Resource> locations, ResourceResolverChain chain) {
return chain.resolveUrlPath(resourceUrlPath, locations);
}
}