自定义

1. 自定义通知器

你可以实现 Notifier 接口,以Spring Beans的形式添加自己的通知程序,当然最好是继承 AbstractEventNotifierAbstractStatusChangeNotifier 的方式。

public class CustomNotifier extends AbstractEventNotifier {

	private static final Logger LOGGER = LoggerFactory.getLogger(LoggingNotifier.class);

	public CustomNotifier(InstanceRepository repository) {
		super(repository);
	}

	@Override
	protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
		return Mono.fromRunnable(() -> {
			if (event instanceof InstanceStatusChangedEvent) {
				LOGGER.info("Instance {} ({}) is {}", instance.getRegistration().getName(), event.getInstance(),
						((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());
			}
			else {
				LOGGER.info("Instance {} ({}) {}", instance.getRegistration().getName(), event.getInstance(),
						event.getType());
			}
		});
	}

}

2. 自定义HTTP请求头

为了监控应用而发送的actuator接口,如果你需要在其中添加自定义HTTP请求头的话,可以使用 HttpHeadersProvider 来轻松的实现:

@Bean
public HttpHeadersProvider customHttpHeadersProvider() {
	return (instance) -> {
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add("X-CUSTOM", "My Custom Value");
		return httpHeaders;
	};
}

3. 对请求和响应进行拦截

为了监控应用而发送的actuator接口,如果你需要拦截并修改其请求以及响应的话,可以通过实现 InstanceExchangeFilterFunction 来完成。 这对审核或者是添加额外的安全检查很有作用。

@Bean
public InstanceExchangeFilterFunction auditLog() {
	return (instance, request, next) -> next.exchange(request).doOnSubscribe((s) -> {
		if (HttpMethod.DELETE.equals(request.method()) || HttpMethod.POST.equals(request.method())) {
			log.info("{} for {} on {}", request.method(), instance.getId(), request.url());
		}
	});
}

4. 链接/嵌入外部页面

你可以非常简单的通过配置添加一个外部页面的链接甚至是直接将他们嵌入进来 (添加 iframe=true 参数)。

spring:
  boot:
    admin:
      ui:
        external-views:
          - label: "🚀"
            url: http://codecentric.de
            order: 2000

5. 自定义页面

您也可以向前端添加自定义页面。页面必须是一个 Vue.js 组件。

js文件还有css样式表必须放在classpath下的 /META-INF/spring-boot-admin-server-ui/extensions/{name}/ 路径,这样服务端才能拿到它们。 spring-boot-admin-sample-custom-ui 模块中包含了一个示例,该示例展示了如何配置maven来成功的构建一个模块。

要想自定义拓展将自己注册,可以调用 SBA.use() 然后还需要暴露 install() 函数,这在配置路由参数的时候会被ui调用。 install() 函数在注册页面和/或是回调时接收以下参数:

如果前端添加了顶级路由,那么后端也必须知道。 可以在 /META-INF/spring-boot-admin-server-ui/extensions/{name}/routes.txt 文件配置所有的顶级路由,每个路由占一行。

5.1. 添加顶级页面

下面是一个简单的顶级页面示例,其中列表出所有已注册的应用:

<template>
  <pre v-text="stringify(applications, null, 4)" />
</template>

<script>
export default {
  props: {
    applications: { (1)
      type: Array,
      required: true
    }
  },
  methods: {
    stringify: JSON.stringify
  }
};
</script>
1 如果你在组件中定义了 applications 属性,组件会将所有已注册的应用注入进来。
应用中还提供了一些很有用的方法和可用的实例对象。可以参考 application.js 以及 instance.js

这里展示了如何注册顶级试图。

SBA.use({
  install({viewRegistry}) {
    viewRegistry.addView({
      name: 'custom',  (1)
      path: '/custom', (2)
      component: custom, (3)
      label: 'Custom', (4)
      order: 1000, (5)
    });
  }
});
1 试图以及路由名称
2 试图的访问路径
3 引用的自定义组件,将会在路由上渲染
4 在顶部导航栏显示自定义试图的label
5 对试图进行排序。顶部导航栏中的试图是按照升序进行排序的。

添加了路由的 routes.txt 文件:

/custom/**

5.2. 查看自定义接口

下面是调用自定义接口的试图:

<template>
  <div class="custom">
    <p>Instance: <span v-text="instance.id" /></p>
    <p>Output: <span v-text="text" /></p>
  </div>
</template>

<script>
  export default {
    props: {
      instance: { (1)
        type: Object,
        required: true
      }
    },
    data: () => ({
      text: ''
    }),
    async created() {
      const response = await this.instance.axios.get('actuator/custom'); (2)
      this.text = response.data;
    }
  };
</script>

<style>
  .custom {
    font-size: 20px;
    width: 80%;
  }
</style>
1 如果你在组件中定义了 instance 属性,组件会将需要渲染的对象注入进来。
2 所有的实例中都提前配置好了 axios 供您访问,您只需要输入正确的路径和请求头就好了。

注册实例的工作方式与顶级试图差不多,但有一些额外的附加属性:

SBA.use({
  install({viewRegistry, vueI18n}) {
    viewRegistry.addView({
      name: 'instances/custom',
      parent: 'instances', (1)
      path: 'custom',
      component: customEndpoint,
      label: 'Custom',
      group: 'custom', (2)
      order: 1000,
      isEnabled: ({instance}) => instance.hasEndpoint('custom') (3)
    });

    vueI18n.mergeLocaleMessage('en', { (4)
      sidebar: {
        custom : {
          title : "My Custom Extensions"
        }
      }
    });
  }
});
1 这里的parent必须是 'instances' ,这样才能为单例提供新的自定义试图。
2 可以对试图进行分组。
3 如果添加了 isEnabled 选项,则可以动态判断是否为特定实例显示试图。
4 注册自定义i18n翻译
你可以以同分组、同名试图的方式对默认试图进行覆盖。

6. 自定义顶部Logo和标题

你可以使用下列属性对标题中的信息(例如显示登录信息或者是公司名称)进行自定义:

  • spring.boot.admin.ui.brand: 这段HTML会渲染到导航标题中,默认是 <img src="assets/img/icon-spring-boot-admin.svg"><span>Spring Boot Admin</span>。 默认情况下这里会显示SBA的logo,后面加上名称。 你可以在 /META-INF/spring-boot-admin-server-ui/ 这个路径(SBA默认会从这个路径下注册 ResourceHandler )下添加图片,或者用其它方式确保正确的提供图片(比如手动注册 ResourceHandler )。

  • spring.boot.admin.ui.title: 使用这个选项可以自定义浏览器窗口标题。

你可以自定义登录页面的图片。

  1. 将图片放在http可以访问到的资源目录下(例如 /META-INF/spring-boot-admin-server-ui/assets/img/)。

  2. 使用下面的属性来配置要使用的图标:

    • spring.boot.admin.ui.login-icon: 用作登录页面上的图标(例 assets/img/custom-login-icon.svg)。

8. 自定义Favicon

可以使用自定义的favicon,它也会用在桌面通知上。 当有一个或多个应用关闭时,Spring Boot Admin会使用不同的图标。

  1. 将favicon图标(.png 格式,最低192x192像素)放在http可以访问到的资源目录下(例如 /META-INF/spring-boot-admin-server-ui/assets/img/)。

  2. 下面的属性可以配置要使用的图标:

    • spring.boot.admin.ui.favicon: 配置默认图标(例 assets/img/custom-favicon.png) 。

    • spring.boot.admin.ui.favicon-danger: 在一个或多个服务关闭时使用这个图标 (例 assets/img/custom-favicon-danger.png)。

9. 自定义可用语言

要想筛选所有支持的语言,可以使用下面的选项:

  • spring.boot.admin.ui.available-languages: 配置现有语言的过滤器 (例如 en,de out of existing de,en,fr,ko,pt-BR,ru,zh)。

10. 显示/隐藏页面

要想在导航栏中隐藏试图,可以很简单的实现:

spring:
  boot:
    admin:
      ui:
        view-settings:
          - name: "journal"
            enabled: false