成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用Spring Boot Admin實(shí)時(shí)監(jiān)控你的系統(tǒng)

開發(fā) 前端
Spring Boot Admin(SBA)是一個(gè)管理和監(jiān)視SpringBoot應(yīng)用程序的社區(qū)項(xiàng)目。通過Spring Boot Admin Client(通過HTTP)注冊我們的應(yīng)用程序到Admin Server中,或者使用Spring Cloud?服務(wù)發(fā)現(xiàn)(例如Eureka、Consul)。

環(huán)境:SpringBoot2.3.9.RELEASE + SpringBootAdmin2.3.1

說明:如果使用SpringBootAdmin2.4.*版本那么SpringBoot的版本也必須是2.4.*否則啟動(dòng)報(bào)錯(cuò)。

Spring Boot Admin(SBA)是一個(gè)管理和監(jiān)視SpringBoot應(yīng)用程序的社區(qū)項(xiàng)目。通過Spring Boot Admin Client(通過HTTP)注冊我們的應(yīng)用程序到Admin Server中,或者使用Spring Cloud?服務(wù)發(fā)現(xiàn)(例如Eureka、Consul)。

★ 配置Spring Boot Admin服務(wù)端

  • 添加依賴
<dependencies>
  <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>
    <version>2.3.1</version>
  </dependency>
</dependencies>
  • 啟動(dòng)類添加注解

啟動(dòng)類添加@EnableAdminServer注解

@SpringBootApplication
@EnableAdminServer
public class SpringBootAdminApplication {


  public static void main(String[] args) {
    SpringApplication.run(SpringBootAdminApplication.class, args);
  }


}
  • 應(yīng)用配置文件
server:
  port: 8080
---
spring:
  application:
    name: admin-server
---
spring:
  boot:
    admin:
      context-path: /sba

非常簡單,啟動(dòng)服務(wù)直接訪問:http://localhost:8080/sba

圖片圖片


空空如也,現(xiàn)在我們還沒有客戶端注冊上來,接下來寫個(gè)客戶端。

★ 客戶端注冊

  • 添加依賴
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.3.1</version>
  </dependency>
</dependencies>
  • 安全配置

放行所有的請求

@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
  
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll()  
            .and().csrf().disable();
    }
}
  • 應(yīng)用配置文件
server:
  port: 8081
---
spring:
  application:
    name: admin-client
---
spring:
  boot:
    admin:
      client:
        url:
        - http://localhost:8080/sba


啟動(dòng)客戶端(確保服務(wù)端已經(jīng)啟動(dòng))

圖片圖片


客戶端已經(jīng)注冊上來了,但是這里顯示的地址是主機(jī)名,修改配置顯示ip地址

  • 顯示客戶端IP
spring:
  boot:
    admin:
      client:
        url:
        - http://localhost:8080
        instance:
          prefer-ip: true


圖片圖片

點(diǎn)擊實(shí)例進(jìn)入查看實(shí)例的詳細(xì)信息

圖片圖片

  • 查看日志

應(yīng)用中配置日志功能,在應(yīng)用配置文件中配置logging.file.path or logging.file.name兩個(gè)只能配置一個(gè)

logging:
  file:
    path: d:/logs
  pattern:
    file: '%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx'

這樣配置完后重啟,在實(shí)例的詳細(xì)頁面中就能查看日志信息了

圖片圖片

  • 保護(hù)Server端,添加登錄功能

加入依賴

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>

安全配置

@Configuration(proxyBeanMethods = false)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


  private final AdminServerProperties adminServer;


  private final SecurityProperties security;


  public SecurityConfig(AdminServerProperties adminServer, SecurityProperties security) {
    this.adminServer = adminServer;
    this.security = security;
  }


  @Override
  protected void configure(HttpSecurity http) throws Exception {
    SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
    successHandler.setTargetUrlParameter("redirectTo");
    successHandler.setDefaultTargetUrl(this.adminServer.path("/"));


    http.authorizeRequests((authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**"))
        .permitAll().antMatchers(this.adminServer.path("/actuator/info")).permitAll()
        .antMatchers(this.adminServer.path("/actuator/health")).permitAll()
        .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated())
        .formLogin((formLogin) -> formLogin.loginPage(this.adminServer.path("/login"))
            .successHandler(successHandler).and())
        .logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout")))
        .httpBasic(Customizer.withDefaults())
        .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .ignoringRequestMatchers(
                new AntPathRequestMatcher(this.adminServer.path("/instances"),
                    HttpMethod.POST.toString()),
                new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
                    HttpMethod.DELETE.toString()),
                new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))))
        .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
  }


  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser(security.getUser().getName())
        .password("{noop}" + security.getUser().getPassword()).roles("USER");
  }


}

應(yīng)用配置文件

spring:
  boot:
    admin:
      context-path: /sba
  security:
    user:
      name: admin
      password: admin

配置用戶和密碼

再次啟動(dòng)服務(wù)

