spring boot admin

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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容