Commit 4a0a7061 authored by liaozan's avatar liaozan 🏀

Add spring-boot-admin

parent 20adb552
package com.schbrain.common.util;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.*;
import java.util.Enumeration;
import java.util.Optional;
/**
* @author liaozan
* @since 2021/11/19
*/
@Slf4j
public class HostInfoHolder {
public class IpAddressHolder {
private static final HostInfo HOST_INFO = findFirstNonLoopBackHostInfo();
private static final String POD_IP = System.getenv("POD_IP");
private static final String LOCAL_IP = Optional.ofNullable(findFirstNonLoopBackAddress()).map(InetAddress::getHostAddress).orElse("127.0.0.1");
public static HostInfo getHostInfo() {
return HOST_INFO;
}
private static HostInfo findFirstNonLoopBackHostInfo() {
InetAddress address = findFirstNonLoopBackAddress();
if (address != null) {
return convertAddress(address);
}
log.warn("Cannot find first non-loopBack address, fallback to localhost");
HostInfo hostInfo = new HostInfo();
hostInfo.setHostname("localhost");
hostInfo.setIpAddress("127.0.0.1");
return hostInfo;
public static String getIpAddress() {
return POD_IP == null ? LOCAL_IP : POD_IP;
}
private static InetAddress findFirstNonLoopBackAddress() {
......@@ -72,21 +61,4 @@ public class HostInfoHolder {
return null;
}
private static HostInfo convertAddress(final InetAddress address) {
HostInfo hostInfo = new HostInfo();
String hostname = address.getHostName();
hostInfo.setHostname(hostname);
hostInfo.setIpAddress(address.getHostAddress());
return hostInfo;
}
@Data
public static class HostInfo {
private String ipAddress;
private String hostname;
}
}
......@@ -87,6 +87,7 @@
<pagehelper.version>5.3.3</pagehelper.version>
<redisson.version>3.18.0</redisson.version>
<skywalking-tooklit.version>9.0.0</skywalking-tooklit.version>
<spring-boot-admin.version>2.7.11</spring-boot-admin.version>
<vavr.version>0.10.4</vavr.version>
<xxl-job.version>2.0.2</xxl-job.version>
<zookeeper.version>3.9.1</zookeeper.version>
......@@ -422,6 +423,16 @@
<artifactId>apm-toolkit-logback-1.x</artifactId>
<version>${skywalking-tooklit.version}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
......
......@@ -8,7 +8,6 @@ import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import cn.hutool.json.JSONObject;
import com.schbrain.common.util.*;
import com.schbrain.common.util.HostInfoHolder.HostInfo;
import com.schbrain.framework.autoconfigure.logger.logstash.EnhancedLogstashEncoder;
import com.schbrain.framework.autoconfigure.logger.properties.LoggingProperties;
import lombok.extern.slf4j.Slf4j;
......@@ -126,11 +125,9 @@ public class JSONLoggingInitializer {
}
private String getCustomFields() {
HostInfo hostInfo = HostInfoHolder.getHostInfo();
JSONObject customFields = new JSONObject();
customFields.set("appName", applicationName);
customFields.set("hostName", hostInfo.getHostname());
customFields.set("podIp", hostInfo.getIpAddress());
customFields.set("podIp", IpAddressHolder.getIpAddress());
return customFields.toString();
}
......
......@@ -5,9 +5,7 @@ import cn.hutool.system.SystemUtil;
import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.google.common.collect.Maps;
import com.schbrain.common.util.HostInfoHolder;
import com.schbrain.common.util.HostInfoHolder.HostInfo;
import com.schbrain.common.util.IpAddressHolder;
import com.schbrain.framework.autoconfigure.apollo.event.ConfigLoadedEvent;
import com.schbrain.framework.autoconfigure.apollo.event.listener.ConfigLoadedEventListenerAdaptor;
import com.schbrain.framework.autoconfigure.logger.JSONLoggingInitializer;
......@@ -33,7 +31,7 @@ public class LoggingConfigLoadedEventListener extends ConfigLoadedEventListenerA
@Override
protected void onConfigLoaded(ConfigLoadedEvent event, LoggingProperties properties) {
event.getPropertySource().addProperties(buildHostInfoProperties());
event.getPropertySource().addProperties(buildIpAddressProperties());
configLoggingFileLocation(event.getEnvironment(), properties.getLogConfigNamespace());
}
......@@ -45,12 +43,8 @@ public class LoggingConfigLoadedEventListener extends ConfigLoadedEventListenerA
/**
* hostInfo properties, for logging pattern, used in logback-spring.xml
*/
private Map<String, Object> buildHostInfoProperties() {
HostInfo hostInfo = HostInfoHolder.getHostInfo();
Map<String, Object> properties = Maps.newHashMapWithExpectedSize(2);
properties.put("application.hostname", hostInfo.getHostname());
properties.put("application.ipAddress", hostInfo.getIpAddress());
return properties;
private Map<String, Object> buildIpAddressProperties() {
return Map.of("application.ipAddress", IpAddressHolder.getIpAddress());
}
/**
......
package com.schbrain.framework.autoconfigure.xxl.properties;
import com.schbrain.common.util.HostInfoHolder;
import com.schbrain.common.util.IpAddressHolder;
import com.schbrain.common.util.support.ConfigurableProperties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
......@@ -15,7 +15,7 @@ public class XxlJobProperties implements ConfigurableProperties {
private String adminAddresses;
private String ip = HostInfoHolder.getHostInfo().getIpAddress();
private String ip = IpAddressHolder.getIpAddress();
private int port = -1;
......
......@@ -20,16 +20,24 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
......
package com.schbrain.framework.support.spring.admin;
import com.schbrain.common.util.IpAddressHolder;
import de.codecentric.boot.admin.client.config.InstanceProperties;
import de.codecentric.boot.admin.client.registration.ServletApplicationFactory;
import de.codecentric.boot.admin.client.registration.metadata.MetadataContributor;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath;
import javax.servlet.ServletContext;
/**
* @author liaozan
* @since 2023/10/26
*/
public class AdminApplicationFactory extends ServletApplicationFactory {
public AdminApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, ServletContext servletContext, PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint, MetadataContributor metadataContributor,
DispatcherServletPath dispatcherServletPath) {
super(instance, management, server, servletContext, pathMappedEndpoints, webEndpoint, metadataContributor, dispatcherServletPath);
}
@Override
protected String getServiceHost() {
return IpAddressHolder.getIpAddress();
}
@Override
protected String getManagementHost() {
return IpAddressHolder.getIpAddress();
}
}
package com.schbrain.framework.support.spring.admin;
import de.codecentric.boot.admin.client.config.InstanceProperties;
import de.codecentric.boot.admin.client.config.SpringBootAdminClientAutoConfiguration;
import de.codecentric.boot.admin.client.registration.ApplicationFactory;
import de.codecentric.boot.admin.client.registration.metadata.MetadataContributor;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath;
import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.context.annotation.Bean;
import javax.servlet.ServletContext;
/**
* @author liaozan
* @since 2023/10/26
*/
@AutoConfiguration(before = SpringBootAdminClientAutoConfiguration.class)
@ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES)
public class SpringBootAdminFeatureAutoConfiguration {
@Bean
public ApplicationFactory applicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, ServletContext servletContext, PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint, MetadataContributor metadataContributor,
DispatcherServletPath dispatcherServletPath) {
return new AdminApplicationFactory(instance, management, server, servletContext, pathMappedEndpoints, webEndpoint, metadataContributor, dispatcherServletPath);
}
}
......@@ -32,13 +32,17 @@ public class DefaultPropertiesEnvironmentPostProcessor extends LoggerAwareEnviro
private static final String SPRING_PROFILE_ACTIVE = "spring.profiles.active";
private final Map<String, Object> defaultProperties = new LinkedHashMap<>();
public DefaultPropertiesEnvironmentPostProcessor(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) {
super(logFactory, bootstrapContext);
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Map<String, Object> defaultProperties = new LinkedHashMap<>();
// active profile
configureActiveProfileIfPresent(environment, defaultProperties);
environment.setDefaultProfiles(EnvUtils.DEVELOPMENT);
// management
defaultProperties.put("management.trace.http.enabled", false);
defaultProperties.put("management.endpoints.web.exposure.include", "*");
......@@ -46,6 +50,7 @@ public class DefaultPropertiesEnvironmentPostProcessor extends LoggerAwareEnviro
defaultProperties.put("management.endpoint.health.show-details", Show.ALWAYS.name());
defaultProperties.put("management.endpoint.health.show-components", Show.ALWAYS.name());
defaultProperties.put("management.info.git.mode", Mode.FULL.name());
defaultProperties.put("management.info.env.enabled", true);
defaultProperties.put("management.server.port", PortUtils.findAvailablePort(1024));
// servlet
defaultProperties.put("spring.servlet.multipart.max-file-size", DataSize.ofBytes(-1).toString());
......@@ -64,9 +69,9 @@ public class DefaultPropertiesEnvironmentPostProcessor extends LoggerAwareEnviro
defaultProperties.put("spring.main.allow-circular-references", true);
defaultProperties.put("spring.main.banner-mode", Banner.Mode.OFF.name());
defaultProperties.put("server.shutdown", Shutdown.GRACEFUL.name());
// active profile
configureActiveProfileIfPresent(environment, defaultProperties);
environment.setDefaultProfiles(EnvUtils.DEVELOPMENT);
if (EnvUtils.runningOnCloudPlatform(environment)) {
defaultProperties.put("spring.boot.admin.client.url", "http://spring-boot-admin-server.devops:8019");
}
DefaultPropertiesPropertySource.addOrMerge(defaultProperties, environment.getPropertySources());
}
......
com.schbrain.framework.support.spring.admin.SpringBootAdminFeatureAutoConfiguration
com.schbrain.framework.support.spring.elasticsearch.ElasticsearchFeatureAutoConfiguration
com.schbrain.framework.support.spring.redisson.RedissonFeatureAutoConfiguration
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment