Spring Boot Admin 环境搭建与基本使用详解
一、Spring Boot Admin是什么
它是用于监控和管理Spring Boot应用程序的开源工具。它为开发人员或者是运维人员提供了友好的Web界面。可以实时监控和管理部署在不同环境中的Spring Boot应用。
二、提供了那些功能
- 应用程序监控:可以显示程序的基本信息:内存使用情况、线程信息。
- 应用程序管理:可以管理监控的应用:动态配置日志的级别。
- 通知和报警:可以配置通知和警报,当应用程序出现问题或者叨叨预定的阈值时及时通知相关人员。
- 微服务支持:可以适用微服务架构,一次性监控和管理多个微服务应用。
- 安全性:可以与Spring Security集成,实现对监控和管理界面的访问控制。
三、 使用Spring Boot Admin
示例项目整体结构:这里为什么要使用Eureka,主要是想体现复用的思想。所有服务都注册到了Eureka之后,而Spring Boot Admin只要集成了Eureka之后就能够获取到所有的服务信息注册信息。能够对所有注册到Eureka中的服务进行监控和管理。
Eureka的搭建可以参考这篇博客:【Spring Cloud 三】Eureka服务注册与服务发现
3.1搭建Spring Boot Admin服务
pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <? 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配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ##??? 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 #服务实例的续约时间间隔 management: endpoints: web: exposure: include: '*' #暴露所有的监控端点 #如果一个服务需要被监控,那么就要将自身的一些清苦(一些信息接口)暴露出去 |
启动类
1 2 3 4 5 6 7 8 | @SpringBootApplication @EnableEurekaClient @EnableAdminServer //#开启admin服务端 public class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication. class , args); } } |
启动admin服务效果
3.2 common-api
这个模块是抽离出来的提供接口用于两个服务之间的跨服务调用。之后由服务消费者集成。
pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <? 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
1 2 3 4 5 | @FeignClient (value = "order-service" ,fallback = UserOrderFeignHystrix. class ) public interface UserOrderFeign { @GetMapping ( "order/getOrderByUserId/{id}" ) Order getOrderByUserId ( @PathVariable ( "id" )Integer id); } |
hystrix
1 2 3 4 5 6 7 8 9 10 11 12 | @Component public class UserOrderFeignHystrix implements UserOrderFeign { /** * 一般远程调用的熔断可以直接返回null * @param id * @return */ @Override public Order getOrderByUserId(Integer id) { return null ; } } |
3.3服务消费者
pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <? 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 > <!--用于在应用程序中添加各种监控和管理功能--> < 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配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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 #服务实例的续约时间间隔 feign: hystrix: enabled: true #开启熔断 management: endpoints: web: exposure: include: '*' |
启动类
1 2 3 4 5 6 7 8 | @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class UserServiceApplication { public static void main(String[] args) { SpringApplication.run(UserServiceApplication. class ,args); } } |
controller
1 2 3 4 5 6 7 8 9 | @RestController public class UserController { @Autowired private UserOrderFeign userOrderFeign; @GetMapping ( "findOrder" ) public Order findOrder(){ return userOrderFeign.getOrderByUserId( 1 ); } } |
3.4服务提供者
服务提供者与服务消费者的主要区别是没有依赖actuator以及对应的暴露端点的配置。所以在admin的Web页面中是不为看到服务提供者的详细信息。
1 2 3 4 5 | <!--用于在应用程序中添加各种监控和管理功能--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> |
pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <? 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配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 #服务实例的续约时间间隔 |
项目启动类
1 2 3 4 5 6 7 | @SpringBootApplication @EnableEurekaClient public class OrderServiceApplication { public static void main(String[] args) { SpringApplication.run(OrderServiceApplication. class ,args); } } |
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 | @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 环境搭建与基本使用的文章就介绍到这了,更多相关Spring Boot Admin搭建内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
解决idea npm:无法将“npm”项识别为cmdlet、函数、脚本文件或可运行程序的名称问题
在IDEA中运行npm命令时出现无法识别的错误,通常是由于npm环境变量配置不正确引起,解决方法包括以管理员身份运行IDEA,确认node和npm是否正确安装及配置环境变量,需要在系统环境变量中添加node.js的安装路径,并设置npm的全局模块和缓存路径2024-10-10SpringMVC JSON数据交互及RESTful支持实现方法
这篇文章主要介绍了SpringMVC JSON数据交互及RESTful支持实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-06-06
最新评论