Spring Boot Admin集成到spring项目

130 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

Spring Boot Admin 是一个管理和监控Spring Boot 应用程序的可视化监控软件

1.而在springboot中使用起来也十分方便,首先我们先创建一个项目并添加web依赖和Spring Boot Admin依赖

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


		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-server</artifactId>
			<version>2.0.2</version>
		</dependency>
	</dependencies>

然后我们再在项目的启动类上添加@EnableAdminServer注解 用来开启Spring Boot Admin服务

import org.springframework.boot.autoconfigure.SpringBootApplication;

import de.codecentric.boot.admin.server.config.EnableAdminServer;

@SpringBootApplication
@EnableAdminServer
public class SpringBootAdminApplication {

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

}

这样在项目启动的时候就自动启动了springbootadmin,接下来运行项目,然后在浏览器上输入http://localhost:8080/#/applications就可以看到这个页面

​​​​

但是这样子我们只是创建了一个Spring Boot Admin的监控平台 而我们怎么去实现监控我们其它的项目呢?

首先我们再创建另一个项目,并把它注册进我们的Spring Boot Admin 里面

首先我们先添加依赖

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

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

		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-client</artifactId>
			<version>2.0.2</version>
		</dependency>

其中最主要的是spring-boot-admin-starter-client 和 spring-boot-starter-actuator这两个用来注册和发现服务端的依赖

然后再在我们的application.properties里面进行配置

management.endpoints.web.cors.allowed-origins=*
management.endpoints.web.cors.allowed-methods==GET,POST
management.endpoint.health.show-details=always
info.author.name=A517
server.port=8081
spring.boot.admin.client.url=http://localhost:8080

首先我们的client项目的端口号要进行修改,然后通过配置

1.management.endpoints.web.exposure.include=//暴露端口信息
2.management.endpoints.web.cors.allowed-origins=
//允许跨域
3.management.endpoints.web.cors.allowed-methods==GET,POST//允许方法
4.management.endpoint.health.show-details=always//通过权限配置,有never和when-authorized和always三种属性来进行查看权限

然后运行项目,就会看到项目的信息已被注册进去 ​​​ ​​​

PS:不过如果在client项目配置了安全框架的话,在安全配置哪里把client的 .antMatchers("/actuator/").permitAll()给暴露**就好了,