Commit 4a0a7061 authored by liaozan's avatar liaozan 🏀

Add spring-boot-admin

parent 20adb552
package com.schbrain.common.util; package com.schbrain.common.util;
import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.io.IOException; import java.io.IOException;
import java.net.*; import java.net.*;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Optional;
/** /**
* @author liaozan * @author liaozan
* @since 2021/11/19 * @since 2021/11/19
*/ */
@Slf4j @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() { public static String getIpAddress() {
return HOST_INFO; return POD_IP == null ? LOCAL_IP : POD_IP;
}
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;
} }
private static InetAddress findFirstNonLoopBackAddress() { private static InetAddress findFirstNonLoopBackAddress() {
...@@ -72,21 +61,4 @@ public class HostInfoHolder { ...@@ -72,21 +61,4 @@ public class HostInfoHolder {
return null; 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 @@ ...@@ -87,6 +87,7 @@
<pagehelper.version>5.3.3</pagehelper.version> <pagehelper.version>5.3.3</pagehelper.version>
<redisson.version>3.18.0</redisson.version> <redisson.version>3.18.0</redisson.version>
<skywalking-tooklit.version>9.0.0</skywalking-tooklit.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> <vavr.version>0.10.4</vavr.version>
<xxl-job.version>2.0.2</xxl-job.version> <xxl-job.version>2.0.2</xxl-job.version>
<zookeeper.version>3.9.1</zookeeper.version> <zookeeper.version>3.9.1</zookeeper.version>
...@@ -422,6 +423,16 @@ ...@@ -422,6 +423,16 @@
<artifactId>apm-toolkit-logback-1.x</artifactId> <artifactId>apm-toolkit-logback-1.x</artifactId>
<version>${skywalking-tooklit.version}</version> <version>${skywalking-tooklit.version}</version>
</dependency> </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> <dependency>
<groupId>io.vavr</groupId> <groupId>io.vavr</groupId>
<artifactId>vavr</artifactId> <artifactId>vavr</artifactId>
......
...@@ -8,7 +8,6 @@ import ch.qos.logback.core.rolling.RollingFileAppender; ...@@ -8,7 +8,6 @@ import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import com.schbrain.common.util.*; 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.logstash.EnhancedLogstashEncoder;
import com.schbrain.framework.autoconfigure.logger.properties.LoggingProperties; import com.schbrain.framework.autoconfigure.logger.properties.LoggingProperties;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -126,11 +125,9 @@ public class JSONLoggingInitializer { ...@@ -126,11 +125,9 @@ public class JSONLoggingInitializer {
} }
private String getCustomFields() { private String getCustomFields() {
HostInfo hostInfo = HostInfoHolder.getHostInfo();
JSONObject customFields = new JSONObject(); JSONObject customFields = new JSONObject();
customFields.set("appName", applicationName); customFields.set("appName", applicationName);
customFields.set("hostName", hostInfo.getHostname()); customFields.set("podIp", IpAddressHolder.getIpAddress());
customFields.set("podIp", hostInfo.getIpAddress());
return customFields.toString(); return customFields.toString();
} }
......
...@@ -5,9 +5,7 @@ import cn.hutool.system.SystemUtil; ...@@ -5,9 +5,7 @@ import cn.hutool.system.SystemUtil;
import com.ctrip.framework.apollo.ConfigFile; import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.ConfigService; import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.google.common.collect.Maps; import com.schbrain.common.util.IpAddressHolder;
import com.schbrain.common.util.HostInfoHolder;
import com.schbrain.common.util.HostInfoHolder.HostInfo;
import com.schbrain.framework.autoconfigure.apollo.event.ConfigLoadedEvent; import com.schbrain.framework.autoconfigure.apollo.event.ConfigLoadedEvent;
import com.schbrain.framework.autoconfigure.apollo.event.listener.ConfigLoadedEventListenerAdaptor; import com.schbrain.framework.autoconfigure.apollo.event.listener.ConfigLoadedEventListenerAdaptor;
import com.schbrain.framework.autoconfigure.logger.JSONLoggingInitializer; import com.schbrain.framework.autoconfigure.logger.JSONLoggingInitializer;
...@@ -33,7 +31,7 @@ public class LoggingConfigLoadedEventListener extends ConfigLoadedEventListenerA ...@@ -33,7 +31,7 @@ public class LoggingConfigLoadedEventListener extends ConfigLoadedEventListenerA
@Override @Override
protected void onConfigLoaded(ConfigLoadedEvent event, LoggingProperties properties) { protected void onConfigLoaded(ConfigLoadedEvent event, LoggingProperties properties) {
event.getPropertySource().addProperties(buildHostInfoProperties()); event.getPropertySource().addProperties(buildIpAddressProperties());
configLoggingFileLocation(event.getEnvironment(), properties.getLogConfigNamespace()); configLoggingFileLocation(event.getEnvironment(), properties.getLogConfigNamespace());
} }
...@@ -45,12 +43,8 @@ public class LoggingConfigLoadedEventListener extends ConfigLoadedEventListenerA ...@@ -45,12 +43,8 @@ public class LoggingConfigLoadedEventListener extends ConfigLoadedEventListenerA
/** /**
* hostInfo properties, for logging pattern, used in logback-spring.xml * hostInfo properties, for logging pattern, used in logback-spring.xml
*/ */
private Map<String, Object> buildHostInfoProperties() { private Map<String, Object> buildIpAddressProperties() {
HostInfo hostInfo = HostInfoHolder.getHostInfo(); return Map.of("application.ipAddress", IpAddressHolder.getIpAddress());
Map<String, Object> properties = Maps.newHashMapWithExpectedSize(2);
properties.put("application.hostname", hostInfo.getHostname());
properties.put("application.ipAddress", hostInfo.getIpAddress());
return properties;
} }
/** /**
......
package com.schbrain.framework.autoconfigure.xxl.properties; 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 com.schbrain.common.util.support.ConfigurableProperties;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
...@@ -15,7 +15,7 @@ public class XxlJobProperties implements ConfigurableProperties { ...@@ -15,7 +15,7 @@ public class XxlJobProperties implements ConfigurableProperties {
private String adminAddresses; private String adminAddresses;
private String ip = HostInfoHolder.getHostInfo().getIpAddress(); private String ip = IpAddressHolder.getIpAddress();
private int port = -1; private int port = -1;
......
...@@ -20,16 +20,24 @@ ...@@ -20,16 +20,24 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-aop</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <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>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId> <artifactId>spring-boot-configuration-processor</artifactId>
</dependency> </dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId> <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 ...@@ -32,13 +32,17 @@ public class DefaultPropertiesEnvironmentPostProcessor extends LoggerAwareEnviro
private static final String SPRING_PROFILE_ACTIVE = "spring.profiles.active"; private static final String SPRING_PROFILE_ACTIVE = "spring.profiles.active";
private final Map<String, Object> defaultProperties = new LinkedHashMap<>();
public DefaultPropertiesEnvironmentPostProcessor(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) { public DefaultPropertiesEnvironmentPostProcessor(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext) {
super(logFactory, bootstrapContext); super(logFactory, bootstrapContext);
} }
@Override @Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Map<String, Object> defaultProperties = new LinkedHashMap<>(); // active profile
configureActiveProfileIfPresent(environment, defaultProperties);
environment.setDefaultProfiles(EnvUtils.DEVELOPMENT);
// management // management
defaultProperties.put("management.trace.http.enabled", false); defaultProperties.put("management.trace.http.enabled", false);
defaultProperties.put("management.endpoints.web.exposure.include", "*"); defaultProperties.put("management.endpoints.web.exposure.include", "*");
...@@ -46,6 +50,7 @@ public class DefaultPropertiesEnvironmentPostProcessor extends LoggerAwareEnviro ...@@ -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-details", Show.ALWAYS.name());
defaultProperties.put("management.endpoint.health.show-components", 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.git.mode", Mode.FULL.name());
defaultProperties.put("management.info.env.enabled", true);
defaultProperties.put("management.server.port", PortUtils.findAvailablePort(1024)); defaultProperties.put("management.server.port", PortUtils.findAvailablePort(1024));
// servlet // servlet
defaultProperties.put("spring.servlet.multipart.max-file-size", DataSize.ofBytes(-1).toString()); defaultProperties.put("spring.servlet.multipart.max-file-size", DataSize.ofBytes(-1).toString());
...@@ -64,9 +69,9 @@ public class DefaultPropertiesEnvironmentPostProcessor extends LoggerAwareEnviro ...@@ -64,9 +69,9 @@ public class DefaultPropertiesEnvironmentPostProcessor extends LoggerAwareEnviro
defaultProperties.put("spring.main.allow-circular-references", true); defaultProperties.put("spring.main.allow-circular-references", true);
defaultProperties.put("spring.main.banner-mode", Banner.Mode.OFF.name()); defaultProperties.put("spring.main.banner-mode", Banner.Mode.OFF.name());
defaultProperties.put("server.shutdown", Shutdown.GRACEFUL.name()); defaultProperties.put("server.shutdown", Shutdown.GRACEFUL.name());
// active profile if (EnvUtils.runningOnCloudPlatform(environment)) {
configureActiveProfileIfPresent(environment, defaultProperties); defaultProperties.put("spring.boot.admin.client.url", "http://spring-boot-admin-server.devops:8019");
environment.setDefaultProfiles(EnvUtils.DEVELOPMENT); }
DefaultPropertiesPropertySource.addOrMerge(defaultProperties, environment.getPropertySources()); DefaultPropertiesPropertySource.addOrMerge(defaultProperties, environment.getPropertySources());
} }
......
com.schbrain.framework.support.spring.admin.SpringBootAdminFeatureAutoConfiguration
com.schbrain.framework.support.spring.elasticsearch.ElasticsearchFeatureAutoConfiguration com.schbrain.framework.support.spring.elasticsearch.ElasticsearchFeatureAutoConfiguration
com.schbrain.framework.support.spring.redisson.RedissonFeatureAutoConfiguration 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