springboot学习28

一、使用SpringBootAdmin
1、创建Admin服务器
Admin服务器通常会作为一个单独的应用。添加依赖:
admin-server的版本要低于等于admin-client的版本,否则会无法注册进服务。

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

配置一个唯一的端口。如:
application.yml:

server:
 port: 9090

启动访问:

http://localhost:9090

在这里插入图片描述
2、注册Admin客户端
1)显示配置Admin客户端
在客户端项目添加依赖:

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

在客户端项目配置Admin服务器的位置。
application.yml:

spring:
 application:
   name: ingredient-service		# 用户识别Admin服务器中的应用
 boot:
   admin:
     client:
       url: http://localhost:9090		# 将spring.boot.admin.client.url 属性设置为Admin服务器的根路径。

2)发现Admin客户端
在Admin服务器的构建文件中,添加依赖:
pom.xml:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

也可在Spring Initializr中选择Eureka Discovery 复选框来添加依赖。

二、了解Admin服务器
使用Admin服务器可以得到运行中应用的大量信息,包括:

通用的健康信息
通过Micrometer和“/metrics”端点发布的所有指标
环境属性
包和类的日志级别
线程跟踪细节
HTTP请求的跟踪情况
审计日志

1、查看应用基本的健康状况和信息
/health端点
在这里插入图片描述

/info端点
在这里插入图片描述

2、观察核心指标
/mertrics
在这里插入图片描述
实时数据,会自动更新,无须刷新页面。
3、环境属性
在这里插入图片描述
4、查看和设置日志级别
在这里插入图片描述
5、监控线程
在这里插入图片描述
三、保护Admin 服务器
1、为Admin服务器启用登录功能
添加依赖:

扫描二维码关注公众号,回复: 12680543 查看本文章
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

将简单的管理员用户名和密码配置在application.yml中,如:

spring:
  security:
    user:
      name: admin
      password: pwd12312

在浏览器访问地址:

http://localhost:9090/

输入用户名密码:
在这里插入图片描述

成功登录页面:

在这里插入图片描述
2、为Actuator启用认证
因为启动了认证保护,而不知道Actuator端点用户名和密码的用户就访问不了。也就是Admin服务器不能消费Actuator端点了,为了解决这个,可以通过2种方式。
Admin服务器的客户端应用可以通过直接向Admin服务器注册通过Eureka发现的方式来向Admin服务器提供其凭证。 如果应用直接在Admin服务器注册自身,那么它可以在注册时将其凭证发送到服务器。
通过配置几个属性:

spring.boot.admin.client.instance.metadata.user.name
spring.boot.admin.client.instance.metadata.user.password

指定了Admin服务器访问应用的Actuator端点是可以使用的凭证信息。
application.yml加入以下,如:

spring:
 boot:
  admin:
   client:
    url: http://localhost:9090
    instance: 
     metadata:
      user.name: ${
    
    spring.security.user.name}
      user.password: ${
    
    spring.security.user.password}

用户名和密码必须要设置在所有向Admin服务器注册的应用中。

如果应用是由Admin服务器通过Eureka发现的,那么需要设置以下2个属性:

eureka.instance.metadata-map.user.name
eureka.instance.metadata-map.user.password

如:
application.yml:

eureka:
 instance:
  metadata-map:
   user.name: admin
   user.password: password

当应用使用Eureka注册时,凭证信息将会包含到Eureka注册记录的元数据中。当Admin服务器发现应用时,会和应用的其他详情一起从Eureka获取它的凭证信息。

猜你喜欢

转载自blog.csdn.net/tongwudi5093/article/details/114368654
28