每天20分钟之spring boot admin

2022-08-27 14:22:59 浏览数 (1)

springboot应用的监控

对于springboot应用的监控我们有很多选择,因为actuator提供了一组数据

我们可以使用springboot-admin,使用skywalking之类的监控中间件,也可以使用普罗米修斯 graphna

此处我们介绍spring-boot-admin

spring-boot-admin的server

配置springboot即可

代码语言:txt复制
    <dependencies>
        <!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.5.2</version>
        </dependency>
    </dependencies>
image.pngimage.png

spring-boot-admin的client

代码

代码语言:txt复制
@RestController
public class UserController {

    private final ObjectMapper mapper =new ObjectMapper();



    @GetMapping("/user/{uid}")
    public String info(@PathVariable long uid) throws JsonProcessingException {

        @Data
        class IdData {
            private Long uid;
        }

        IdData idData = new IdData();
        idData.setUid(uid);
        return mapper.writeValueAsString(idData);
    }
}

配置服务

代码语言:txt复制
    <dependencies>
        <!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.5.2</version>
        </dependency>
    </dependencies>
代码语言:txt复制
spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://127.0.0.1:10800

server:
  port: 10801
  address: 127.0.0.1
debug: true
management:
    endpoints:
      web:
        exposure:
          include: "*"
    endpoint:
      health:
        show-details: always
logging:
  config: classpath:logback-spring.xml

ab -n 1000 -c 10 http://127.0.0.1:10801/user/123

spring boot admin的认证

服务端

代码语言:txt复制
spring:
  application:
    name: admin-server
  security:
    user:
      name: lili
      password: 123456

认证配置

代码语言:txt复制
@Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
        private final String adminContextPath;

        public SecurityPermitAllConfig(AdminServerProperties adminServerProperties) {
            this.adminContextPath = adminServerProperties.getContextPath();
        }
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
            successHandler.setTargetUrlParameter("redirectTo");
            http.authorizeRequests()
                    .antMatchers(adminContextPath   "/assets/**").permitAll()
                    .antMatchers(adminContextPath   "/login").permitAll()
                    .anyRequest().authenticated()
                    .and().httpBasic()
                    .and().csrf().disable();
        }
    }

客户端

代码语言:txt复制
spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://127.0.0.1:10800
        username: lili
        password: 123456

0 人点赞