Spring Boot Admin 環(huán)境搭建與基本使用詳解
一、Spring Boot Admin是什么
它是用于監(jiān)控和管理Spring Boot應用程序的開源工具。它為開發(fā)人員或者是運維人員提供了友好的Web界面。可以實時監(jiān)控和管理部署在不同環(huán)境中的Spring Boot應用。
二、提供了那些功能
- 應用程序監(jiān)控:可以顯示程序的基本信息:內存使用情況、線程信息。
- 應用程序管理:可以管理監(jiān)控的應用:動態(tài)配置日志的級別。
- 通知和報警:可以配置通知和警報,當應用程序出現問題或者叨叨預定的閾值時及時通知相關人員。
- 微服務支持:可以適用微服務架構,一次性監(jiān)控和管理多個微服務應用。
- 安全性:可以與Spring Security集成,實現對監(jiān)控和管理界面的訪問控制。
三、 使用Spring Boot Admin
示例項目整體結構:這里為什么要使用Eureka,主要是想體現復用的思想。所有服務都注冊到了Eureka之后,而Spring Boot Admin只要集成了Eureka之后就能夠獲取到所有的服務信息注冊信息。能夠對所有注冊到Eureka中的服務進行監(jiān)控和管理。

Eureka的搭建可以參考這篇博客:【Spring Cloud 三】Eureka服務注冊與服務發(fā)現
3.1搭建Spring Boot Admin服務
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wangwei</groupId>
<artifactId>admin-server-05</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>05-admin-server</name>
<description>05-admin-server</description>
<properties>
<java.version>8</java.version>
<spring-boot-admin.version>2.3.0</spring-boot-admin.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>yml配置文件
##???
server:
port: 10086 #端口號 0-65535
spring:
application:
name: admin-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
register-with-eureka: true #設置為fasle 不往eureka-server注冊,默認為true
fetch-registry: true #應用是否拉取服務列表到本地
registry-fetch-interval-seconds: 10 #為了緩解服務列表的臟讀問題,時間越短臟讀越少 性能相應的消耗回答
instance: #實例的配置
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
hostname: localhost #主機名稱或者服務ip
prefer-ip-address: true #以ip的形式顯示具體的服務信息
lease-renewal-interval-in-seconds: 10 #服務實例的續(xù)約時間間隔
management:
endpoints:
web:
exposure:
include: '*' #暴露所有的監(jiān)控端點 #如果一個服務需要被監(jiān)控,那么就要將自身的一些清苦(一些信息接口)暴露出去啟動類
@SpringBootApplication
@EnableEurekaClient
@EnableAdminServer //#開啟admin服務端
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}啟動admin服務效果

3.2 common-api
這個模塊是抽離出來的提供接口用于兩個服務之間的跨服務調用。之后由服務消費者集成。
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>feign-project</artifactId>
<groupId>com.wangwei</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common-api</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.wangwei</groupId>
<artifactId>project-domain</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
</project>feign
@FeignClient(value = "order-service",fallback = UserOrderFeignHystrix.class)
public interface UserOrderFeign {
@GetMapping("order/getOrderByUserId/{id}")
Order getOrderByUserId (@PathVariable("id")Integer id);
}hystrix
@Component
public class UserOrderFeignHystrix implements UserOrderFeign {
/**
* 一般遠程調用的熔斷可以直接返回null
* @param id
* @return
*/
@Override
public Order getOrderByUserId(Integer id) {
return null;
}
}3.3服務消費者
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>feign-project</artifactId>
<groupId>com.wangwei</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-center</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--用于在應用程序中添加各種監(jiān)控和管理功能-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.wangwei</groupId>
<artifactId>common-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>yml配置文件
server:
port: 8081
spring:
application:
name: user-service
eureka:
client:
service-url: #??????
defaultZone: http://localhost:8761/eureka
register-with-eureka: true #設置為fasle 不往eureka-server注冊
fetch-registry: true #應用是否拉取服務列表到本地
registry-fetch-interval-seconds: 10 #為了緩解服務列表的臟讀問題,時間越短臟讀越少 性能相應的消耗回答
instance: #實例的配置
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
hostname: localhost #主機名稱或者服務ip
prefer-ip-address: true #以ip的形式顯示具體的服務信息
lease-renewal-interval-in-seconds: 10 #服務實例的續(xù)約時間間隔
feign:
hystrix:
enabled: true #開啟熔斷
management:
endpoints:
web:
exposure:
include: '*'啟動類
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class,args);
}
}controller
@RestController
public class UserController {
@Autowired
private UserOrderFeign userOrderFeign;
@GetMapping("findOrder")
public Order findOrder(){
return userOrderFeign.getOrderByUserId(1);
}
}3.4服務提供者
服務提供者與服務消費者的主要區(qū)別是沒有依賴actuator以及對應的暴露端點的配置。所以在admin的Web頁面中是不為看到服務提供者的詳細信息。
<!--用于在應用程序中添加各種監(jiān)控和管理功能-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>feign-project</artifactId>
<groupId>com.wangwei</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-center</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.wangwei</groupId>
<artifactId>common-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>yml配置文件
server:
port: 8080
spring:
application:
name: order-service
eureka:
client:
service-url: #??????
defaultZone: http://localhost:8761/eureka
register-with-eureka: true #設置為fasle 不往eureka-server注冊
fetch-registry: true #應用是否拉取服務列表到本地
registry-fetch-interval-seconds: 10 #為了緩解服務列表的臟讀問題,時間越短臟讀越少 性能相應的消耗回答
instance: #實例的配置
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
hostname: localhost #主機名稱或者服務ip
prefer-ip-address: true #以ip的形式顯示具體的服務信息
lease-renewal-interval-in-seconds: 10 #服務實例的續(xù)約時間間隔項目啟動類
@SpringBootApplication
@EnableEurekaClient
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class,args);
}
}controller
@RestController
public class OrderController {
@GetMapping("order/getOrderByUserId/{id}")
Order getOrderByUserId (@PathVariable("id")Integer id){
System.out.println(id);
Order order=Order.builder()
.name("青椒肉絲蓋飯")
.price(15D)
.orderId(1)
.build();
return order;
}
}服務整體啟動之后的效果



由于Eureka服務沒有依賴actuator所以不能看到詳細信息。

四、 總結
- 本篇博客主要是對于Spring Boot Admin的基本認識和基本運用,通過本篇博客能夠對Spring Boot Admin有一個宏觀認知和能夠快速上手。
- Spring Boot Admin還可以設置通知可報警,本篇博客并沒有涉及到。
到此這篇關于Spring Boot Admin 環(huán)境搭建與基本使用的文章就介紹到這了,更多相關Spring Boot Admin搭建內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java畢業(yè)設計實戰(zhàn)之教室預訂管理系統的實現
這是一個使用了java+SpringBoot+Maven+Vue+mysql開發(fā)的教室預訂管理系統,是一個畢業(yè)設計的實戰(zhàn)練習,具有教室預訂管理該有的所有功能,感興趣的朋友快來看看吧2022-02-02
使用mybatisPlus的queryWrapper做左聯接,內聯接方式
本文介紹了如何使用Mybatis-Plus的QueryWrapper進行SQL查詢,包括左連接、內連接等操作,通過示例代碼展示了如何構建復雜的SQL查詢,并將結果存儲在List對象中返回,希望給讀者提供參考2025-03-03
springboot如何讀取自定義properties并注入到bean中
這篇文章主要介紹了springboot讀取自定義properties并注入到bean中,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

