一般来说springbootadmin会分为服务端,和客户端
服务端需要依赖spring-boot-admin-starter-server
客户端需要依赖spring-boot-admin-starter-client
如果是微服务的话将spingboot admin注册到服务发现中心就可以了
这是官网上的描述
简单来说就是
如果已经将 Spring Cloud Discovery 用于您的应用程序,则不需要 SBA 客户端。只需在 Spring Boot Admin Server 中添加一个 DiscoveryClient,其余的由我们的 AutoConfiguration 完成。
只需要在springbootadmin服务上加上注解@EnableDiscoveryClient,启用发现客户端导入选择器
springboot admin最重要的配置ApplicationRegistry 如下,
我们可以看到getApplications()通过groupID和服务名称获取到了所有已注册程序的应用列表
/*
* Copyright 2014-2019 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 de.codecentric.boot.admin.server.services;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;
import de.codecentric.boot.admin.server.domain.entities.Application;
import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.values.BuildVersion;
import de.codecentric.boot.admin.server.domain.values.InstanceId;
import de.codecentric.boot.admin.server.domain.values.StatusInfo;
import de.codecentric.boot.admin.server.eventstore.InstanceEventPublisher;
import static de.codecentric.boot.admin.server.domain.values.StatusInfo.STATUS_UNKNOWN;
import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
/**
* Registry for all applications that should be managed/administrated by the Spring Boot
* Admin server. Backed by an InstanceRegistry for persistence and an
* InstanceEventPublisher for events
*
* @author Dean de Bree
*/
public class ApplicationRegistry {
private final InstanceRegistry instanceRegistry;
private final InstanceEventPublisher instanceEventPublisher;
public ApplicationRegistry(InstanceRegistry instanceRegistry, InstanceEventPublisher instanceEventPublisher) {
this.instanceRegistry = instanceRegistry;
this.instanceEventPublisher = instanceEventPublisher;
}
/**
* Get a list of all registered applications.
* @return flux of all the applications.
*/
public Flux<Application> getApplications() {
return this.instanceRegistry.getInstances().filter(Instance::isRegistered)
.groupBy((instance) -> instance.getRegistration().getName())
.flatMap((grouped) -> toApplication(grouped.key(), grouped), Integer.MAX_VALUE);
}
/**
* Get a specific application instance.
* @param name the name of the application to find.
* @return a Mono with the application or an empty Mono if not found.
*/
public Mono<Application> getApplication(String name) {
return this.toApplication(name, this.instanceRegistry.getInstances(name).filter(Instance::isRegistered))
.filter((a) -> !a.getInstances().isEmpty());
}
public Flux<Application> getApplicationStream() {
return Flux.from(this.instanceEventPublisher)
.flatMap((event) -> this.instanceRegistry.getInstance(event.getInstance()))
.map(this::getApplicationForInstance).flatMap((group) -> toApplication(group.getT1(), group.getT2()));
}
public Flux<InstanceId> deregister(String name) {
return this.instanceRegistry.getInstances(name)
.flatMap((instance) -> this.instanceRegistry.deregister(instance.getId()));
}
protected Tuple2<String, Flux<Instance>> getApplicationForInstance(Instance instance) {
String name = instance.getRegistration().getName();
return Tuples.of(name, this.instanceRegistry.getInstances(name).filter(Instance::isRegistered));
}
protected Mono<Application> toApplication(String name, Flux<Instance> instances) {
return instances.collectList().map((instanceList) -> {
Tuple2<String, Instant> status = getStatus(instanceList);
return Application.create(name).instances(instanceList).buildVersion(getBuildVersion(instanceList))
.status(status.getT1()).statusTimestamp(status.getT2()).build();
});
}
@Nullable
protected BuildVersion getBuildVersion(List<Instance> instances) {
List<BuildVersion> versions = instances.stream().map(Instance::getBuildVersion).filter(Objects::nonNull)
.distinct().sorted().collect(toList());
if (versions.isEmpty()) {
return null;
}
else if (versions.size() == 1) {
return versions.get(0);
}
else {
return BuildVersion.valueOf(versions.get(0) + " ... " + versions.get(versions.size() - 1));
}
}
protected Tuple2<String, Instant> getStatus(List<Instance> instances) {
// TODO: Correct is just a second readmodel for groups
Map<String, Instant> statusWithTime = instances.stream().collect(
toMap((instance) -> instance.getStatusInfo().getStatus(), Instance::getStatusTimestamp, this::getMax));
if (statusWithTime.size() == 1) {
Map.Entry<String, Instant> e = statusWithTime.entrySet().iterator().next();
return Tuples.of(e.getKey(), e.getValue());
}
if (statusWithTime.containsKey(StatusInfo.STATUS_UP)) {
Instant oldestNonUp = statusWithTime.entrySet().stream()
.filter((e) -> !StatusInfo.STATUS_UP.equals(e.getKey())).map(Map.Entry::getValue)
.min(naturalOrder()).orElse(Instant.EPOCH);
Instant latest = getMax(oldestNonUp, statusWithTime.getOrDefault(StatusInfo.STATUS_UP, Instant.EPOCH));
return Tuples.of(StatusInfo.STATUS_RESTRICTED, latest);
}
return statusWithTime.entrySet().stream().min(Map.Entry.comparingByKey(StatusInfo.severity()))
.map((e) -> Tuples.of(e.getKey(), e.getValue())).orElse(Tuples.of(STATUS_UNKNOWN, Instant.EPOCH));
}
protected Instant getMax(Instant t1, Instant t2) {
return (t1.compareTo(t2) >= 0) ? t1 : t2;
}
}
@EnableDiscoveryClient的实现类EnableDiscoveryClientImportSelector
/*
* Copyright 2012-2020 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
*
* https://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 org.springframework.cloud.client.discovery;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import org.springframework.cloud.commons.util.SpringFactoryImportSelector;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.type.AnnotationMetadata;
/**
* @author Spencer Gibb
*/
@Order(Ordered.LOWEST_PRECEDENCE - 100)
public class EnableDiscoveryClientImportSelector
extends SpringFactoryImportSelector<EnableDiscoveryClient> {
@Override
public String[] selectImports(AnnotationMetadata metadata) {
String[] imports = super.selectImports(metadata);
AnnotationAttributes attributes = AnnotationAttributes.fromMap(
metadata.getAnnotationAttributes(getAnnotationClass().getName(), true));
boolean autoRegister = attributes.getBoolean("autoRegister");
if (autoRegister) {
List<String> importsList = new ArrayList<>(Arrays.asList(imports));
importsList.add(
"org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration");
imports = importsList.toArray(new String[0]);
}
else {
Environment env = getEnvironment();
if (ConfigurableEnvironment.class.isInstance(env)) {
ConfigurableEnvironment configEnv = (ConfigurableEnvironment) env;
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("spring.cloud.service-registry.auto-registration.enabled", false);
MapPropertySource propertySource = new MapPropertySource(
"springCloudDiscoveryClient", map);
configEnv.getPropertySources().addLast(propertySource);
}
}
return imports;
}
@Override
protected boolean isEnabled() {
return getEnvironment().getProperty("spring.cloud.discovery.enabled",
Boolean.class, Boolean.TRUE);
}
@Override
protected boolean hasDefaultFactory() {
return true;
}
}