springboot-admin集成到现有springboot项目中

刚好最近疫情原因,导致不方便进业主方场地进行多系统开发,就学习了springcloud全家桶。
刚好学到springboot-admin系统监控,发现可以单独拆出来用,就顺手装到自己项目上去玩了。

一、springboot-admin用于管理和监控SpringBoot应用程序。

springboot-admin分为服务端Spring Boot Admin Server和客户端Spring Boot Admin Client,使用时,先开启服务端,然后启动客户端自动注册就可以使用啦。本身集成还是很简单的,一般常用的功能如下:
常见的功能或者监控如下:

  • 显示健康状况
  • 显示详细信息,例如
  • JVM和内存指标
  • micrometer.io指标
  • 数据源指标
  • 缓存指标
  • 显示构建信息编号
  • 关注并下载日志文件
  • 查看jvm系统和环境属性
  • 查看Spring Boot配置属性
  • 支持Spring Cloud的postable / env-和/ refresh-endpoint
  • 轻松的日志级管理
  • 与JMX-beans交互
  • 查看线程转储
  • 等等

界面如下:
在这里插入图片描述

二、接入使用

服务端

1、创建一个普通的springboot项目,引入如下依赖:

<!-- springBoot admin 监控 -->
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
        <version>2.1.0</version>
    </dependency>

<!-- 开启验证-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <!-- 引入Web场景 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

2、然后配置端口及验证账密码:

spring.application.name=xxx-admin
server.port=29000
#配置admin工程登录的账号密码
spring.security.user.name=zc
spring.security.user.password=123456

3、接着在启动类配置注释及放行页面,放行页面具体不用管,按官网的抄就完事了

@SpringBootApplication
@EnableAdminServer
public class AdminApplication {

   public static void main(String[] args) {
   	SpringApplication.run(AdminApplication.class, args);
   }

   //配置安全校验
   @Configuration
   @Order(Ordered.LOWEST_PRECEDENCE)
   public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
   	private final String adminContextPath;

   	public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
   		this.adminContextPath = adminServerProperties.getContextPath();
   	}

   	@Override
   	protected void configure(HttpSecurity http) throws Exception {
   		SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
   		successHandler.setTargetUrlParameter("redirectTo");
   		http.authorizeRequests()
   				.antMatchers(adminContextPath + "/assets/**").permitAll()
   				.antMatchers(adminContextPath + "/login").permitAll()
   				.anyRequest().authenticated()
   				.and()
   				.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
   				.logout().logoutUrl(adminContextPath + "/logout").and()
   				.httpBasic().and()
   				.csrf().disable();
   	}
   }
}

客户端

1、在现有的项目里,加入如下依赖:

    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-client</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

2、配置连接参数等

#要展示的应用名称
spring.application.name= springboot
#admin工程的url
spring.boot.admin.client.url=http://localhost:29000
#展示全部细节信息
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*
#允许admin工程远程停止本应用
management.endpoint.shutdown.enabled=true
#admin工程的账号密码
spring.boot.admin.client.username=zc
spring.boot.admin.client.password=123456

**3、先启动服务器端,然后启动客户端即可,接入可能会花费点时间,耐心等一会儿即可。**在这里插入图片描述

踩的坑

1、服务端的springboot和springboot-admin版本一定要要匹配,谁都谁低都可能炸锅,还基本排查处理不了,反正我是不想花费那么多时间去排查,累。我用的springboot2.1.1和admin的2.1.0,也可以适当高一些,这个版本好像没有中文界面,看着麻烦。
2、客户端和服务端的admin版本一定要一样,不然又会遇到神奇的bug。
3、连不上,注意检查下端口是否开放,是否一致,账号密码是否一致,客户端是否暴露了端点等。
4、很多文章列举了邮件通知,但中国人一个是不喜欢用邮件,一个是定义麻烦,可以加上自定义通知。

自定义通知类,加入可服务端代码里即可。

package com.imooc.ecommerce.notifier;

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import de.codecentric.boot.admin.server.notify.AbstractEventNotifier;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @packageName:e-commerce-springcloud
 * @ClassName:ZcNotifier
 * @auther:zc
 * @date 2022/3/30 14:01
 * 通知告警信息
 * 主要是依靠继承AbstractEventNotifier,实现通知方法
 */
@Component
public class ZcNotifier extends AbstractEventNotifier {

    protected ZcNotifier(InstanceRepository repository) {
        super(repository);
    }

    /**
     * 实现事件通知
     * @param event 哪一个事件
     * @param instance 发生了什么事
     * @return 这里你想发短信也好,发邮件也罢,单纯的记录也行,都随意了,自己拿到数据,干嘛都行
     */
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() ->{
            if (event instanceof InstanceStatusChangedEvent) {
                System.out.println(instance.getRegistration().getName()+event.getInstance()+
                        ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());

            } else {
                System.out.println(instance.getRegistration().getName()+event.getInstance()+
                        event.getType());

            }
        });
    }
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,对于集成 xxx-job 到 Spring Boot 项目,你可以按照以下步骤进行操作: 1. 添加 xxx-job 的依赖: ``` <dependency> <groupId>com.xxl.job</groupId> <artifactId>xxl-job-core</artifactId> <version>${xxl.job.version}</version> </dependency> ``` 其,`${xxl.job.version}` 是需要替换成你所使用的 xxx-job 的版本号。 2. 在 Spring Boot 项目添加配置文件 `application.properties`,并添加 xxx-job 的配置: ``` # xxl-job config xxl.job.admin.addresses=http://localhost:8080/xxl-job-admin xxl.job.executor.appname=executor-sample xxl.job.executor.address= xxl.job.executor.ip= xxl.job.executor.port=9999 xxl.job.accessToken= xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler xxl.job.executor.logretentiondays=7 ``` 其,`xxl.job.admin.addresses` 是 xxx-job 的管理地址,`xxl.job.executor.appname` 是执行器的名称,可以自定义,其他配置项可以根据实际情况进行修改。 3. 创建任务类并实现 `IJobHandler` 接口: ``` @Component public class DemoJobHandler extends IJobHandler { @Override public ReturnT<String> execute(String param) throws Exception { System.out.println("Hello, xxl-job!"); return ReturnT.SUCCESS; } } ``` 4. 在 Spring Boot 项目添加任务调度配置类: ``` @Configuration public class XxlJobConfig { @Autowired private DemoJobHandler demoJobHandler; @Bean public XxlJobSpringExecutor xxlJobExecutor() { XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); xxlJobSpringExecutor.setAdminAddresses("http://localhost:8080/xxl-job-admin"); xxlJobSpringExecutor.setAppname("executor-sample"); xxlJobSpringExecutor.setLogPath("/data/applogs/xxl-job/jobhandler"); xxlJobSpringExecutor.setLogRetentionDays(7); xxlJobSpringExecutor.setJobHandlers(demoJobHandler); return xxlJobSpringExecutor; } } ``` 其,`demoJobHandler` 是步骤 3 创建的任务类。 5. 在 xxx-job 的管理平台上添加任务,并配置执行器为 `executor-sample`。 至此,你已经成功地将 xxx-job 集成Spring Boot 项目,并创建了一个简单的任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值