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

Eureka的搭建可以參考這篇博客:【Spring Cloud 三】Eureka服務(wù)注冊與服務(wù)發(fā)現(xiàn)
3.1搭建Spring Boot Admin服務(wù)
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 #設(shè)置為fasle 不往eureka-server注冊,默認(rèn)為true
fetch-registry: true #應(yīng)用是否拉取服務(wù)列表到本地
registry-fetch-interval-seconds: 10 #為了緩解服務(wù)列表的臟讀問題,時(shí)間越短臟讀越少 性能相應(yīng)的消耗回答
instance: #實(shí)例的配置
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
hostname: localhost #主機(jī)名稱或者服務(wù)ip
prefer-ip-address: true #以ip的形式顯示具體的服務(wù)信息
lease-renewal-interval-in-seconds: 10 #服務(wù)實(shí)例的續(xù)約時(shí)間間隔
management:
endpoints:
web:
exposure:
include: '*' #暴露所有的監(jiān)控端點(diǎn) #如果一個(gè)服務(wù)需要被監(jiān)控,那么就要將自身的一些清苦(一些信息接口)暴露出去啟動(dòng)類
@SpringBootApplication
@EnableEurekaClient
@EnableAdminServer //#開啟admin服務(wù)端
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}啟動(dòng)admin服務(wù)效果

3.2 common-api
這個(gè)模塊是抽離出來的提供接口用于兩個(gè)服務(wù)之間的跨服務(wù)調(diào)用。之后由服務(wù)消費(fèi)者集成。
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 {
/**
* 一般遠(yuǎn)程調(diào)用的熔斷可以直接返回null
* @param id
* @return
*/
@Override
public Order getOrderByUserId(Integer id) {
return null;
}
}3.3服務(wù)消費(fèi)者
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>
<!--用于在應(yīng)用程序中添加各種監(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 #設(shè)置為fasle 不往eureka-server注冊
fetch-registry: true #應(yīng)用是否拉取服務(wù)列表到本地
registry-fetch-interval-seconds: 10 #為了緩解服務(wù)列表的臟讀問題,時(shí)間越短臟讀越少 性能相應(yīng)的消耗回答
instance: #實(shí)例的配置
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
hostname: localhost #主機(jī)名稱或者服務(wù)ip
prefer-ip-address: true #以ip的形式顯示具體的服務(wù)信息
lease-renewal-interval-in-seconds: 10 #服務(wù)實(shí)例的續(xù)約時(shí)間間隔
feign:
hystrix:
enabled: true #開啟熔斷
management:
endpoints:
web:
exposure:
include: '*'啟動(dòng)類
@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服務(wù)提供者
服務(wù)提供者與服務(wù)消費(fèi)者的主要區(qū)別是沒有依賴actuator以及對應(yīng)的暴露端點(diǎn)的配置。所以在admin的Web頁面中是不為看到服務(wù)提供者的詳細(xì)信息。
<!--用于在應(yīng)用程序中添加各種監(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 #設(shè)置為fasle 不往eureka-server注冊
fetch-registry: true #應(yīng)用是否拉取服務(wù)列表到本地
registry-fetch-interval-seconds: 10 #為了緩解服務(wù)列表的臟讀問題,時(shí)間越短臟讀越少 性能相應(yīng)的消耗回答
instance: #實(shí)例的配置
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
hostname: localhost #主機(jī)名稱或者服務(wù)ip
prefer-ip-address: true #以ip的形式顯示具體的服務(wù)信息
lease-renewal-interval-in-seconds: 10 #服務(wù)實(shí)例的續(xù)約時(shí)間間隔項(xiàng)目啟動(dòng)類
@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;
}
}服務(wù)整體啟動(dòng)之后的效果



由于Eureka服務(wù)沒有依賴actuator所以不能看到詳細(xì)信息。

四、 總結(jié)
- 本篇博客主要是對于Spring Boot Admin的基本認(rèn)識和基本運(yùn)用,通過本篇博客能夠?qū)pring Boot Admin有一個(gè)宏觀認(rèn)知和能夠快速上手。
- Spring Boot Admin還可以設(shè)置通知可報(bào)警,本篇博客并沒有涉及到。
到此這篇關(guān)于Spring Boot Admin 環(huán)境搭建與基本使用的文章就介紹到這了,更多相關(guān)Spring Boot Admin搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之教室預(yù)訂管理系統(tǒng)的實(shí)現(xiàn)
這是一個(gè)使用了java+SpringBoot+Maven+Vue+mysql開發(fā)的教室預(yù)訂管理系統(tǒng),是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有教室預(yù)訂管理該有的所有功能,感興趣的朋友快來看看吧2022-02-02
java使用篩選法求n以內(nèi)的素?cái)?shù)示例(java求素?cái)?shù))
這篇文章主要介紹了java使用篩選法求n以內(nèi)的素?cái)?shù)示例(java求素?cái)?shù)),需要的朋友可以參考下2014-04-04
使用mybatisPlus的queryWrapper做左聯(lián)接,內(nèi)聯(lián)接方式
本文介紹了如何使用Mybatis-Plus的QueryWrapper進(jìn)行SQL查詢,包括左連接、內(nèi)連接等操作,通過示例代碼展示了如何構(gòu)建復(fù)雜的SQL查詢,并將結(jié)果存儲在List對象中返回,希望給讀者提供參考2025-03-03
springboot如何讀取自定義properties并注入到bean中
這篇文章主要介紹了springboot讀取自定義properties并注入到bean中,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java16新特性record類使用細(xì)節(jié)示例詳解
這篇文章主要為大家介紹了Java16新特性record類使用細(xì)節(jié)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

