上一篇https://blog.csdn.net/zh_9590/article/details/100050870写了配置中心,接下来学习监控中心admin的配置。
实际服务部署中,生产的服务肯定会有服务状态监控的,多数都是服务级别的监控,有甚的是业务层级的服务监控,这种监控是比不可少的,所以这块是必须的。
在这里强调下,这一系列的都是关于我经历的公司采用springcloud进行的自己设计的,有些可能不合理什么的,不过这些都是一个累计的阶段,对于自己的阅历只能慢慢积累。
1、先看看监控的代码结构
springboot项目的常规常矩的使用,主要干货在配置文件。由于监控属于springcloud层次的,所以配置文件不走配置中心,配置中心主要管理业务层次的服务的配置。
2、pom文件的
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>ms-admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ms-admin</name>
<description>微服务监控中心</description>
<parent>
<groupId>com.ms.zh</groupId>
<artifactId>ms-platform</artifactId>
<version>0.0.1</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--Spring Security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jetty -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、application的配置文件
#spring基本配置
spring:
profiles:
active: @env@
info:
app:
name: "@project.name@"
description: "@project.description@"
version: "@project.version@"
spring-boot-version: "@project.version@"
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
4、application-dev的配置:
server:
port: 9040
spring:
application:
name: ${project.name}
security:
user:
#监控中心的登录名
name: 'admin'
#监控中心的登录密码
password: 'admin'
boot:
admin:
discovery:
ignored-services: ${project.name}
notify:
mail:
#从那里发
from: xxxxxxxx
#发到哪里
to: xxxxxx
#配置是否启用邮件通知 false是不起用
enabled: true
mail:
#端口,用哪个邮箱就采用哪个host,
host: smtp.qq.com
username: xxxxxx
#授权码 不知道怎么搞可以百度
password: vvmbyuujawbhfhad
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
#eureka注册中心
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9010/eureka/
instance:
preferIpAddress: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
这个里边有必要说明下这个邮箱配置的注意点,
① mail.host 这个是不同类型的邮箱使用的不一样,就自己目前知道的
smtp.qq.com qq邮箱的,smtp.163.com 163邮箱的
② mail.password 授权码,这个在qq或者163的好处理,在邮箱的设置里边就可以找到,自行百度即可,不过也有公司自己的
邮箱服务器,这个授权码一般都是邮箱的密码,这块需要注意下。
③ 要发送的邮箱可以多配置
5、启动类
package com.ms.zh.admin;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@Slf4j
@SpringBootApplication
@EnableEurekaClient
@EnableAdminServer
public class MsAdminApplication {
public static void main(String[] args) {
long start = System.currentTimeMillis();
SpringApplication.run(MsAdminApplication.class, args);
long end = System.currentTimeMillis();
log.info("微服务监控管理中心启动成功,耗时:{}......", (end - start));
}
}
很一般的启动类,里边就两个注解需要注意下
EnableEurekaClient 服务也是需要注册到eureka注册中心的,
EnableAdminServer 监控中心服务注解
6、SecuritySecureConfig类内容:
package com.ms.zh.admin.config;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
/**
* @Desc v
**/
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
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();
// @formatter:on
}
}
检测自己的成果,
依次启动注册中心,配置中心,监控中心,
访问监控中心地址:http://127.0.0.1:9040/
输入账号密码,登录后就能看到自己的服务状态,由于此时只有一个配置中心在,所以停掉配置中心,自己的接收告警的邮件就只有一个配置中心的告警邮件。
监控中心只能监控除注册中心和自己服务以外的服务,因为采用springcloud的admin,是基于注册中心来达到获取所有服务状态的,所以,注册中心挂了,它是无法正常工作的。
检测自身服务这块,这块暂时没搞明白,个人感觉就是,监工不能监督自己把,哈哈,后续会去了解这块。
由于项目组织结构功能已经具备有,对于网关和基础服务,由于网关是基于基础服务来进行路由熔断跳转的,所以,从下次文章就开始写关于基础服务的构建了。
点点滴滴的积累。