美文网首页
spring boot admin

spring boot admin

作者: 轻舞凋零 | 来源:发表于2022-10-08 12:09 被阅读0次

    springboot应用的监控

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

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

    此处我们介绍spring-boot-admin

    spring-boot-admin的server

    配置springboot即可

        <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>
    

    spring-boot-admin的client

    代码

    @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);
        }
    }
    

    配置服务

        <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>
    
    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的认证

    服务端

    spring:
      application:
        name: admin-server
      security:
        user:
          name: lili
          password: 123456
    

    认证配置

    @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();
            }
        }
    

    客户端

    spring:
      application:
        name: admin-client
      boot:
        admin:
          client:
            url: http://127.0.0.1:10800
            username: lili
            password: 123456
    

    相关文章

      网友评论

          本文标题:spring boot admin

          本文链接:https://www.haomeiwen.com/subject/iwklirtx.html