亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Springboot輕量級的監(jiān)控組件SpringbootAdmin

 更新時間:2023年02月10日 16:51:59   作者:程序員拾山  
這篇文章主要為大家介紹了Springboot輕量級的監(jiān)控組件SpringbootAdmin使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

簡介

Springboot Admin是一個管理和監(jiān)控Springboot項目的組件,分為服務(wù)端和客戶端,兩端通過http進行通信。由于其輕量級的特性,所以特別適合中小項目使用。

其效果圖如下:

服務(wù)端配置

1,引入Springboot admin和Spring Security依賴。

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.5.1</version>
</dependency>
<dependency>
      <groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
</dependency>

2,配置相關(guān)屬性。

server:
  port: 8080
  servlet:
    context-path: /server
spring:
  security:
    user:
      #admin Server端登錄時用的賬戶密碼
      name: server123
      password: 123456
  boot:
    admin:
      instance-auth:
        #啟用header驗證
        enabled: true
        #Server訪問client接口時會使用下面的配置生成authorization
        default-user-name: "name_shishan"
        default-password: "pwd_shishan"

3,配置@EnableAdminServer注解

@SpringBootApplication
@Configuration
@EnableAdminServer
public class ServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}

經(jīng)過以上3步,服務(wù)端就可以啟動了。

訪問 http://localhost:8080/server/,就可以看到以下登錄界面。

使用在yml文件中配置的賬戶密碼就可以登錄了。

客戶端配置

1,在我們要監(jiān)控的客戶端中加入以下依賴。

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

2,暴露監(jiān)控接口以及配置Server地址。

客戶端在啟動后會向配置的Server發(fā)起注冊申請,此時為了安全性還需要Server端的賬戶密碼進行校驗。

spring:
  boot:
    admin:
      client:
        #admin注冊地址
        url: http://localhost:8080/server
        #配置admin的賬戶
        username: server123
        password: 123456
admin:
  header:
    auth:
      name: "name_shishan"
      password: "pwd_shishan"
#暴露出端口
management:
  endpoints:
    web:
      exposure:
        include: "*"

3,對暴露的接口進行權(quán)限校驗。

由于我們將監(jiān)控接口進行了暴露,所以必須對相關(guān)的接口進行權(quán)限校驗,否則就有可能泄露相關(guān)信息。

對接口進行權(quán)限過濾有很多種選擇,比如設(shè)置IP訪問的白名單,只允許admin Server所在的服務(wù)器訪問,也可以配置相關(guān)的token等等。

下面我們以一個簡單的接口過濾器實現(xiàn)對/actuator/**相關(guān)接口的權(quán)限校驗。

@Component
public class PathFilter implements Filter {
    @Value("${admin.header.auth.name}")
    private String username;
    @Value("${admin.header.auth.password}")
    private String password;
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        if (antPathMatcher.match("/actuator/**", request.getServletPath())) {
            String authorization = request.getHeader("authorization");
            if (StringUtils.hasText(authorization)) {
                String token = Base64Utils.encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
                if (authorization.equals("Basic " + token)) {
                    //token匹配才放行
                    filterChain.doFilter(request, servletResponse);
                    return;
                }
            }
            response.setContentType("application/json;charset=UTF-8");
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            response.getWriter().print("權(quán)限不足");
            return;
        }
        //其他接口直接放行
        filterChain.doFilter(request, servletResponse);
    }
}

在這個filter中,對actuator相關(guān)的接口進行了header參數(shù)的校驗,只有通過校驗才可以訪問暴露出的actuator接口。

當然,如果我們使用了SpringSecurity或者SaToken這樣的第三方權(quán)限框架,也可以去重寫相關(guān)的配置完成權(quán)限的判斷,原理都是一樣的。

下面我們看一下最終的監(jiān)控效果:

最后

除了通過普通http請求方式獲取監(jiān)控信息以外,Springboot admin還支持通過注冊中心的方式獲取相關(guān)信息,在其官方文檔大家也可以看到相關(guān)的配置。

官方文檔:codecentric.github.io/spring-boot…

以上就是Springboot輕量級的監(jiān)控組件SpringbootAdmin的詳細內(nèi)容,更多關(guān)于Springboot監(jiān)控組件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論