圖片圖片

再次啟動(dòng)客戶端,有如下錯(cuò)誤

圖片圖片

修改客戶端配置,需要配置admin server的認(rèn)證信息

spring:
  boot:
    admin:
      client:
        username: admin
        password: admin
        url:
        - http://localhost:8080/sba
        instance:
          prefer-ip: true

添加spring.boot.admin.client.username和spring.boot.admin.client.password用戶名密碼

再次啟動(dòng)注冊成功

圖片圖片

admin server是通過actuator來實(shí)時(shí)監(jiān)控系統(tǒng)的,那如果客戶端的設(shè)置了認(rèn)證信息呢?會(huì)發(fā)生什么情況?

  • 保護(hù)Client端認(rèn)證信息

客戶端加入security

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>

配置認(rèn)證信息

spring:
  security:
    user:
      name: ak
      password: 123456

啟動(dòng)客戶端

圖片圖片

客戶端是注冊上來了,但是信息很少。修改客戶端配置信息

spring:
  boot:
    admin:
      client:
        username: admin
        password: admin
        url:
        - http://localhost:8080/sba
        instance:
          prefer-ip: true
          metadata:
            user.name: ${spring.security.user.name}
            user.password: ${spring.security.user.password}
---
spring:
  security:
    user:
      name: ak
      password: 123456

注冊的時(shí)候配置元信息

再次啟動(dòng)客戶端

圖片圖片

現(xiàn)在完全正常了。

  • 動(dòng)態(tài)修改日志級別

定義一個(gè)接口,輸出參數(shù)信息

@RestController
@RequestMapping("/demo")
public class DemoController {
  
  private static Logger logger = LoggerFactory.getLogger(DemoController.class) ;
  
  @GetMapping("/{id}")
  public Object index(@PathVariable("id") String id) {
    logger.debug("DEBUG接收到參數(shù): {}", id) ;
    logger.info("INFO接收到參數(shù):{}", id) ;
    return id ;
  }
  
}

配置文件中加入日志級別

logging:
  level:
    '[com.pack.controller]': debug

監(jiān)控端查看日志配置

圖片圖片



請求接口查看控制臺輸出


圖片

info, debug都輸出了,通過監(jiān)控端,修改日志級別

圖片圖片

再次請求,查看控制臺輸出

圖片圖片

責(zé)任編輯:武曉燕 來源: 實(shí)戰(zhàn)案例錦集
相關(guān)推薦

2020-11-10 09:19:23

Spring BootJava開發(fā)

2020-12-01 08:32:12

Spring Boot

2022-07-28 06:50:52

微服務(wù)業(yè)務(wù)系統(tǒng)

2022-02-09 20:39:52

Actuator應(yīng)用監(jiān)控

2025-01-26 00:00:40

微服務(wù)架構(gòu)服務(wù)

2023-12-27 18:05:13

2024-06-06 08:06:37

2022-05-18 08:32:05

服務(wù)監(jiān)控Prometheus開源

2024-06-12 08:10:08

2023-04-11 16:04:19

Spring Boo端點(diǎn)運(yùn)維

2017-05-23 15:00:06

PythonDjangoadmin

2023-09-01 08:46:44

2022-01-26 07:01:00

開源社區(qū)項(xiàng)目

2022-07-11 09:36:38

SpringJava開發(fā)

2022-01-14 06:59:39

開源Spring BootSBA

2023-10-11 14:37:21

工具開發(fā)

2024-06-19 08:24:47

2023-11-26 09:10:34

WebSocketgreeting?在線用戶

2018-10-22 15:34:31

Spring Boo監(jiān)控視化

2024-11-11 10:58:03

Spring接口編程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 91社区在线高清 | 一区二区三区不卡视频 | 日本久草 | 亚洲综合一区二区三区 | 亚洲精品大全 | 亚洲一区二区三区高清 | 成人免费在线 | 成人av片在线观看 | 久久精品视频9 | 天天综合干 | 亚洲欧美日韩在线 | 久操亚洲 | 久www| 日本 欧美 三级 高清 视频 | 日本一区二区三区四区 | 久久久久久久久久性 | 亚州av| 日本一区二区三区免费观看 | 欧美一级在线 | 国产精品99久久久久久久久 | 一区二区中文 | 亚洲一区二区免费看 | 亚洲精品一区国语对白 | 精品一区二区三区在线观看国产 | 日韩欧美综合在线视频 | 成人国产免费视频 | 欧美日韩三级在线观看 | 久久久久久久久一区 | 国产精品www | 紧缚调教一区二区三区视频 | 影音先锋中文字幕在线观看 | 午夜国产一区 | 亚洲欧美日韩精品久久亚洲区 | 香蕉婷婷 | 国产一区二区 | 日韩av一区二区在线观看 | 欧美黑人狂野猛交老妇 | 欧美a在线 | 欧美99 | 密室大逃脱第六季大神版在线观看 | 亚洲午夜av |