SpringBoot应用监控

SpringBoot应用监控

应用端点监控

pom.xml中引入以下依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

在启动类父文件夹下新建SecurityConfig.java

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatcher(EndpointRequest.toAnyEndpoint())
                .authorizeRequests()
                .anyRequest().hasRole("ADMIN")
                .and()
                .httpBasic();
    }
}

在启动类父文件夹下新建XiaoliuInfo.java

@Component
public class XiaoliuInfo implements InfoContributor {

    @Override
    public void contribute(Info.Builder builder) {
        Map<String,Object> info=new HashMap<>();
        info.put("email","2330163321@qq.com" );
        builder.withDetail("author",info );
    }
}

在application.properties中做以下配置

management.endpoint.shutdown.enabled=true
#management.endpoints.enabled-by-default=false
#management.endpoint.info.enabled=true
management.endpoints.web.exposure.include=*

spring.security.user.name=xiaoliu
spring.security.user.password=123
spring.security.user.roles=ADMIN
#management.endpoints.web.base-path=/xiaoliu
#management.endpoints.web.path-mapping..health=xiaoliu-health

management.endpoints.web.cors.allowed-origins=http://localhost:8081
management.endpoints.web.cors.allowed-methods=GET,POST

management.endpoint.health.show-details=when_authorized
spring.redis.host=localhost

info.app.encoding=@project.build.sourceEncoding@
info.app.java.source=@java.version@
info.app.java.target=@java.version@
info.author.name=xiaoliu
info.author.address=www.xiaoliu.com

management.info.git.mode=full

启动项目,登录并且访问Info端点与Health端点

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I1mkiap4-1588397754718)(C:\Users\MI\AppData\Roaming\Typora\typora-user-images\1588396751197.png)]

监控信息可视化

新建admin项目,pom.xml中加入以下依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

启动类上添加以下代码

@SpringBootApplication
@EnableAdminServer
public class AdminApplication {

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

}

访问http://localhost:8080/index.html

就可以实现对监控信息的可视化

新建client项目,pom.xml中引入以下依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

在application.properties中做以下配置

management.endpoints.web.exposure.include=*

server.port=8081
spring.boot.admin.client.url=http://localhost:8080

启动项目,任然访问http://localhost:8080/index.html

就可以实现对此项目的监控信息可视化

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LpDbErZq-1588397754724)(C:\Users\MI\AppData\Roaming\Typora\typora-user-images\1588397670126.png)]

邮件报警

admin项目中的application.properties中添加以下配置便可实现

spring.mail.host=smtp.qq.com
spring.mail.port=587
spring.mail.username=2330163321@qq.com
spring.mail.password=wjyexmwgemcldide
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true

spring.boot.admin.notify.mail.to=1041344803@qq.com
spring.boot.admin.notify.mail.from=2330163321@qq.com
spring.boot.admin.notify.mail.ignore-changes=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值