spring boot admin 自定义
发布日期:2021-08-13 07:44:58 浏览次数:8 分类:技术文章

本文共 4366 字,大约阅读时间需要 14 分钟。

自定义通知事件

  • 实现notifier接口
// 此接口在服务端package de.codecentric.boot.admin.server.notify;public interface Notifier {    Mono
notify(InstanceEvent var1);}复制代码
  • AbstractEventNotifier
  • AbstractStatusChangeNotifier

可以通过实现Notifier接口,或者继承AbstractEventNotifier或者AbstractStatusChangeNotifier来实现自定义通知事件。其集成关系如下图所示:

以下为自定义实现一个通知程序。

public class CustomNotifier  extends AbstractEventNotifier {    private static final Logger LOGGER = LoggerFactory.getLogger(LoggingNotifier.class);    public CustomNotifier(InstanceRepository repository) {        super(repository);    }    @Override    protected Mono
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()); } }); }}复制代码

注入自定义的http请求头

如过想将自定义的http请求头信息注入到监控服务端,需要实现HttpHeadersProvider

package de.codecentric.boot.admin.server.web.client;public interface HttpHeadersProvider {    HttpHeaders getHeaders(Instance var1);}复制代码

自定义实现如下:

@Configuration@EnableAutoConfiguration@EnableAdminServer@Import({SecurityPermitAllConfig.class, SecuritySecureConfig.class, NotifierConfig.class})public class SpringBootAdminServletApplication {    private static final Logger log = LoggerFactory.getLogger(SpringBootAdminServletApplication.class);    public static void main(String[] args) {        SpringApplication.run(SpringBootAdminServletApplication.class, args);    }    // tag::customization-instance-exchange-filter-function[]    @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());            }        });    }    // end::customization-instance-exchange-filter-function[]    @Bean    public CustomNotifier customNotifier(InstanceRepository repository) {        return new CustomNotifier(repository);    }    @Bean    public CustomEndpoint customEndpoint() {        return new CustomEndpoint();    }    // tag::customization-http-headers-providers[]    @Bean    public HttpHeadersProvider customHttpHeadersProvider() {        return instance -> {            HttpHeaders httpHeaders = new HttpHeaders();            httpHeaders.add("X-CUSTOM", "My Custom Value");            return httpHeaders;        };    }    // end::customization-http-headers-providers[]}复制代码

拦截请求和响应

可以通过实现InstanceExchangeFilterFunction接口来拦截和修改对受监视应用程序的执行器端点的请求和响应。这对于审核或添加一些额外的安全检查非常有用。

如上代码中的InstanceExchangeFilterFunction auditLog()

链接或者嵌入外部页面

只需要需改yml文件中的配置即可

# tag::自定义外部视图spring:  boot:    admin:      ui:        external-views:          - label: "?"            url: http://codecentric.de            order: 2000# end::customization-external-views复制代码

自定义视图

可以像ui添加自定义视图,但此视图必须为vue.js的组件。 JavaScript-Bundle和CSS-Stylesheet必须放在类路径上,/META-INF/spring-boot-admin-server-ui/extensions/{name}/以便服务器可以处理他们。可以做为一个参考。

自定义扩展需要通过调用自身的SBA.use()方法注册,并需要公开一个install()函数,该函数在设置路由时由ui调用。该install()函数接收以下参数以注册视图或回调:

  • 引用{github-src} /spring-boot-admin-server-ui/src/main/frontend/viewRegistry.js [viewRegistry]的对象

  • 引用{github-src} /spring-boot-admin-server-ui/src/main/frontend/store.js [applicationStore]的对象

  • 引用全局Vue [Vue]的对象

  • 引用{github-src} /spring-boot-admin-server-ui/src/main/frontend/utils/axios.js [axios]的对象

如果将新的顶级路由添加到前端,则后端也必须知道它们。添加一个/META-INF/spring-boot-admin-server-ui/extensions/{name}/routes.txt包含所有新的顶层路线(每条线路一条路线)。

// 注册组件SBA.use({  install({viewRegistry}) {    viewRegistry.addView({      name: 'custom',  //<1>      path: '/custom', //<2>      component: custom, //<3>      label: 'Custom', //<4>      order: 1000, //<5>    });  }});复制代码

添加父级

如下是一个简单的父级视图,用来列出所有的注册的模块。

// spring-boot-admin-sample-custom-ui/src/custom.vue
复制代码

注册组件如上,在index.js中,并添加路由信息,routes.txt中

/custom/**复制代码

可视化自定义

以下是自定义可视化端点视图,

参考

转载于:https://juejin.im/post/5d37f1726fb9a07ec3742fc1

转载地址:https://blog.csdn.net/weixin_30679823/article/details/100090805 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【愣锤笔记】基于vue的进阶散点干货
下一篇:MySQL8.0.11的安装和Navicat连接mysql

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月01日 02时29分41秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【C++】攻克哈希表(unordered_map) 2019-04-27
转:【答学员问】- 该如何根据岗位学习相关技能 2019-04-27
转:【答学员问】有什么经验教训,是你在面试很多次之后才知道的? 2019-04-27
消息队列:解耦、异步、削峰,现有MQ对比以及新手入门该如何选择MQ? 2019-04-27
【奇技淫巧】-- 三角形最小路径和 2019-04-27
【小技巧】argc和argv的用法 2019-04-27
学不下去了怎么办? 2019-04-27
二叉树的前中后序遍历(迭代法)(带动画) 2019-04-27
【小技巧】【XShell】【Xftp】Windows桌面与Linux虚拟机互传文件 2019-04-27
【redis入门】Centos下安装redis 2019-04-27
【redis入门】redis安装后相关知识串讲 2019-04-27
【redis】来吧,展示一下redis 发布-订阅模式 2019-04-27
讲通C/C++预编译/条件编译指令 #ifdef,#ifndef,#endif,#define,… 2019-04-27
【redis6.0.6】redis源码慢慢学,慢慢看 -- 第二天:空间配置(zmalloc) 2019-04-27
当下热点词再学:redis缓存预热、更新、降级,限流 2019-04-27
【redis6.0.6】redis源码慢慢学,慢慢看 -- 第五天:adlist 2019-04-27
别抖,OK? 操作系统抖动现象、网络抖动与延迟、函数抖动之防抖与节流,串讲 2019-04-27
第六天:网络处理(anet部分)-- redis源码慢慢学,慢慢看【redis6.0.6】 2019-04-27
通过域名获取主机IP -- struct addrinfo 2019-04-27
【C++】算法集锦(8):从两数和问题拓展到一百数和问题 2019-04-27