springboot-admin 监控

齐俊贤
2023-12-01

项目里面用着的完整的监控 直接上干货

创建监控项目admin 

pom.xml

<parent>
		<groupId>com.wkxhotel</groupId>
		<artifactId>wkxhotel-parent</artifactId>
		<version>1.0</version>
		<relativePath>../pom.xml</relativePath>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<boot.admin.version>1.5.5</boot.admin.version>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-server</artifactId>
			<version>${boot.admin.version}</version>
		</dependency>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-server-ui</artifactId>
			<version>${boot.admin.version}</version>
		</dependency>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-client</artifactId>
			<version>${boot.admin.version}</version>
		</dependency>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-server-ui-hystrix</artifactId>
			<version>${boot.admin.version}</version>
		</dependency>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-server-ui-login</artifactId>
			<version>${boot.admin.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</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-thymeleaf</artifactId>
		</dependency>
		<!-- 用于admin监控 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR4</version><!-- admin对与eureka的依赖 有版本的要求 Dalston.SR4 -->
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>UTF-8</encoding>
					<useDefaultDelimiters>true</useDefaultDelimiters>
				</configuration>
			</plugin>
		</plugins>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
		<testResources>
			<testResource>
				<directory>src/test/resources</directory>
				<filtering>true</filtering>
			</testResource>
		</testResources>
	</build>

	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<profiles.active>dev</profiles.active>
				<maven.test.skip>true</maven.test.skip>
				<scope.jar>compile</scope.jar>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>test</id>
			<properties>
				<profiles.active>test</profiles.active>
				<maven.test.skip>true</maven.test.skip>
				<scope.jar>provided</scope.jar>
			</properties>
		</profile>
		<profile>
			<id>prod</id>
			<properties>
				<profiles.active>prod</profiles.active>
				<maven.test.skip>true</maven.test.skip>
				<scope.jar>provided</scope.jar>
			</properties>
		</profile>
	</profiles>

配置文件里面的内容

server:
  port: 9001
#########eureka############
eureka:
  client:
    service-url:
      defaultZone: http://admin:1q2w3e4r@localhost:1111/eureka/
#########admin############ 
management:
  security:
    enabled: false
security:
  basic:
    enabled: false
  user:
    name: admin
    password: XXXX
spring:
  application:
    name: wkxhotel-monitoring-admin
  boot:
    admin:
      routes:
        endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream
      notify:
        mail:
          ignore-changes: OFFLINE:UP
          to: 需要发送的邮箱地址
          from: XXXXX@163.com 
      monitor:
        connect-timeout: 5000000  
        read-timeout: 5000000  
  mail:
    host: smtp.163.com
    username: XXXXX@163.com 
    password: xxxxxx(授权码)
    properties:
      mail:
        port: 465
        smtp:
         auth: true
         starttls:
          enable: true
          required: true 

一开始我用的试qq邮箱,虽然可以预警发送邮件,但是发送完成之后会抱一个socket链接的错误,导致在admin的页面展示的时候老是down up 状态规律性的变动,后来换成163的邮箱 就可以了

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	@Override
	public void configure(WebSecurity web) throws Exception {
		// 忽略css.jq.img等文件 监控访问的路径
		web.ignoring().antMatchers("/**.html", "/**.css", "/img/**", "/**.js", "/third-party/**");
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {

		http.csrf().disable().authorizeRequests()// 开放需要认证的接口 
				.antMatchers("/login", "/api/**", "/**/heapdump", "/**/loggers", "/**/liquibase", "/**/logfile",
						"/**/flyway", "/**/auditevents", "/**/jolokia", "/health","/metrics/**")
				.permitAll() // 放开"/api/**":为了给被监控端免登录注册并解决Log与Logger冲突
				.and().authorizeRequests().antMatchers("/**").hasRole("USER").antMatchers("/**").authenticated().and()
				.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll().defaultSuccessUrl("/")
				.and().logout().deleteCookies("remove").logoutSuccessUrl("/login.html").permitAll().and().httpBasic();
	}
}

在所有被监督的项目中只需要设置2个地方:1 修改配置文

management.security.enabled= false

2.pom.xml 引入三个jar包

	<!-- 用于admin监控 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
所有的项目的监控试通过eureka来实现的,admin监控所有注册在eureka上面的项目,也包括它自己.如果监控要登陆才能访问的项目,必须要做处理,本人在网上找了好久没有什么好的方案,只能把被监控的项目的接口放开(shiro设置anon)



 类似资料: