SpringBoot2.x搭建SpringBootAdmin2.x

时间:2022-12-30 23:17:50

1 说明

  1. 全部配置基于1.8.0_111
  2. 当前SpringBoot使用2.0.5
  3. SpringBootAdmin基于Eureka进行Client发现,Eureka搭建参见SpringBoot2.x搭建Eureka
  4. SpringBootAdmin项目文档参见SpringBootAdmin参考文档

2 创建项目

SpringBoot项目生成器中,输入GroupArtifact,如下配置:
SpringBoot2.x搭建SpringBootAdmin2.x

3 编辑pom.xml文件

pom.xml中新添加如下内容:

 <properties>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
 </properties>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>
<dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.0.3</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
</dependencies>

4 修改application.properties为application.yml文件

编辑application.properties为application.yml文件,添加以下内容:

server:
  port: 8099
spring:
  application:
    name: SpringBootAdmin
  security:
    user:
      name: anxminise
      password: 123456
  boot:
    admin:
      ui:
        title: 忧臣解读

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
eureka:
  instance:
    metadata-map:
      user.name: anxminise
      user.password: 123456
    easeRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    ip-address: 127.0.0.1
    prefer-ip-address: true
    instance-id: ${eureka.instance.ip-address}:${server.port}
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: http://anxminise:123456@127.0.0.1:8888/eureka/

配置参数解释:

参数 说明
security.user.name SpringBootAdmin登录时的用户名
security.user.password SpringBootAdmin登录时的密码
eureka.instance.metadata-map.user.name SpringBootAdmin本身作为一个Eureka客户端被发现,这里由于SpringBootAdmin需要进行登录,因此,此处配置SpringBootAdmin登录时使用的用户名
eureka.instance.metadata-map.user.password 同上,配置SpringBootAdmin登录使用的密码

5 编辑SpbadminApplication.java文件

最后编辑SpbadminApplication.java文件,修改为:

@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class SpbadminApplication {

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

    @Configuration
    public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
        private final String adminContextPath;

        public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
            this.adminContextPath = adminServerProperties.getContextPath();
        }

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

            http.authorizeRequests()
                    .antMatchers(adminContextPath + "/assets/**").permitAll()
                    .antMatchers(adminContextPath + "/login").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                    .logout().logoutUrl(adminContextPath + "/logout").and()
                    .httpBasic().and()
                    .csrf()
                    .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                    .ignoringAntMatchers(
                            adminContextPath + "/instances",
                            adminContextPath + "/actuator/**"
                    );
        }
    }
}

6 编译并启动运行

注:本次SpringBootAdmin是基于Eureka搭建的,需要先启动Eureka
./mvnw clean package -DskipTests #编辑应用
java -jar target/spbadmin-0.0.1-SNAPSHOT.jar #启动应用,注:jar包的名称应换成自己的

SpringBoot2.x搭建SpringBootAdmin2.x
SpringBoot2.x搭建SpringBootAdmin2.x
SpringBoot2.x搭建SpringBootAdmin2.x

7 Eureka客户端监控

SpringBootAdmin的作用是:监控Eureka中的微服务,这里的微服务,我们使用SpringBoot2.x进行开发,对于SpringBoot2.x的SpringBoot项目,只需在application.yml中新加入以下内容:

logging:
  path: logs/log
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

参数解释说明

参数 说明
logging.path 配置日志的存放路径,此处用于SpringBootAdmin的日志监控
management.* 配置SpringBoot的全部监控点,此处注意:当前配置未对endpoint进行访问保护,勿对外网开放,后续我们加入对其的访问保护